git.iko.soy/typeable/pandoc
on master help sign in
Added json format for reading and writing.
167eeefJohn MacFarlane
README+4−3
@@ -140,8 +140,8 @@ Options
140=======
140=======
141
141
142`-f` *FORMAT*, `-r` *FORMAT*, `--from=`*FORMAT*, `--read=`*FORMAT*
142`-f` *FORMAT*, `-r` *FORMAT*, `--from=`*FORMAT*, `--read=`*FORMAT*
143−: Specify input format. *FORMAT* can be
143+: Specify input format. *FORMAT* can be `native` (native Haskell),
144− `native` (native Haskell), `markdown` (markdown or plain text),
144+ `json` (JSON version of native AST), `markdown` (markdown),
145 `textile` (Textile), `rst` (reStructuredText), `html` (HTML),
145 `textile` (Textile), `rst` (reStructuredText), `html` (HTML),
146 or `latex` (LaTeX). If `+lhs` is appended to `markdown`, `rst`,
146 or `latex` (LaTeX). If `+lhs` is appended to `markdown`, `rst`,
147 or `latex`, the input will be treated as literate Haskell source:
147 or `latex`, the input will be treated as literate Haskell source:
@@ -150,7 +150,8 @@ Options
150
150
151`-t` *FORMAT*, `-w` *FORMAT*, `--to=`*FORMAT*, `--write=`*FORMAT*
151`-t` *FORMAT*, `-w` *FORMAT*, `--to=`*FORMAT*, `--write=`*FORMAT*
152: Specify output format. *FORMAT* can be `native` (native Haskell),
152: Specify output format. *FORMAT* can be `native` (native Haskell),
153+ `json` (JSON version of native AST), `plain` (plain text),
153− `plain` (plain text), `markdown` (markdown), `rst` (reStructuredText),
154+ `markdown` (markdown), `rst` (reStructuredText),
154 `html` (HTML), `latex` (LaTeX), `context` (ConTeXt), `man` (groff man),
155 `html` (HTML), `latex` (LaTeX), `context` (ConTeXt), `man` (groff man),
155 `mediawiki` (MediaWiki markup), `textile` (Textile), `org` (Emacs
156 `mediawiki` (MediaWiki markup), `textile` (Textile), `org` (Emacs
156 Org-Mode), `texinfo` (GNU Texinfo), `docbook` (DocBook XML),
157 Org-Mode), `texinfo` (GNU Texinfo), `docbook` (DocBook XML),
pandoc.cabal+4−2
@@ -167,7 +167,8 @@ Library
167 HTTP >= 4000.0.5, texmath >= 0.4, xml >= 1.3.5 && < 1.4,
167 HTTP >= 4000.0.5, texmath >= 0.4, xml >= 1.3.5 && < 1.4,
168 random, extensible-exceptions,
168 random, extensible-exceptions,
169 citeproc-hs >= 0.3 && < 0.4,
169 citeproc-hs >= 0.3 && < 0.4,
170− pandoc-types == 1.7.*
170+ pandoc-types == 1.7.*,
171+ json >= 0.4 && < 0.5
171 if impl(ghc >= 6.10)
172 if impl(ghc >= 6.10)
172 Build-depends: base >= 4 && < 5, syb
173 Build-depends: base >= 4 && < 5, syb
173 else
174 else
@@ -238,7 +239,8 @@ Executable pandoc
238 HTTP >= 4000.0.5, texmath, xml >= 1.3.5 && < 1.4,
239 HTTP >= 4000.0.5, texmath, xml >= 1.3.5 && < 1.4,
239 random, extensible-exceptions,
240 random, extensible-exceptions,
240 citeproc-hs >= 0.3 && < 0.4,
241 citeproc-hs >= 0.3 && < 0.4,
241− pandoc-types == 1.7.*
242+ pandoc-types == 1.7.*,
243+ json >= 0.4 && < 0.5
242 if impl(ghc >= 6.10)
244 if impl(ghc >= 6.10)
243 Build-depends: base >= 4 && < 5, syb
245 Build-depends: base >= 4 && < 5, syb
244 else
246 else
src/pandoc.hs+11
@@ -29,6 +29,7 @@ Parses command-line options and calls the appropriate readers and
29writers.
29writers.
30-}
30-}
31module Main where
31module Main where
32+import Text.JSON.Generic (encodeJSON, decodeJSON)
32import Text.Pandoc
33import Text.Pandoc
33import Text.Pandoc.S5 (s5HeaderIncludes)
34import Text.Pandoc.S5 (s5HeaderIncludes)
34import Text.Pandoc.Shared ( tabFilter, ObfuscationMethod (..), readDataFile,
35import Text.Pandoc.Shared ( tabFilter, ObfuscationMethod (..), readDataFile,
@@ -83,6 +84,7 @@ wrapWords c = wrap' c c where
83-- | Association list of formats and readers.
84-- | Association list of formats and readers.
84readers :: [(String, ParserState -> String -> Pandoc)]
85readers :: [(String, ParserState -> String -> Pandoc)]
85readers = [("native" , readPandoc)
86readers = [("native" , readPandoc)
87+ ,("json" , readJSON)
86 ,("markdown" , readMarkdown)
88 ,("markdown" , readMarkdown)
87 ,("markdown+lhs" , readMarkdown)
89 ,("markdown+lhs" , readMarkdown)
88 ,("rst" , readRST)
90 ,("rst" , readRST)
@@ -97,9 +99,18 @@ readers = [("native" , readPandoc)
97readPandoc :: ParserState -> String -> Pandoc
99readPandoc :: ParserState -> String -> Pandoc
98readPandoc _ = read
100readPandoc _ = read
99
101
102+-- | Reader for JSON version of Pandoc AST.
103+readJSON :: ParserState -> String -> Pandoc
104+readJSON _ = decodeJSON
105+
106+-- | Writer for JSON version of Pandoc AST.
107+writeJSON :: WriterOptions -> Pandoc -> String
108+writeJSON _ = encodeJSON
109+
100-- | Association list of formats and writers.
110-- | Association list of formats and writers.
101writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
111writers :: [ ( String, WriterOptions -> Pandoc -> String ) ]
102writers = [("native" , writeNative)
112writers = [("native" , writeNative)
113+ ,("json" , writeJSON)
103 ,("html" , writeHtmlString)
114 ,("html" , writeHtmlString)
104 ,("html+lhs" , writeHtmlString)
115 ,("html+lhs" , writeHtmlString)
105 ,("s5" , writeHtmlString)
116 ,("s5" , writeHtmlString)