git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #3014 from tarleb/org-writer-div

Org writer: improve Div handling

commite2659a46db3e3b118fe491bf5fd0cdae0268ef90 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 5 Jul 2016, 12:46 (10 years ago) parent6ea8ff15378b7c
2 files changed+41−79
src/Text/Pandoc/Writers/Org.hs+41−7 tests/writer.org−72
src/Text/Pandoc/Writers/Org.hs+41−7
@@ -1,6 +1,7 @@
11{-# LANGUAGE OverloadedStrings #-}
22{-
33Copyright (C) 2010-2015 Puneeth Chaganti <punchagan@gmail.com>
4+ Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>,
45 and John MacFarlane <jgm@berkeley.edu>
56
67This program is free software; you can redistribute it and/or modify
@@ -38,7 +39,8 @@ import Text.Pandoc.Shared
3839import Text.Pandoc.Writers.Shared
3940import Text.Pandoc.Pretty
4041import Text.Pandoc.Templates (renderTemplate')
41−import Data.List ( intersect, intersperse, transpose )
42+import Data.Char ( toLower )
43+import Data.List ( intersect, intersperse, partition, transpose )
4244import Control.Monad.State
4345
4446data WriterState =
@@ -123,12 +125,34 @@ blockToOrg (Div (_,classes@(cls:_),kvs) bs) | "drawer" `elem` classes = do
123125 blankline
124126blockToOrg (Div attrs bs) = do
125127 contents <- blockListToOrg bs
126− let startTag = tagWithAttrs "div" attrs
127− let endTag = text "</div>"
128− return $ blankline $$ "#+BEGIN_HTML" $$
129− nest 2 startTag $$ "#+END_HTML" $$ blankline $$
130− contents $$ blankline $$ "#+BEGIN_HTML" $$
131− nest 2 endTag $$ "#+END_HTML" $$ blankline
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
150+ startTag = tagWithAttrs "div" attrs
151+ endTag = text "</div>"
152+ in blankline $$ "#+BEGIN_HTML" $$
153+ nest 2 startTag $$ "#+END_HTML" $$ blankline $$
154+ contents $$ blankline $$ "#+BEGIN_HTML" $$
155+ nest 2 endTag $$ "#+END_HTML" $$ blankline
132156blockToOrg (Plain inlines) = inlineListToOrg inlines
133157-- title beginning with fig: indicates that the image is a figure
134158blockToOrg (Para [Image attr txt (src,'f':'i':'g':':':tit)]) = do
@@ -260,6 +284,16 @@ propertiesDrawer (ident, classes, kv) =
260284 kvToOrgProperty (key, value) =
261285 text ":" <> text key <> text ": " <> text value <> cr
262286
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+
263297-- | Convert list of Pandoc block elements to Org.
264298blockListToOrg :: [Block] -- ^ List of block elements
265299 -> State WriterState Doc
tests/writer.org−72
@@ -405,54 +405,14 @@ Blank line after term, indented marker, alternate markers:
405405
406406Simple block on one line:
407407
408−#+BEGIN_HTML
409− <div>
410−#+END_HTML
411−
412408foo
413409
414−#+BEGIN_HTML
415− </div>
416−#+END_HTML
417−
418410And nested without indentation:
419411
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−
432412foo
433413
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−
446414bar
447415
448−#+BEGIN_HTML
449− </div>
450−#+END_HTML
451−
452−#+BEGIN_HTML
453− </div>
454−#+END_HTML
455−
456416Interpreted markdown in a table:
457417
458418#+BEGIN_HTML
@@ -497,16 +457,8 @@ And this is *strong*
497457
498458Here's a simple block:
499459
500−#+BEGIN_HTML
501− <div>
502−#+END_HTML
503−
504460foo
505461
506−#+BEGIN_HTML
507− </div>
508−#+END_HTML
509−
510462This should be a code block, though:
511463
512464#+BEGIN_EXAMPLE
@@ -523,32 +475,8 @@ As should this:
523475
524476Now, nested:
525477
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−
538478foo
539479
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−
552480This should just be an HTML comment:
553481
554482#+BEGIN_HTML