Skip to content

Commit

Permalink
ConTeXt writer: new section syntax and --section-divs
Browse files Browse the repository at this point in the history
  • Loading branch information
hmenke committed Jan 23, 2018
1 parent 517f65a commit e9eb750
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 55 deletions.
52 changes: 36 additions & 16 deletions src/Text/Pandoc/Writers/ConTeXt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ elementToConTeXt :: PandocMonad m => WriterOptions -> Element -> WM m Doc
elementToConTeXt _ (Blk block) = blockToConTeXt block
elementToConTeXt opts (Sec level _ attr title' elements) = do
header' <- sectionHeader attr level title'
footer' <- sectionFooter attr level
innerContents <- mapM (elementToConTeXt opts) elements
return $ vcat (header' : innerContents)
return $ header' $$ vcat innerContents $$ footer'

-- | Convert Pandoc block element to ConTeXt.
blockToConTeXt :: PandocMonad m => Block -> WM m Doc
Expand Down Expand Up @@ -485,32 +486,51 @@ sectionHeader :: PandocMonad m
-> Int
-> [Inline]
-> WM m Doc
sectionHeader (ident,classes,_) hdrLevel lst = do
sectionHeader (ident,classes,kvs) hdrLevel lst = do
opts <- gets stOptions
contents <- inlineListToConTeXt lst
st <- get
let opts = stOptions st
levelText <- sectionLevelToText opts (ident,classes,kvs) hdrLevel
let ident' = if null ident
then empty
else "reference=" <> braces (text (toLabel ident))
let contents' = if contents == empty
then empty
else "title=" <> braces contents
let options = if keys == empty || levelText == empty
then empty
else brackets keys
where keys = hcat $ intersperse "," $ filter (empty /=) [contents', ident']
let starter = if writerSectionDivs opts
then "\\start"
else "\\"
return $ starter <> levelText <> options <> blankline

-- | Craft the section footer
sectionFooter :: PandocMonad m => Attr -> Int -> WM m Doc
sectionFooter attr hdrLevel = do
opts <- gets stOptions
levelText <- sectionLevelToText opts attr hdrLevel
return $ if writerSectionDivs opts
then "\\stop" <> levelText <> blankline
else empty

-- | Generate a textual representation of the section level
sectionLevelToText :: PandocMonad m => WriterOptions -> Attr -> Int -> WM m Doc
sectionLevelToText opts (_,classes,_) hdrLevel = do
let level' = case writerTopLevelDivision opts of
TopLevelPart -> hdrLevel - 2
TopLevelChapter -> hdrLevel - 1
TopLevelSection -> hdrLevel
TopLevelDefault -> hdrLevel
let ident' = if null ident
then empty
else brackets (text (toLabel ident))
let (section, chapter) = if "unnumbered" `elem` classes
then (text "subject", text "title")
else (text "section", text "chapter")
return $ case level' of
-1 -> text "\\part" <> ident' <> braces contents
0 -> char '\\' <> chapter <> ident' <>
braces contents
n | n >= 1 && n <= 5 -> char '\\'
<> text (concat (replicate (n - 1) "sub"))
-1 -> text "part"
0 -> chapter
n | n >= 1 && n <= 5 -> text (concat (replicate (n - 1) "sub"))
<> section
<> ident'
<> braces contents
<> blankline
_ -> contents <> blankline
_ -> empty

fromBCP47 :: PandocMonad m => Maybe String -> WM m (Maybe String)
fromBCP47 mbs = fromBCP47' <$> toLang mbs
Expand Down
2 changes: 1 addition & 1 deletion test/Tests/Writers/ConTeXt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tests = [ testGroup "inline code"
]
, testGroup "headers"
[ "level 1" =:
headerWith ("my-header",[],[]) 1 "My header" =?> "\\section[my-header]{My header}"
headerWith ("my-header",[],[]) 1 "My header" =?> "\\section[title={My header},reference={my-header}]"
]
, testGroup "bullet lists"
[ "nested" =:
Expand Down
5 changes: 3 additions & 2 deletions test/command/3968.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## Section
^D
\chapter[chapter]{Chapter}
\section[section]{Section}
\chapter[title={Chapter},reference={chapter}]
\section[title={Section},reference={section}]
```
67 changes: 35 additions & 32 deletions test/writer.context
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,34 @@ markdown test suite.

\thinrule

\section[headers]{Headers}
\section[title={Headers},reference={headers}]

\subsection[level-2-with-an-embedded-link]{Level 2 with an
\useURL[url1][/url][][embedded link]\from[url1]}
\subsection[title={Level 2 with an \useURL[url1][/url][][embedded
link]\from[url1]},reference={level-2-with-an-embedded-link}]

\subsubsection[level-3-with-emphasis]{Level 3 with {\em emphasis}}
\subsubsection[title={Level 3 with
{\em emphasis}},reference={level-3-with-emphasis}]

\subsubsubsection[level-4]{Level 4}
\subsubsubsection[title={Level 4},reference={level-4}]

\subsubsubsubsection[level-5]{Level 5}
\subsubsubsubsection[title={Level 5},reference={level-5}]

\section[level-1]{Level 1}
\section[title={Level 1},reference={level-1}]

\subsection[level-2-with-emphasis]{Level 2 with {\em emphasis}}
\subsection[title={Level 2 with
{\em emphasis}},reference={level-2-with-emphasis}]

\subsubsection[level-3]{Level 3}
\subsubsection[title={Level 3},reference={level-3}]

with no blank line

\subsection[level-2]{Level 2}
\subsection[title={Level 2},reference={level-2}]

with no blank line

\thinrule

\section[paragraphs]{Paragraphs}
\section[title={Paragraphs},reference={paragraphs}]

Here's a regular paragraph.

Expand All @@ -112,7 +114,7 @@ here.

\thinrule

\section[block-quotes]{Block Quotes}
\section[title={Block Quotes},reference={block-quotes}]

E-mail style:

Expand Down Expand Up @@ -155,7 +157,7 @@ And a following paragraph.

\thinrule

\section[code-blocks]{Code Blocks}
\section[title={Code Blocks},reference={code-blocks}]

Code:

Expand All @@ -179,9 +181,9 @@ These should not be escaped: \$ \\ \> \[ \{

\thinrule

\section[lists]{Lists}
\section[title={Lists},reference={lists}]

\subsection[unordered]{Unordered}
\subsection[title={Unordered},reference={unordered}]

Asterisks tight:

Expand Down Expand Up @@ -249,7 +251,7 @@ Minuses loose:
Minus 3
\stopitemize

\subsection[ordered]{Ordered}
\subsection[title={Ordered},reference={ordered}]

Tight:

Expand Down Expand Up @@ -308,7 +310,7 @@ Multiple paragraphs:
Item 3.
\stopitemize

\subsection[nested]{Nested}
\subsection[title={Nested},reference={nested}]

\startitemize[packed]
\item
Expand Down Expand Up @@ -362,7 +364,7 @@ Same thing but with paragraphs:
Third
\stopitemize

\subsection[tabs-and-spaces]{Tabs and spaces}
\subsection[title={Tabs and spaces},reference={tabs-and-spaces}]

\startitemize
\item
Expand All @@ -378,7 +380,7 @@ Same thing but with paragraphs:
\stopitemize
\stopitemize

\subsection[fancy-list-markers]{Fancy list markers}
\subsection[title={Fancy list markers},reference={fancy-list-markers}]

\startitemize[n][start=2,left=(,stopper=),width=2.0em]
\item
Expand Down Expand Up @@ -442,7 +444,7 @@ B. Williams

\thinrule

\section[definition-lists]{Definition Lists}
\section[title={Definition Lists},reference={definition-lists}]

Tight using spaces:

Expand Down Expand Up @@ -553,7 +555,7 @@ Blank line after term, indented marker, alternate markers:
\stopitemize
\stopdescription

\section[html-blocks]{HTML Blocks}
\section[title={HTML Blocks},reference={html-blocks}]

Simple block on one line:

Expand Down Expand Up @@ -613,7 +615,7 @@ Hr's:

\thinrule

\section[inline-markup]{Inline Markup}
\section[title={Inline Markup},reference={inline-markup}]

This is {\em emphasized}, and so {\em is this}.

Expand Down Expand Up @@ -642,7 +644,8 @@ spaces: a^b c^d, a\lettertilde{}b c\lettertilde{}d.

\thinrule

\section[smart-quotes-ellipses-dashes]{Smart quotes, ellipses, dashes}
\section[title={Smart quotes, ellipses,
dashes},reference={smart-quotes-ellipses-dashes}]

\quotation{Hello,} said the spider. \quotation{\quote{Shelob} is my name.}

Expand All @@ -665,7 +668,7 @@ Ellipses\ldots{}and\ldots{}and\ldots{}.

\thinrule

\section[latex]{LaTeX}
\section[title={LaTeX},reference={latex}]

\startitemize[packed]
\item
Expand Down Expand Up @@ -705,7 +708,7 @@ Here's a LaTeX table:

\thinrule

\section[special-characters]{Special Characters}
\section[title={Special Characters},reference={special-characters}]

Here is some unicode:

Expand Down Expand Up @@ -766,9 +769,9 @@ Minus: -

\thinrule

\section[links]{Links}
\section[title={Links},reference={links}]

\subsection[explicit]{Explicit}
\subsection[title={Explicit},reference={explicit}]

Just a \useURL[url4][/url/][][URL]\from[url4].

Expand All @@ -788,7 +791,7 @@ Just a \useURL[url4][/url/][][URL]\from[url4].

\useURL[url12][][][Empty]\from[url12].

\subsection[reference]{Reference}
\subsection[title={Reference},reference={reference}]

Foo \useURL[url13][/url/][][bar]\from[url13].

Expand All @@ -812,7 +815,7 @@ Foo \useURL[url19][/url/][][bar]\from[url19].

Foo \useURL[url20][/url/][][biz]\from[url20].

\subsection[with-ampersands]{With ampersands}
\subsection[title={With ampersands},reference={with-ampersands}]

Here's a \useURL[url21][http://example.com/?foo=1&bar=2][][link with an
ampersand in the URL]\from[url21].
Expand All @@ -825,7 +828,7 @@ Here's an \useURL[url23][/script?foo=1&bar=2][][inline link]\from[url23].
Here's an \useURL[url24][/script?foo=1&bar=2][][inline link in pointy
braces]\from[url24].

\subsection[autolinks]{Autolinks}
\subsection[title={Autolinks},reference={autolinks}]

With an ampersand: \useURL[url25][http://example.com/?foo=1&bar=2]\from[url25]

Expand Down Expand Up @@ -853,7 +856,7 @@ or here: <http://example.com/>

\thinrule

\section[images]{Images}
\section[title={Images},reference={images}]

From \quotation{Voyage dans la Lune} by Georges Melies (1902):

Expand All @@ -863,7 +866,7 @@ Here is a movie {\externalfigure[movie.jpg]} icon.

\thinrule

\section[footnotes]{Footnotes}
\section[title={Footnotes},reference={footnotes}]

Here is a footnote reference,\footnote{Here is the footnote. It can go
anywhere after the footnote reference. It need not be placed at the end of
Expand Down
8 changes: 4 additions & 4 deletions test/writers-lang-and-dir.context
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

\starttext

\section[empty-divs-and-spans]{Empty Divs and Spans}
\section[title={Empty Divs and Spans},reference={empty-divs-and-spans}]

Some text and

Expand All @@ -67,7 +67,7 @@ and more text.

Next paragraph with a span and a word-thatincludesaspanright?

\section[directionality]{Directionality}
\section[title={Directionality},reference={directionality}]

Some text and

Expand All @@ -86,7 +86,7 @@ and a ltr div. with a {\righttoleft rtl span}.
Next paragraph with a {\righttoleft rtl span} and a
word-that-includesa{\lefttoright ltrspan}right?

\section[languages]{Languages}
\section[title={Languages},reference={languages}]

Some text and

Expand All @@ -102,7 +102,7 @@ word-that-includesa\start\language[de-ch]Swiss German span\stop right?

Some \start\language[es]Spanish text\stop .

\section[combined]{Combined}
\section[title={Combined},reference={combined}]

Some text and

Expand Down

0 comments on commit e9eb750

Please sign in to comment.