git.iko.soy/typeable/pandoc
on master help sign in
ConTeXt writer: new section syntax and --section-divs (#4295)

Fixes #2609. This PR introduces the new-style section headings: `\section[my-header]{My Header}` -> `\section[title={My Header},reference={my-header}]`. On top of this, the ConTeXt writer now supports the `--section-divs` option to write sections in the fenced style, with `\startsection` and `\stopsection`.

commit751b5ad010794ec51699bfb89de91b38c85d3297 authorHenri Menke <henri@icp.uni-stuttgart.de> dateFri 26 Jan 2018, 08:56 (9 years ago) parent0d7aedc
5 files changed+103−56
src/Text/Pandoc/Writers/ConTeXt.hs+37−17 test/Tests/Writers/ConTeXt.hs+24−1 test/command/3968.md+3−2 test/writer.context+35−32 test/writers-lang-and-dir.context+4−4
src/Text/Pandoc/Writers/ConTeXt.hs+37−17
@@ -159,8 +159,9 @@ elementToConTeXt :: PandocMonad m => WriterOptions -> Element -> WM m Doc
159159elementToConTeXt _ (Blk block) = blockToConTeXt block
160160elementToConTeXt opts (Sec level _ attr title' elements) = do
161161 header' <- sectionHeader attr level title'
162+ footer' <- sectionFooter attr level
162163 innerContents <- mapM (elementToConTeXt opts) elements
163− return $ vcat (header' : innerContents)
164+ return $ header' $$ vcat innerContents $$ footer'
164165
165166-- | Convert Pandoc block element to ConTeXt.
166167blockToConTeXt :: PandocMonad m => Block -> WM m Doc
@@ -485,32 +486,51 @@ sectionHeader :: PandocMonad m
485486 -> Int
486487 -> [Inline]
487488 -> WM m Doc
488−sectionHeader (ident,classes,_) hdrLevel lst = do
489+sectionHeader (ident,classes,kvs) hdrLevel lst = do
490+ opts <- gets stOptions
489491 contents <- inlineListToConTeXt lst
490− st <- get
491− let opts = stOptions st
492+ levelText <- sectionLevelToText opts (ident,classes,kvs) hdrLevel
493+ let ident' = if null ident
494+ then empty
495+ else "reference=" <> braces (text (toLabel ident))
496+ let contents' = if contents == empty
497+ then empty
498+ else "title=" <> braces contents
499+ let options = if keys == empty || levelText == empty
500+ then empty
501+ else brackets keys
502+ where keys = hcat $ intersperse "," $ filter (empty /=) [contents', ident']
503+ let starter = if writerSectionDivs opts
504+ then "\\start"
505+ else "\\"
506+ return $ starter <> levelText <> options <> blankline
507+
508+-- | Craft the section footer
509+sectionFooter :: PandocMonad m => Attr -> Int -> WM m Doc
510+sectionFooter attr hdrLevel = do
511+ opts <- gets stOptions
512+ levelText <- sectionLevelToText opts attr hdrLevel
513+ return $ if writerSectionDivs opts
514+ then "\\stop" <> levelText <> blankline
515+ else empty
516+
517+-- | Generate a textual representation of the section level
518+sectionLevelToText :: PandocMonad m => WriterOptions -> Attr -> Int -> WM m Doc
519+sectionLevelToText opts (_,classes,_) hdrLevel = do
492520 let level' = case writerTopLevelDivision opts of
493521 TopLevelPart -> hdrLevel - 2
494522 TopLevelChapter -> hdrLevel - 1
495523 TopLevelSection -> hdrLevel
496524 TopLevelDefault -> hdrLevel
497− let ident' = if null ident
498− then empty
499− else brackets (text (toLabel ident))
500525 let (section, chapter) = if "unnumbered" `elem` classes
501526 then (text "subject", text "title")
502527 else (text "section", text "chapter")
503528 return $ case level' of
504− -1 -> text "\\part" <> ident' <> braces contents
505− 0 -> char '\\' <> chapter <> ident' <>
506− braces contents
507− n | n >= 1 && n <= 5 -> char '\\'
508− <> text (concat (replicate (n - 1) "sub"))
509− <> section
510− <> ident'
511− <> braces contents
512− <> blankline
513− _ -> contents <> blankline
529+ -1 -> text "part"
530+ 0 -> chapter
531+ n | n >= 1 -> text (concat (replicate (n - 1) "sub"))
532+ <> section
533+ _ -> empty -- cannot happen
514534
515535fromBCP47 :: PandocMonad m => Maybe String -> WM m (Maybe String)
516536fromBCP47 mbs = fromBCP47' <$> toLang mbs
test/Tests/Writers/ConTeXt.hs+24−1
@@ -18,6 +18,9 @@ context' = unpack . purely (writeConTeXt def{ writerWrapText = WrapNone }) . toP
1818contextNtb :: (ToPandoc a) => a -> String
1919contextNtb = unpack . purely (writeConTeXt def{ writerExtensions = enableExtension Ext_ntb pandocExtensions }) . toPandoc
2020
21+contextDiv :: (ToPandoc a) => a -> String
22+contextDiv = unpack . purely (writeConTeXt def{ writerSectionDivs = True }) . toPandoc
23+
2124{-
2225 "my test" =: X =?> Y
2326
@@ -47,7 +50,27 @@ tests = [ testGroup "inline code"
4750 ]
4851 , testGroup "headers"
4952 [ "level 1" =:
50− headerWith ("my-header",[],[]) 1 "My header" =?> "\\section[my-header]{My header}"
53+ headerWith ("my-header",[],[]) 1 "My header" =?> "\\section[title={My header},reference={my-header}]"
54+ , test contextDiv "section-divs" $
55+ ( headerWith ("header1", [], []) 1 (text "Header1")
56+ <> headerWith ("header2", [], []) 2 (text "Header2")
57+ <> headerWith ("header3", [], []) 3 (text "Header3")
58+ <> headerWith ("header4", [], []) 4 (text "Header4")
59+ <> headerWith ("header5", [], []) 5 (text "Header5")
60+ <> headerWith ("header6", [], []) 6 (text "Header6"))
61+ =?>
62+ unlines [ "\\startsection[title={Header1},reference={header1}]\n"
63+ , "\\startsubsection[title={Header2},reference={header2}]\n"
64+ , "\\startsubsubsection[title={Header3},reference={header3}]\n"
65+ , "\\startsubsubsubsection[title={Header4},reference={header4}]\n"
66+ , "\\startsubsubsubsubsection[title={Header5},reference={header5}]\n"
67+ , "\\startsubsubsubsubsubsection[title={Header6},reference={header6}]\n"
68+ , "\\stopsubsubsubsubsubsection\n"
69+ , "\\stopsubsubsubsubsection\n"
70+ , "\\stopsubsubsubsection\n"
71+ , "\\stopsubsubsection\n"
72+ , "\\stopsubsection\n"
73+ , "\\stopsection" ]
5174 ]
5275 , testGroup "bullet lists"
5376 [ "nested" =:
test/command/3968.md+3−2
@@ -4,6 +4,7 @@
44
55## Section
66^D
7−\chapter[chapter]{Chapter}
8−\section[section]{Section}
7+\chapter[title={Chapter},reference={chapter}]
8+
9+\section[title={Section},reference={section}]
910```
test/writer.context+35−32
@@ -72,32 +72,34 @@ markdown test suite.
7272
7373\thinrule
7474
75−\section[headers]{Headers}
75+\section[title={Headers},reference={headers}]
7676
77−\subsection[level-2-with-an-embedded-link]{Level 2 with an
78−\useURL[url1][/url][][embedded link]\from[url1]}
77+\subsection[title={Level 2 with an \useURL[url1][/url][][embedded
78+link]\from[url1]},reference={level-2-with-an-embedded-link}]
7979
80−\subsubsection[level-3-with-emphasis]{Level 3 with {\em emphasis}}
80+\subsubsection[title={Level 3 with
81+{\em emphasis}},reference={level-3-with-emphasis}]
8182
82−\subsubsubsection[level-4]{Level 4}
83+\subsubsubsection[title={Level 4},reference={level-4}]
8384
84−\subsubsubsubsection[level-5]{Level 5}
85+\subsubsubsubsection[title={Level 5},reference={level-5}]
8586
86−\section[level-1]{Level 1}
87+\section[title={Level 1},reference={level-1}]
8788
88−\subsection[level-2-with-emphasis]{Level 2 with {\em emphasis}}
89+\subsection[title={Level 2 with
90+{\em emphasis}},reference={level-2-with-emphasis}]
8991
90−\subsubsection[level-3]{Level 3}
92+\subsubsection[title={Level 3},reference={level-3}]
9193
9294with no blank line
9395
94−\subsection[level-2]{Level 2}
96+\subsection[title={Level 2},reference={level-2}]
9597
9698with no blank line
9799
98100\thinrule
99101
100−\section[paragraphs]{Paragraphs}
102+\section[title={Paragraphs},reference={paragraphs}]
101103
102104Here's a regular paragraph.
103105
@@ -112,7 +114,7 @@ here.
112114
113115\thinrule
114116
115−\section[block-quotes]{Block Quotes}
117+\section[title={Block Quotes},reference={block-quotes}]
116118
117119E-mail style:
118120
@@ -155,7 +157,7 @@ And a following paragraph.
155157
156158\thinrule
157159
158−\section[code-blocks]{Code Blocks}
160+\section[title={Code Blocks},reference={code-blocks}]
159161
160162Code:
161163
@@ -179,9 +181,9 @@ These should not be escaped: \$ \\ \> \[ \{
179181
180182\thinrule
181183
182−\section[lists]{Lists}
184+\section[title={Lists},reference={lists}]
183185
184−\subsection[unordered]{Unordered}
186+\subsection[title={Unordered},reference={unordered}]
185187
186188Asterisks tight:
187189
@@ -249,7 +251,7 @@ Minuses loose:
249251 Minus 3
250252\stopitemize
251253
252−\subsection[ordered]{Ordered}
254+\subsection[title={Ordered},reference={ordered}]
253255
254256Tight:
255257
@@ -308,7 +310,7 @@ Multiple paragraphs:
308310 Item 3.
309311\stopitemize
310312
311−\subsection[nested]{Nested}
313+\subsection[title={Nested},reference={nested}]
312314
313315\startitemize[packed]
314316\item
@@ -362,7 +364,7 @@ Same thing but with paragraphs:
362364 Third
363365\stopitemize
364366
365−\subsection[tabs-and-spaces]{Tabs and spaces}
367+\subsection[title={Tabs and spaces},reference={tabs-and-spaces}]
366368
367369\startitemize
368370\item
@@ -378,7 +380,7 @@ Same thing but with paragraphs:
378380 \stopitemize
379381\stopitemize
380382
381−\subsection[fancy-list-markers]{Fancy list markers}
383+\subsection[title={Fancy list markers},reference={fancy-list-markers}]
382384
383385\startitemize[n][start=2,left=(,stopper=),width=2.0em]
384386\item
@@ -442,7 +444,7 @@ B. Williams
442444
443445\thinrule
444446
445−\section[definition-lists]{Definition Lists}
447+\section[title={Definition Lists},reference={definition-lists}]
446448
447449Tight using spaces:
448450
@@ -553,7 +555,7 @@ Blank line after term, indented marker, alternate markers:
553555 \stopitemize
554556\stopdescription
555557
556−\section[html-blocks]{HTML Blocks}
558+\section[title={HTML Blocks},reference={html-blocks}]
557559
558560Simple block on one line:
559561
@@ -613,7 +615,7 @@ Hr's:
613615
614616\thinrule
615617
616−\section[inline-markup]{Inline Markup}
618+\section[title={Inline Markup},reference={inline-markup}]
617619
618620This is {\em emphasized}, and so {\em is this}.
619621
@@ -642,7 +644,8 @@ spaces: a^b c^d, a\lettertilde{}b c\lettertilde{}d.
642644
643645\thinrule
644646
645−\section[smart-quotes-ellipses-dashes]{Smart quotes, ellipses, dashes}
647+\section[title={Smart quotes, ellipses,
648+dashes},reference={smart-quotes-ellipses-dashes}]
646649
647650\quotation{Hello,} said the spider. \quotation{\quote{Shelob} is my name.}
648651
@@ -665,7 +668,7 @@ Ellipses\ldots{}and\ldots{}and\ldots{}.
665668
666669\thinrule
667670
668−\section[latex]{LaTeX}
671+\section[title={LaTeX},reference={latex}]
669672
670673\startitemize[packed]
671674\item
@@ -705,7 +708,7 @@ Here's a LaTeX table:
705708
706709\thinrule
707710
708−\section[special-characters]{Special Characters}
711+\section[title={Special Characters},reference={special-characters}]
709712
710713Here is some unicode:
711714
@@ -766,9 +769,9 @@ Minus: -
766769
767770\thinrule
768771
769−\section[links]{Links}
772+\section[title={Links},reference={links}]
770773
771−\subsection[explicit]{Explicit}
774+\subsection[title={Explicit},reference={explicit}]
772775
773776Just a \useURL[url4][/url/][][URL]\from[url4].
774777
@@ -788,7 +791,7 @@ Just a \useURL[url4][/url/][][URL]\from[url4].
788791
789792\useURL[url12][][][Empty]\from[url12].
790793
791−\subsection[reference]{Reference}
794+\subsection[title={Reference},reference={reference}]
792795
793796Foo \useURL[url13][/url/][][bar]\from[url13].
794797
@@ -812,7 +815,7 @@ Foo \useURL[url19][/url/][][bar]\from[url19].
812815
813816Foo \useURL[url20][/url/][][biz]\from[url20].
814817
815−\subsection[with-ampersands]{With ampersands}
818+\subsection[title={With ampersands},reference={with-ampersands}]
816819
817820Here's a \useURL[url21][http://example.com/?foo=1&bar=2][][link with an
818821ampersand in the URL]\from[url21].
@@ -825,7 +828,7 @@ Here's an \useURL[url23][/script?foo=1&bar=2][][inline link]\from[url23].
825828Here's an \useURL[url24][/script?foo=1&bar=2][][inline link in pointy
826829braces]\from[url24].
827830
828−\subsection[autolinks]{Autolinks}
831+\subsection[title={Autolinks},reference={autolinks}]
829832
830833With an ampersand: \useURL[url25][http://example.com/?foo=1&bar=2]\from[url25]
831834
@@ -853,7 +856,7 @@ or here: <http://example.com/>
853856
854857\thinrule
855858
856−\section[images]{Images}
859+\section[title={Images},reference={images}]
857860
858861From \quotation{Voyage dans la Lune} by Georges Melies (1902):
859862
@@ -863,7 +866,7 @@ Here is a movie {\externalfigure[movie.jpg]} icon.
863866
864867\thinrule
865868
866−\section[footnotes]{Footnotes}
869+\section[title={Footnotes},reference={footnotes}]
867870
868871Here is a footnote reference,\footnote{Here is the footnote. It can go
869872 anywhere after the footnote reference. It need not be placed at the end of
test/writers-lang-and-dir.context+4−4
@@ -57,7 +57,7 @@
5757
5858\starttext
5959
60−\section[empty-divs-and-spans]{Empty Divs and Spans}
60+\section[title={Empty Divs and Spans},reference={empty-divs-and-spans}]
6161
6262Some text and
6363
@@ -67,7 +67,7 @@ and more text.
6767
6868Next paragraph with a span and a word-thatincludesaspanright?
6969
70−\section[directionality]{Directionality}
70+\section[title={Directionality},reference={directionality}]
7171
7272Some text and
7373
@@ -86,7 +86,7 @@ and a ltr div. with a {\righttoleft rtl span}.
8686Next paragraph with a {\righttoleft rtl span} and a
8787word-that-includesa{\lefttoright ltrspan}right?
8888
89−\section[languages]{Languages}
89+\section[title={Languages},reference={languages}]
9090
9191Some text and
9292
@@ -102,7 +102,7 @@ word-that-includesa\start\language[de-ch]Swiss German span\stop right?
102102
103103Some \start\language[es]Spanish text\stop .
104104
105−\section[combined]{Combined}
105+\section[title={Combined},reference={combined}]
106106
107107Some text and
108108