页次: 1
让我来测试一个新编写完成的 Perl 脚本啦~
#!/usr/bin/perl -w
local $/;
$_ = <STDIN>;
s/<pre(.*?)>(.*?)<\/pre>/\[cod]$2\[\/cod]/sgi;
s/<code(.*?)>(.*?)<\/code>/\[u]$2\[\/u]/sgi;
s/<b>(.*?)<\/b>/\[b]$1\[\/b]/gi;
s/<i>(.*?)<\/i>/\[i]$1\[\/i]/gi;
s/<u>(.*?)<\/u>/\[u]$1\[\/u]/gi;
s/<em>(.*?)<\/em>/\[b]$1\[\/b]/gi;
s/<strong>(.*?)<\/strong>/\[b]$1\[\/b]/gi;
s/<font color="(.*?)">(.*?)<\/font>/\[color=$1]$2\[\/color]/sgi;
s/<font color=(.*?)>(.*?)<\/font>/\[color=$1]$2\[\/color]/sgi;
s/<li(.*?)>(.*?)<\/li>/\[\*]$2/gi;
s/<ul(.*?)>/\[list]/gi;
s/<\/ul>/\[\/list]/gi;
s/<img(.*?)src="(.*?)"(.*?)>/\[img]\/$2\[\/img]/gi;
s/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/\[url=$2]$4\[\/url]/gi;
print;
希望它能好用 :-)
为什么要用 HTML 呢?原因如下:
更常见,因此有更好的编辑器支持,比如 xml.vim 插件。
bbcode 打起来太费小拇指了。
没有 phpbb 那样的按钮可以点 QAQ
已知 bug:
代码中的字符会影响替换
注意:请把脚本中的cod自行替换成code!
未来计划:
使用语法解析器重写,不留死角!
支持从 markdown 转换!
Vim 编辑器的编写支持(因为这不完全是标准 HTML)
离线
离线
完全木有看懂 (趴
嘻嘻,Perl 代码是只写的——木有读权限哦哦~
离线
LZ可以尝试 CPAN module: Parse::RecDescent (tutorial)
Giumo Clanjor (哆啦比猫/兰威举)
Where there is a hacker, there is art. | Dogfooding myself. (C++ / Lua / Perl / Rust)
离线
离线
离线
是转换用的?没看懂~~弱爆了
是的。就是一堆正则替换啦~
离线
离线
另一个转换到 bbcode 的脚本,使用pandoc,因此支持包含 github 版 markdown 和 HTML 在内的多种源格式。
核心代码如下:
-- 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]"
return ret
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 "[h]" .. s .. "[/h]"
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)
BBCode 支持但是本代码未支持的特性列表:
引用时指定作者
插入文本(ins 标签)
字体颜色
字母序号的列表
更新中的代码可以从这里 https://gist.github.com/lilydjwg/5812009 取得。
离线
忘记写使用方式了……以下是我的2bbcode脚本:
#!/bin/bash
format=${1:-markdown_github}
exec pandoc -f $format -t ~/bin/bbcode.lua
需要 git 版本的 pandoc。64 位版本的编译版本可从本站 FTP 的 lilydjwg 目录下获取。
2013年11月18日更新:
现在用 haskell-core 的 haskell-pandoc 包就可以了~~
最近编辑记录 依云 (2013-11-18 00:52:58)
离线
离线
论坛为什么不支持markdown呢?
离线
论坛为什么不支持markdown呢?
因为 markdown 会妨碍编辑,比如随便写点带下划线的函数名之类的就出问题。
离线
页次: 1