git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #3014 from tarleb/org-writer-div
e2659a4John MacFarlane
src/Text/Pandoc/Writers/Org.hs+41−7
@@ -1,6 +1,7 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-
2{-
3Copyright (C) 2010-2015 Puneeth Chaganti <punchagan@gmail.com>
3Copyright (C) 2010-2015 Puneeth Chaganti <punchagan@gmail.com>
4+ Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>,
4 and John MacFarlane <jgm@berkeley.edu>
5 and John MacFarlane <jgm@berkeley.edu>
5
6
6This program is free software; you can redistribute it and/or modify
7This program is free software; you can redistribute it and/or modify
@@ -38,7 +39,8 @@ import Text.Pandoc.Shared
38import Text.Pandoc.Writers.Shared
39import Text.Pandoc.Writers.Shared
39import Text.Pandoc.Pretty
40import Text.Pandoc.Pretty
40import Text.Pandoc.Templates (renderTemplate')
41import Text.Pandoc.Templates (renderTemplate')
42+import Data.Char ( toLower )
41−import Data.List ( intersect, intersperse, transpose )
43+import Data.List ( intersect, intersperse, partition, transpose )
42import Control.Monad.State
44import Control.Monad.State
43
45
44data WriterState =
46data WriterState =
@@ -123,12 +125,34 @@ blockToOrg (Div (_,classes@(cls:_),kvs) bs) | "drawer" `elem` classes = do
123 blankline
125 blankline
124blockToOrg (Div attrs bs) = do
126blockToOrg (Div attrs bs) = do
125 contents <- blockListToOrg bs
127 contents <- blockListToOrg bs
128+ let isGreaterBlockClass = (`elem` ["center", "quote"]) . map toLower
129+ return $ case attrs of
130+ ("", [], []) ->
131+ -- nullAttr, treat contents as if it wasn't wrapped
132+ blankline $$ contents $$ blankline
133+ (ident, [], []) ->
134+ -- only an id: add id as an anchor, unwrap the rest
135+ blankline $$ "<<" <> text ident <> ">>" $$ contents $$ blankline
136+ (ident, classes, kv) ->
137+ -- if one class looks like the name of a greater block then output as
138+ -- such: The ID, if present, is added via the #+NAME keyword; other
139+ -- classes and key-value pairs are kept as #+ATTR_HTML attributes.
140+ let
141+ (blockTypeCand, classes') = partition isGreaterBlockClass classes
142+ in case blockTypeCand of
143+ (blockType:classes'') ->
144+ blankline $$ attrHtml (ident, classes'' <> classes', kv) $$
145+ "#+BEGIN_" <> text blockType $$ contents $$
146+ "#+END_" <> text blockType $$ blankline
147+ _ ->
148+ -- fallback: wrap in div tags
149+ let
126− let startTag = tagWithAttrs "div" attrs
150+ startTag = tagWithAttrs "div" attrs
127− let endTag = text "</div>"
151+ endTag = text "</div>"
128− return $ blankline $$ "#+BEGIN_HTML" $$
152+ in blankline $$ "#+BEGIN_HTML" $$
129− nest 2 startTag $$ "#+END_HTML" $$ blankline $$
153+ nest 2 startTag $$ "#+END_HTML" $$ blankline $$
130− contents $$ blankline $$ "#+BEGIN_HTML" $$
154+ contents $$ blankline $$ "#+BEGIN_HTML" $$
131− nest 2 endTag $$ "#+END_HTML" $$ blankline
155+ nest 2 endTag $$ "#+END_HTML" $$ blankline
132blockToOrg (Plain inlines) = inlineListToOrg inlines
156blockToOrg (Plain inlines) = inlineListToOrg inlines
133-- title beginning with fig: indicates that the image is a figure
157-- title beginning with fig: indicates that the image is a figure
134blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
158blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
@@ -260,6 +284,16 @@ propertiesDrawer (ident, classes, kv) =
260 kvToOrgProperty (key, value) =
284 kvToOrgProperty (key, value) =
261 text ":" <> text key <> text ": " <> text value <> cr
285 text ":" <> text key <> text ": " <> text value <> cr
262
286
287+attrHtml :: Attr -> Doc
288+attrHtml ("" , [] , []) = mempty
289+attrHtml (ident, classes, kvs) =
290+ let
291+ name = if (null ident) then mempty else "#+NAME: " <> text ident <> cr
292+ keyword = "#+ATTR_HTML"
293+ classKv = ("class", unwords classes)
294+ kvStrings = map (\(k,v) -> ":" <> k <> " " <> v) (classKv:kvs)
295+ in name <> keyword <> ": " <> text (unwords kvStrings) <> cr
296+
263-- | Convert list of Pandoc block elements to Org.
297-- | Convert list of Pandoc block elements to Org.
264blockListToOrg :: [Block] -- ^ List of block elements
298blockListToOrg :: [Block] -- ^ List of block elements
265 -> State WriterState Doc
299 -> State WriterState Doc
tests/writer.org−72
@@ -405,54 +405,14 @@ Blank line after term, indented marker, alternate markers:
405
405
406Simple block on one line:
406Simple block on one line:
407
407
408−#+BEGIN_HTML
409− <div>
410−#+END_HTML
411−
412foo
408foo
413
409
414−#+BEGIN_HTML
415− </div>
416−#+END_HTML
417−
418And nested without indentation:
410And nested without indentation:
419
411
420−#+BEGIN_HTML
421− <div>
422−#+END_HTML
423−
424−#+BEGIN_HTML
425− <div>
426−#+END_HTML
427−
428−#+BEGIN_HTML
429− <div>
430−#+END_HTML
431−
432foo
412foo
433
413
434−#+BEGIN_HTML
435− </div>
436−#+END_HTML
437−
438−#+BEGIN_HTML
439− </div>
440−#+END_HTML
441−
442−#+BEGIN_HTML
443− <div>
444−#+END_HTML
445−
446bar
414bar
447
415
448−#+BEGIN_HTML
449− </div>
450−#+END_HTML
451−
452−#+BEGIN_HTML
453− </div>
454−#+END_HTML
455−
456Interpreted markdown in a table:
416Interpreted markdown in a table:
457
417
458#+BEGIN_HTML
418#+BEGIN_HTML
@@ -497,16 +457,8 @@ And this is *strong*
497
457
498Here's a simple block:
458Here's a simple block:
499
459
500−#+BEGIN_HTML
501− <div>
502−#+END_HTML
503−
504foo
460foo
505
461
506−#+BEGIN_HTML
507− </div>
508−#+END_HTML
509−
510This should be a code block, though:
462This should be a code block, though:
511
463
512#+BEGIN_EXAMPLE
464#+BEGIN_EXAMPLE
@@ -523,32 +475,8 @@ As should this:
523
475
524Now, nested:
476Now, nested:
525
477
526−#+BEGIN_HTML
527− <div>
528−#+END_HTML
529−
530−#+BEGIN_HTML
531− <div>
532−#+END_HTML
533−
534−#+BEGIN_HTML
535− <div>
536−#+END_HTML
537−
538foo
478foo
539
479
540−#+BEGIN_HTML
541− </div>
542−#+END_HTML
543−
544−#+BEGIN_HTML
545− </div>
546−#+END_HTML
547−
548−#+BEGIN_HTML
549− </div>
550−#+END_HTML
551−
552This should just be an HTML comment:
480This should just be an HTML comment:
553
481
554#+BEGIN_HTML
482#+BEGIN_HTML