git.iko.soy/typeable/pandoc
on master help sign in
Update filter code in doc/filters.md...
e23554cJohn MacFarlane
doc/filters.md+21−7
@@ -277,11 +277,14 @@ the file given?
277#!/usr/bin/env runhaskell
277#!/usr/bin/env runhaskell
278-- includes.hs
278-- includes.hs
279import Text.Pandoc.JSON
279import Text.Pandoc.JSON
280+import qualified Data.Text.IO as TIO
281+import qualified Data.Text as T
280
282
281doInclude :: Block -> IO Block
283doInclude :: Block -> IO Block
282doInclude cb@(CodeBlock (id, classes, namevals) contents) =
284doInclude cb@(CodeBlock (id, classes, namevals) contents) =
283 case lookup "include" namevals of
285 case lookup "include" namevals of
284− Just f -> return . (CodeBlock (id, classes, namevals)) =<< readFile f
286+ Just f -> CodeBlock (id, classes, namevals) <$>
287+ TIO.readFile (T.unpack f)
285 Nothing -> return cb
288 Nothing -> return cb
286doInclude x = return x
289doInclude x = return x
287
290
@@ -358,17 +361,23 @@ markdown link with a URL beginning with a hyphen is interpreted as ruby:
358 [はん](-飯)
361 [はん](-飯)
359
362
360~~~ {.haskell}
363~~~ {.haskell}
364+{-# LANGUAGE OverloadedStrings #-}
361-- handleruby.hs
365-- handleruby.hs
362import Text.Pandoc.JSON
366import Text.Pandoc.JSON
363import System.Environment (getArgs)
367import System.Environment (getArgs)
368+import qualified Data.Text as T
364
369
365handleRuby :: Maybe Format -> Inline -> Inline
370handleRuby :: Maybe Format -> Inline -> Inline
366−handleRuby (Just format) (Link _ [Str ruby] ('-':kanji,_))
371+handleRuby (Just format) x@(Link attr [Str ruby] (src,_)) =
372+ case T.uncons src of
373+ Just ('-',kanji)
367− | format == Format "html" = RawInline format
374+ | format == Format "html" -> RawInline format $
368− $ "<ruby>" ++ kanji ++ "<rp>(</rp><rt>" ++ ruby ++ "</rt><rp>)</rp></ruby>"
375+ "<ruby>" <> kanji <> "<rp>(</rp><rt>" <> ruby <>
376+ "</rt><rp>)</rp></ruby>"
369− | format == Format "latex" = RawInline format
377+ | format == Format "latex" -> RawInline format $
370− $ "\\ruby{" ++ kanji ++ "}{" ++ ruby ++ "}"
378+ "\\ruby{" <> kanji <> "}{" <> ruby <> "}"
371− | otherwise = Str ruby
379+ | otherwise -> Str ruby
380+ _ -> x
372handleRuby _ x = x
381handleRuby _ x = x
373
382
374main :: IO ()
383main :: IO ()
@@ -395,6 +404,11 @@ Then run it:
395 ^D
404 ^D
396 \ruby{飯}{はん}
405 \ruby{飯}{はん}
397
406
407+Note: to use this to generate PDFs via LaTeX, you'll need
408+to use `--pdf-engine=xelatex`, specify a `mainfont` that has
409+the Japanese characters (e.g. "Noto Sans CJK TC"), and add
410+`\usepackage{ruby}` to your template or header-includes.
411+
398# Exercises
412# Exercises
399
413
4001. Put all the regular text in a markdown document in ALL CAPS
4141. Put all the regular text in a markdown document in ALL CAPS