aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-11-22 14:58:18 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2021-11-22 14:58:18 -0800
commita039f024f2c0e2d9e6d1ae7710cf96debcbc5e43 (patch)
tree9c1ec6e7f048603547611dc40ac80f9f08463eb3
parent17495bf8eb5f5483d01ebe6a61e1c24eb1f00924 (diff)
downloadpandoc-a039f024f2c0e2d9e6d1ae7710cf96debcbc5e43.tar.gz
Add an example to custom-readers.md.
-rw-r--r--doc/custom-readers.md52
1 files changed, 52 insertions, 0 deletions
diff --git a/doc/custom-readers.md b/doc/custom-readers.md
index bef3ae050..fe032d4b3 100644
--- a/doc/custom-readers.md
+++ b/doc/custom-readers.md
@@ -597,3 +597,55 @@ links](http://www.wikicreole.org), give the link a
[different](internal links) name.
```
+# Example: parsing JSON from an API
+
+This custom reader consumes the JSON output of
+<https://www.reddit.com/r/haskell.json> and produces
+a document containing the current top articles on the
+Haskell subreddit.
+
+It assumes that the `luajson` library is available. (It can be
+installed using `luarocks install luajson`---but be sure you are
+installing it for Lua 5.3, which is the version packaged with
+pandoc.)
+
+
+```lua
+-- consumes the output of https://www.reddit.com/r/haskell.json
+
+local json = require'json' -- luajson must be available
+
+local function read_inlines(raw)
+ local doc = pandoc.read(raw, "commonmark")
+ return pandoc.utils.blocks_to_inlines(doc.blocks)
+end
+
+local function read_blocks(raw)
+ local doc = pandoc.read(raw, "commonmark")
+ return doc.blocks
+end
+
+function Reader(input)
+
+ local parsed = json.decode(input)
+ local blocks = {}
+
+ for _,entry in ipairs(parsed.data.children) do
+ local d = entry.data
+ table.insert(blocks, pandoc.Header(2,
+ pandoc.Link(read_inlines(d.title), d.url)))
+ for _,block in ipairs(read_blocks(d.selftext)) do
+ table.insert(blocks, block)
+ end
+ end
+
+ return pandoc.Pandoc(blocks)
+
+end
+```
+
+Similar code can be used to consume JSON output from other APIs.
+
+Note that the content of the text fields is markdown, so we
+convert it using `pandoc.read()`.
+