Skip to content

Instantly share code, notes, and snippets.

@alexklapheke
Last active July 8, 2016 20:52
Show Gist options
  • Save alexklapheke/2ba78f3e953d5a18243d to your computer and use it in GitHub Desktop.
Save alexklapheke/2ba78f3e953d5a18243d to your computer and use it in GitHub Desktop.
Pandoc filter for parallel texts
#!/usr/bin/runhaskell
import Text.Pandoc.JSON
-- Set parallel texts. Use a <div> for each pair of
-- paragraphs and a horizontal rule to divide them:
--
-- <div class="parallel">
-- Longtemps, je me suis couché de bonne heure.
--
-- * * * * *
--
-- For a long time, I used to go to bed early.
-- </div>
--
-- LaTeX output requires the parallel package:
-- <https://ctan.org/pkg/parallel>
main :: IO ()
main = toJSONFilter parallelTranslation
parallelTranslation :: Maybe Format -> Block -> Block
parallelTranslation (Just format) d@(Div (id,classes,namevals) pars) =
case ("parallel" `elem` classes) of
True | format `elem` fmap Format ["html", "html5", "epub", "epub3", "slidy", "slideous", "dzslides", "revealjs", "s5"] ->
Div (id,classes,("style","overflow:hidden;"):namevals)
[Div ("",["parallelLeft"], [("style","width:49%;float:left;")]) left
,Div ("",["parallelRight"],[("style","width:49%;float:right;")]) right]
| format `elem` fmap Format ["latex", "beamer"] ->
Div ("",["parallel"],[]) $
[RawBlock format "\\begin{Parallel}{0.49\\linewidth}{0.49\\linewidth}\\sloppy"] ++
[RawBlock format "\\ParallelLText{"] ++ left ++
[RawBlock format "}\\ParallelRText{"] ++ right ++
[RawBlock format "}\\end{Parallel}"]
| otherwise -> d
where left = takeWhile (/= HorizontalRule) pars
right = tail $ dropWhile (/= HorizontalRule) pars
_ -> d
parallelTranslation _ x = x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment