您尚未登录。

#1 2013-06-17 22:25:34

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

使用 HTML 书写论坛帖子

让我来测试一个新编写完成的 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)

离线

#2 2013-06-17 23:25:38

wych
论坛版主
注册时间: 2011-11-27
帖子: 103

Re: 使用 HTML 书写论坛帖子

完全木有看懂 (趴


Twitter@wych42

离线

#3 2013-06-17 23:35:48

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

Re: 使用 HTML 书写论坛帖子

wych 说:

完全木有看懂 (趴

嘻嘻,Perl 代码是只写的——木有读权限哦哦~

离线

#4 2013-06-18 11:02:45

cjxgm
忙,却还想养猫?
所在地: 杭州
注册时间: 2011-08-19
帖子: 414
个人网站

Re: 使用 HTML 书写论坛帖子

LZ可以尝试 CPAN module: Parse::RecDescent (tutorial)

离线

#5 2013-06-18 12:26:24

mapleray
awesome小白一个
注册时间: 2012-12-02
帖子: 174
个人网站

Re: 使用 HTML 书写论坛帖子

是转换用的?没看懂~~弱爆了

离线

#6 2013-06-18 13:56:09

cuihao
所在地: USTC, Hefei
注册时间: 2011-08-19
帖子: 1,222
个人网站

Re: 使用 HTML 书写论坛帖子

不明觉厉


Site: CVHC.CC   Twitter: @cuihaoleo   Org: LUG@USTC
AD:  ~欢迎参与志愿计算~

离线

#7 2013-06-18 14:36:13

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

Re: 使用 HTML 书写论坛帖子

mapleray 说:

是转换用的?没看懂~~弱爆了

是的。就是一堆正则替换啦~

离线

#8 2013-06-19 13:56:22

felixonmars
Arch Linux Developer
所在地: Wuhan, Hubei, China
注册时间: 2011-08-21
帖子: 148
个人网站

Re: 使用 HTML 书写论坛帖子

赞, 虽然我不用 perl (


Felix Yan
Twitter: @felixonmars
Web: http://felixc.at
tongue

离线

#9 2013-06-19 14:12:39

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

Re: 使用 HTML 书写论坛帖子

另一个转换到 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 取得。

离线

#10 2013-06-19 14:14:56

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

Re: 使用 HTML 书写论坛帖子

忘记写使用方式了……以下是我的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)

离线

#11 2013-06-19 20:55:15

reverland
root
注册时间: 2012-02-04
帖子: 356
个人网站

Re: 使用 HTML 书写论坛帖子

……bbcode


>>>>>>>>>jekyll博客>>>>>>>>>>
<<<<<<<<<更残念的vimwiki<<<<<<<<<
本人vim控,偏偏喜欢lisp

离线

#12 2014-01-19 10:38:33

sandylaw
会员
注册时间: 2012-11-03
帖子: 20

Re: 使用 HTML 书写论坛帖子

论坛为什么不支持markdown呢?

离线

#13 2014-01-19 15:17:03

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,384
个人网站

Re: 使用 HTML 书写论坛帖子

sandylaw 说:

论坛为什么不支持markdown呢?

因为 markdown 会妨碍编辑,比如随便写点带下划线的函数名之类的就出问题。

离线

页脚