git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #2419 from mb21/bidi

Support bidirectional text output with XeLaTeX, ConTeXt and HTML

commitfdfc961284a71f634e8513d76f69f6d0add0b29b authorJohn MacFarlane <jgm@berkeley.edu> dateSat 26 Sep 2015, 17:06 (11 years ago) parent72bade07b0c1e0
3 files changed+59−13
README+19 src/Text/Pandoc/Writers/ConTeXt.hs+24−9 src/Text/Pandoc/Writers/LaTeX.hs+16−4
README+19
@@ -959,6 +959,25 @@ as `title`, `author`, and `date`) as well as the following:
959959 Currently only used by XeTeX through the generated
960960 `polyglossia-otherlangs` variable.
961961
962+`dir`
963+: the base direction of the document, either `rtl` (right-to-left)
964+ or `ltr` (left-to-right).
965+
966+ For bidirectional documents, native pandoc `span`s and `div`s
967+ with the `dir` attribute (value `rtl` or `ltr`) can be used to
968+ override the base direction in some output formats.
969+ This may not always be necessary if the final renderer
970+ (e.g. the browser, when generating HTML) supports the
971+ [Unicode Bidirectional Algorithm][uba-basics].
972+
973+ When using LaTeX for bidi documents, only the XeLaTeX engine
974+ is fully supported (see `--latex-engine`).
975+ Setting the `dir` variable enables bidirectional text
976+ handling in LaTeX and ConTeXt, thus even if the base direction
977+ is the default left-to-right, you should set `dir: ltr` for
978+ documents that also contain some right-to-left script.
979+
980+
962981`slidy-url`
963982: base URL for Slidy documents (defaults to
964983 `http://www.w3.org/Talks/Tools/Slidy2`)
src/Text/Pandoc/Writers/ConTeXt.hs+24−9
@@ -83,13 +83,18 @@ pandocToConTeXt options (Pandoc meta blocks) = do
8383 $ metadata
8484 let context' = defField "context-lang" (maybe "" (fromBcp47 . splitBy (=='-')) $
8585 getField "lang" context)
86− context
86+ $ defField "context-dir" (toContextDir $ getField "dir" context)
87+ $ context
8788 return $ if writerStandalone options
8889 then renderTemplate' (writerTemplate options) context'
8990 else main
9091
91−-- escape things as needed for ConTeXt
92+toContextDir :: Maybe String -> String
93+toContextDir (Just "rtl") = "r2l"
94+toContextDir (Just "ltr") = "l2r"
95+toContextDir _ = ""
9296
97+-- | escape things as needed for ConTeXt
9398escapeCharForConTeXt :: WriterOptions -> Char -> String
9499escapeCharForConTeXt opts ch =
95100 let ligatures = writerTeXLigatures opts in
@@ -151,13 +156,18 @@ blockToConTeXt (CodeBlock _ str) =
151156 -- blankline because \stoptyping can't have anything after it, inc. '}'
152157blockToConTeXt (RawBlock "context" str) = return $ text str <> blankline
153158blockToConTeXt (RawBlock _ _ ) = return empty
154−blockToConTeXt (Div (ident,_,_) bs) = do
159+blockToConTeXt (Div (ident,_,kvs) bs) = do
155160 contents <- blockListToConTeXt bs
156− if null ident
157− then return contents
158− else return $
159− ("\\reference" <> brackets (text $ toLabel ident) <> braces empty <>
160− "%") $$ contents
161+ let contents' = if null ident
162+ then contents
163+ else ("\\reference" <> brackets (text $ toLabel ident) <>
164+ braces empty <> "%") $$ contents
165+ let align dir = blankline <> "\\startalignment[" <> dir <> "]"
166+ $$ contents' $$ "\\stopalignment" <> blankline
167+ return $ case lookup "dir" kvs of
168+ Just "rtl" -> align "righttoleft"
169+ Just "ltr" -> align "lefttoright"
170+ _ -> contents'
161171blockToConTeXt (BulletList lst) = do
162172 contents <- mapM listItemToConTeXt lst
163173 return $ ("\\startitemize" <> if isTightList lst
@@ -335,7 +345,12 @@ inlineToConTeXt (Note contents) = do
335345 then text "\\footnote{" <> nest 2 contents' <> char '}'
336346 else text "\\startbuffer " <> nest 2 contents' <>
337347 text "\\stopbuffer\\footnote{\\getbuffer}"
338−inlineToConTeXt (Span _ ils) = inlineListToConTeXt ils
348+inlineToConTeXt (Span (_,_,kvs) ils) = do
349+ contents <- inlineListToConTeXt ils
350+ return $ case lookup "dir" kvs of
351+ Just "rtl" -> braces $ "\\righttoleft " <> contents
352+ Just "ltr" -> braces $ "\\lefttoright " <> contents
353+ _ -> contents
339354
340355-- | Craft the section header, inserting the secton reference, if supplied.
341356sectionHeader :: Attr
src/Text/Pandoc/Writers/LaTeX.hs+16−4
@@ -187,6 +187,9 @@ pandocToLaTeX options (Pandoc meta blocks) = do
187187 $ defField "polyglossia-otherlangs"
188188 (maybe [] (map $ fst . toPolyglossia . splitBy (=='-')) $
189189 getField "otherlangs" context)
190+ $ defField "latex-dir-rtl" (case (getField "dir" context)::Maybe String of
191+ Just "rtl" -> True
192+ _ -> False)
190193 $ context
191194 return $ if writerStandalone options
192195 then renderTemplate' template context'
@@ -323,14 +326,19 @@ isLineBreakOrSpace _ = False
323326blockToLaTeX :: Block -- ^ Block to convert
324327 -> State WriterState Doc
325328blockToLaTeX Null = return empty
326−blockToLaTeX (Div (identifier,classes,_) bs) = do
329+blockToLaTeX (Div (identifier,classes,kvs) bs) = do
327330 beamer <- writerBeamer `fmap` gets stOptions
328331 ref <- toLabel identifier
329332 let linkAnchor = if null identifier
330333 then empty
331334 else "\\hyperdef{}" <> braces (text ref) <>
332335 braces ("\\label" <> braces (text ref))
333− contents <- blockListToLaTeX bs
336+ contents' <- blockListToLaTeX bs
337+ let align dir = inCmd "begin" dir $$ contents' $$ inCmd "end" dir
338+ let contents = case lookup "dir" kvs of
339+ Just "rtl" -> align "RTL"
340+ Just "ltr" -> align "LTR"
341+ _ -> contents'
334342 if beamer && "notes" `elem` classes -- speaker notes
335343 then return $ "\\note" <> braces contents
336344 else return (linkAnchor $$ contents)
@@ -727,10 +735,12 @@ isQuoted _ = False
727735-- | Convert inline element to LaTeX
728736inlineToLaTeX :: Inline -- ^ Inline to convert
729737 -> State WriterState Doc
730−inlineToLaTeX (Span (id',classes,_) ils) = do
738+inlineToLaTeX (Span (id',classes,kvs) ils) = do
731739 let noEmph = "csl-no-emph" `elem` classes
732740 let noStrong = "csl-no-strong" `elem` classes
733741 let noSmallCaps = "csl-no-smallcaps" `elem` classes
742+ let rtl = ("dir","rtl") `elem` kvs
743+ let ltr = ("dir","ltr") `elem` kvs
734744 ref <- toLabel id'
735745 let linkAnchor = if null id'
736746 then empty
@@ -740,7 +750,9 @@ inlineToLaTeX (Span (id',classes,_) ils) = do
740750 ((if noEmph then inCmd "textup" else id) .
741751 (if noStrong then inCmd "textnormal" else id) .
742752 (if noSmallCaps then inCmd "textnormal" else id) .
743− (if not (noEmph || noStrong || noSmallCaps)
753+ (if rtl then inCmd "RL" else id) .
754+ (if ltr then inCmd "LR" else id) .
755+ (if not (noEmph || noStrong || noSmallCaps || rtl || ltr)
744756 then braces
745757 else id)) `fmap` inlineListToLaTeX ils
746758inlineToLaTeX (Emph lst) =