NoPaste

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:
  1. function map(xs, f)
  2.     local ys = {}
  3.     for i,x in pairs(xs) do
  4.         ys[i] = f(x)
  5.     end
  6.     return ys
  7. end
  8.  
  9. function startswith(str, sub)
  10.     return string.sub(str, 1, string.len(sub)) == sub
  11. end
  12.  
  13. function processListPlain (p)
  14.     str = pandoc.utils.stringify(p)
  15.     if startswith(str, '[list') then
  16.         return str
  17.     else
  18.         return string.format('\n[*] %s', pandoc.utils.stringify(p:walk(filters)))
  19.     end
  20. end
  21.  
  22. function processBulletList (bl)
  23.     inner = bl:walk {
  24.         BulletList = function (bl) processBulletList(bl.content) end,
  25.         Plain = processListPlain,
  26.     }
  27.     return string.format('[list]\n%s\n[/list]', pandoc.utils.stringify(inner))
  28. end
  29.  
  30. function processOrderedList (bl)
  31.     inner = bl:walk {
  32.         BulletList = function (bl) processOrderedList(bl.content) end,
  33.         Plain = processListPlain,
  34.     }
  35.     return string.format('[list=1]\n%s\n[/list]', pandoc.utils.stringify(inner))
  36. end
  37.  
  38. filters = {
  39.     Header = function (h)
  40.         local headerSizes = {
  41.             [1] = 150, [2] = 130, [3] = 120, [4] = 115, [5] = 110, [6] = 105,
  42.         }
  43.         size = headerSizes[h.level]
  44.         content = pandoc.utils.stringify(h.content)
  45.         return string.format('[size=%d][b]%s[/b][/size]', size, content)
  46.     end,
  47.     Strong = function (s)
  48.         return string.format('[b]%s[/b]', pandoc.utils.stringify(s))
  49.     end,
  50.     Emph = function (e)
  51.         return string.format('[i]%s[/i]', pandoc.utils.stringify(e))
  52.     end,
  53.     Code = function (c)
  54.         return string.format('[tt]%s[/tt]', pandoc.utils.stringify(c))
  55.     end,
  56.     BlockQuote = function (bq)
  57.         str = pandoc.utils.stringify(bq)
  58.         return pandoc.RawBlock('plain', string.format('[quote]\n%s\n[/quote]', str))
  59.     end,
  60.     CodeBlock = function (cb)
  61.         return pandoc.RawBlock('plain', string.format('[code]\n%s\n[/code]', cb.text))
  62.     end,
  63.     Link = function (l)
  64.         content = pandoc.utils.stringify(l.content)
  65.         return string.format('[url=%s]%s[/url]', l.target, content)
  66.     end,
  67.     BulletList = processBulletList,
  68.     OrderedList = processOrderedList,
  69. }
  70.  
  71. function Writer (doc, opts)
  72.     return pandoc.write(doc:walk(filters), 'plain', { wrap_text = 'wrap-none' })
  73. end

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN