bbcode.lua
von paedubucher- SNIPPET_DESC:
- BBCode-Ausgabe mit Pandoc (Lua-Filter)
- SNIPPET_CREATION_TIME:
- 19.11.2023 09:08:02
- SNIPPET_PRUNE_TIME:
- Unendlich
- SNIPPET_TEXT:
-
- function map(xs, f)
- local ys = {}
- for i,x in pairs(xs) do
- ys[i] = f(x)
- end
- return ys
- end
- function startswith(str, sub)
- return string.sub(str, 1, string.len(sub)) == sub
- end
- function processListPlain (p)
- str = pandoc.utils.stringify(p)
- if startswith(str, '[list') then
- return str
- else
- return string.format('\n[*] %s', pandoc.utils.stringify(p:walk(filters)))
- end
- end
- function processBulletList (bl)
- inner = bl:walk {
- BulletList = function (bl) processBulletList(bl.content) end,
- Plain = processListPlain,
- }
- return string.format('[list]\n%s\n[/list]', pandoc.utils.stringify(inner))
- end
- function processOrderedList (bl)
- inner = bl:walk {
- BulletList = function (bl) processOrderedList(bl.content) end,
- Plain = processListPlain,
- }
- return string.format('[list=1]\n%s\n[/list]', pandoc.utils.stringify(inner))
- end
- filters = {
- Header = function (h)
- local headerSizes = {
- [1] = 150, [2] = 130, [3] = 120, [4] = 115, [5] = 110, [6] = 105,
- }
- size = headerSizes[h.level]
- content = pandoc.utils.stringify(h.content)
- return string.format('[size=%d][b]%s[/b][/size]', size, content)
- end,
- Strong = function (s)
- return string.format('[b]%s[/b]', pandoc.utils.stringify(s))
- end,
- Emph = function (e)
- return string.format('[i]%s[/i]', pandoc.utils.stringify(e))
- end,
- Code = function (c)
- return string.format('[tt]%s[/tt]', pandoc.utils.stringify(c))
- end,
- BlockQuote = function (bq)
- str = pandoc.utils.stringify(bq)
- return pandoc.RawBlock('plain', string.format('[quote]\n%s\n[/quote]', str))
- end,
- CodeBlock = function (cb)
- return pandoc.RawBlock('plain', string.format('[code]\n%s\n[/code]', cb.text))
- end,
- Link = function (l)
- content = pandoc.utils.stringify(l.content)
- return string.format('[url=%s]%s[/url]', l.target, content)
- end,
- BulletList = processBulletList,
- OrderedList = processOrderedList,
- }
- function Writer (doc, opts)
- return pandoc.write(doc:walk(filters), 'plain', { wrap_text = 'wrap-none' })
- end
Quellcode
Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN