Skip to content

Instantly share code, notes, and snippets.

@chipotle
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save chipotle/9588277 to your computer and use it in GitHub Desktop.

Select an option

Save chipotle/9588277 to your computer and use it in GitHub Desktop.

Revisions

  1. chipotle revised this gist Mar 16, 2014. 1 changed file with 5 additions and 11 deletions.
    16 changes: 5 additions & 11 deletions bbcode.lua
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ function LineBreak()
    end

    function Emph(s)
    return "[em]" .. s .. "[/em]"
    return "[i]" .. s .. "[/i]"
    end

    function Strong(s)
    @@ -55,13 +55,7 @@ function Strikeout(s)
    end

    function Link(s, src, tit)
    local ret = '[url'
    if s then
    ret = ret .. '=' .. src
    else
    s = src
    end
    ret = ret .. "]" .. s .. "[/url]"
    return '[url=' .. src .. ']' .. s .. '[/url]'
    end

    function Image(s, src, tit)
    @@ -98,11 +92,11 @@ function Header(lev, s, attr)
    end

    function BlockQuote(s)
    return "[quote]\n" .. s .. "\n[/quote]"
    return "[quote]" .. s .. "[/quote]"
    end

    function HorizontalRule()
    return "--------------------------------------------------------------------------------"
    return "+ + +"
    end

    function CodeBlock(s, attr)
    @@ -167,4 +161,4 @@ meta.__index =
    io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
    return function() return "" end
    end
    setmetatable(_G, meta)
    setmetatable(_G, meta)
  2. @lilydjwg lilydjwg revised this gist Jun 19, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions 2bbcode
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    #!/bin/bash

    format=${1:-markdown_github}

    exec pandoc -f $format -t ~/bin/bbcode.lua
  3. @lilydjwg lilydjwg revised this gist Jun 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bbcode.lua
    Original file line number Diff line number Diff line change
    @@ -94,7 +94,7 @@ end

    -- lev is an integer, the header level.
    function Header(lev, s, attr)
    return "[b]" .. s .. "[/b]"
    return "[h]" .. s .. "[/h]"
    end

    function BlockQuote(s)
  4. @lilydjwg lilydjwg created this gist Jun 19, 2013.
    170 changes: 170 additions & 0 deletions bbcode.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,170 @@
    -- Invoke with: pandoc -t sample.lua

    -- Blocksep is used to separate block elements.
    function Blocksep()
    return "\n\n"
    end

    -- This function is called once for the whole document. Parameters:
    -- body, title, date are strings; authors is an array of strings;
    -- variables is a table. One could use some kind of templating
    -- system here; this just gives you a simple standalone HTML file.
    function Doc(body, title, authors, date, variables)
    return body .. '\n'
    end

    -- The functions that follow render corresponding pandoc elements.
    -- s is always a string, attr is always a table of attributes, and
    -- items is always an array of strings (the items in a list).
    -- Comments indicate the types of other variables.

    function Str(s)
    return s
    end

    function Space()
    return " "
    end

    function LineBreak()
    return "\n"
    end

    function Emph(s)
    return "[em]" .. s .. "[/em]"
    end

    function Strong(s)
    return "[b]" .. s .. "[/b]"
    end

    function Subscript(s)
    error("Subscript isn't supported")
    end

    function Superscript(s)
    error("Superscript isn't supported")
    end

    function SmallCaps(s)
    error("SmallCaps isn't supported")
    end

    function Strikeout(s)
    return '[del]' .. s .. '[/del]'
    end

    function Link(s, src, tit)
    local ret = '[url'
    if s then
    ret = ret .. '=' .. src
    else
    s = src
    end
    ret = ret .. "]" .. s .. "[/url]"
    end

    function Image(s, src, tit)
    return "[img=" .. tit .. "]" .. src .. "[/img]"
    end

    function Code(s, attr)
    return "[u]" .. s .. "[/u]"
    end

    function InlineMath(s)
    error("InlineMath isn't supported")
    end

    function DisplayMath(s)
    error("DisplayMath isn't supported")
    end

    function Note(s)
    error("Note isn't supported")
    end

    function Plain(s)
    return s
    end

    function Para(s)
    return s
    end

    -- lev is an integer, the header level.
    function Header(lev, s, attr)
    return "[b]" .. s .. "[/b]"
    end

    function BlockQuote(s)
    return "[quote]\n" .. s .. "\n[/quote]"
    end

    function HorizontalRule()
    return "--------------------------------------------------------------------------------"
    end

    function CodeBlock(s, attr)
    return "[code]\n" .. s .. '\n[/code]'
    end

    function BulletList(items)
    local buffer = {}
    for _, item in ipairs(items) do
    table.insert(buffer, "[*]" .. item)
    end
    return "[list]\n" .. table.concat(buffer, "\n") .. "\n[/list]"
    end

    function OrderedList(items)
    local buffer = {}
    for _, item in ipairs(items) do
    table.insert(buffer, "[*]" .. item)
    end
    return "[list=1]\n" .. table.concat(buffer, "\n") .. "\n[/list]"
    end

    -- Revisit association list STackValue instance.
    function DefinitionList(items)
    local buffer = {}
    for _, item in pairs(items) do
    for k, v in pairs(item) do
    table.insert(buffer, "[b]" .. k .. "[/b]:\n" ..
    table.concat(v, "\n"))
    end
    end
    return table.concat(buffer, "\n")
    end

    -- Convert pandoc alignment to something HTML can use.
    -- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
    function html_align(align)
    if align == 'AlignLeft' then
    return 'left'
    elseif align == 'AlignRight' then
    return 'right'
    elseif align == 'AlignCenter' then
    return 'center'
    else
    return 'left'
    end
    end

    -- Caption is a string, aligns is an array of strings,
    -- widths is an array of floats, headers is an array of
    -- strings, rows is an array of arrays of strings.
    function Table(caption, aligns, widths, headers, rows)
    error("Table isn't supported")
    end

    -- The following code will produce runtime warnings when you haven't defined
    -- all of the functions you need for the custom writer, so it's useful
    -- to include when you're working on a writer.
    local meta = {}
    meta.__index =
    function(_, key)
    io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
    return function() return "" end
    end
    setmetatable(_G, meta)