require 'wiki/vikiwikistyle'
module Wiki
class RDLikeWikiStyle < VikiWikiStyle
#===================================================================
# Format Rule
#===================================================================
#
FM_PRE_S = '...'
FM_PRE_E = '...'
FM_SOFTPRE_S = '-...'
FM_SOFTPRE_E = '-...'
RE_PRE_S = /^\.{3}$/
RE_PRE_E = /^\.{3}$/
RE_SOFTPRE_S = /^-\.{3}$/
RE_SOFTPRE_E = /^-\.{3}$/
# to
FM_H2 = '=%s'
FM_H3 = '==%s'
FM_H4 = '===%s'
FM_H5 = '====%s'
FM_H6 = '+%s'
RE_H2 = /^=([^=].*)$/
RE_H3 = /^==([^=].*)$/
RE_H4 = /^===([^=].*)$/
RE_H5 = /^====([^=].*)$/
RE_H6 = /^\+([^\+].*)$/
#
FM_HR = '----'
RE_HR = /^----+/
#
FM_DIV_S = '<<<%s'
FM_DIV_E = '>>>'
RE_DIV_S = /^<<<([A-Za-z]\w*)?$/
RE_DIV_E = /^>>>$/
#
FM_SPAN = '@{%s %s}'
RE_SPAN = /^\@\{(#{TX_IDENT})\s+([^}]*)\}/
#
FM_UL = '*%s'
RE_UL = /^\*\s*(.*)/
#
FM_OL = '(0)%s'
RE_OL = /^\(\d+\)\s*(.*)/
#
FM_DL = ":%s\n %s"
FM_DL2 = ":%s\n %s"
RE_DT = /^(?:\:|---)([^:-].*)$/
RE_DD = /^\s{2,}(.*)$/
#
FM_BLOCKQUOTE = '>%s'
RE_BLOCKQUOTE = /^>(.+)$/
#
KW_TABLE = '||'
RE_TABLE = /^\|\|(.+)$/
#
KW_TABLE_N = '|'
RE_TABLE_N = /^\|([^\|].*)$/
#
FM_TD_LEFT = '%s '
FM_TD_CENTER = ' %s '
FM_TD_RIGHT = ' %s'
RE_TD_LEFT = /^(.+)\s$/
RE_TD_CENTER = /^\s(.+)\s$/
RE_TD_RIGHT = /^\s(.+)$/
#
FM_COMMENT = '//%s'
RE_COMMENT = /^\/\/(.+)$/
# plugin
FM_PLUGIN = '#%s%s%s'
RE_PLUGIN = /^\#\s*(#{TX_IDENT})(?:\#(\S+))?(?:\s+(.+))?$/
# WikiName and BlacketName
FM_WIKINAME = nil
RE_WIKINAME = nil
FM_BRACKETNAME = '((<%s>))'
RE_BRACKETNAME = /^\(\(<((?!URL::|IMG::)[^\|>]+)>\)\)/
FM_ALIASNAME = '((<%s|%s>))'
RE_ALIASNAME = /^\(\(<([^\|]+)\|((?!URL::|IMG::)[^>]+)>\)\)/
#
FM_EM = "((*%s*))"
RE_EM = /^\(\(\*([^\*]+)\*\)\)/
#
FM_STRONG = "((**%s**))"
RE_STRONG = /^\(\(\*{2}([^\*]+)\*{2}\)\)/
#
FM_DEL = '((=%s=))'
RE_DEL = /^\(\(=([^=]+)=\)\)/
#
FM_TT = '((;%s;))'
RE_TT = /^\(\(;([^;]+);\)\)/
#
FM_B = '((@%s@))'
RE_B = /^\(\(@([^@]+)@\)\)/
#
FM_CODE = '(({%s}))'
RE_CODE = /^\(\({([^}]+)}\)\)/
#
FM_VAR = '((|%s|))'
RE_VAR = /^\(\(\|([^\|]+)\|\)\)/
#
FM_KBD = '((%%%s%%))'
RE_KBD = /^\(\(%([^%]+)%\)\)/
# escape
FM_ESCAPE = "(('%s'))"
RE_ESCAPE = /^\(\('(#{TX_NOPARE_M})'\)\)/
# link and image
FM_A = '((<%s|URL::%s>))'
RE_A = /^\(\(<([^\|>]+)\|URL::([^>]+)>\)\)/
FM_URL = '(())'
RE_URL = /^\(\(]+)>\)\)/
FM_IMG = '((<%s|IMG::%s>))'
RE_IMG = /^\(\(<([^\|>]+)\|IMG::([^>]+)>\)\)/
# plugin (inline)
FM_INPLUGIN = '#{%s%s%s}'
RE_INPLUGIN = /^\#\{(#{TX_IDENT})(?:\#(\S+))?(?:\s+(.+))?\}/
# indent
RE_INDENT = /^(\s*)/
# word
RE_WORD = /^([#{TX_WORD}]+)/
# Plugin
FM_PLUGIN_PRM = " %s"
RE_PLUGIN_PRM = /^(?:\"((?:[^\"\\]|\\.)*)\"|(\S+))/
FM_PLUGIN_HDC = "<<%s"
RE_PLUGIN_HDC = /^<<(#{TX_IDENT})/
FM_PLUGIN_SEP = " "
RE_PLUGIN_SEP = /^\s+/
BLOCK_PARSING_ORDER = [
'plugin', 'div', 'softpre', 'pre', 'blockquote', 'comment',
'h6', 'h5', 'h4', 'h3', 'h2',
'hr', 'ul', 'ol', 'dt', 'table', 'table_n', 'p'
]
INLINE_PARSING_ORDER = [
'url', 'span', 'inplugin', 'em', 'strong', 'tt',
'b', 'code', 'var', 'kbd', 'del',
'escape', 'aliasname', 'bracketname', 'img', 'a'
]
def tt_onparse(root, match)
appendChild_inline(root, 'tt', nil, match[1])
end
def b_onparse(root, match)
appendChild_inline(root, 'b', nil, match[1])
end
def code_onparse(root, match)
appendChild_inline(root, 'code', nil, match[1])
end
def var_onparse(root, match)
appendChild_inline(root, 'var', nil, match[1])
end
def kbd_onparse(root, match)
appendChild_inline(root, 'kbd', nil, match[1])
end
def bracketname_onparse(root, match, _alias=false)
text, wri = get_alias_uri(match[2], match[1])
appendChild_inline(root, 'WikiName', {'wri'=>wri, 'bracket'=>true, 'alias'=>_alias}, text)
end
def aliasname_onparse(root, match)
bracketname_onparse(root, match, true)
end
def a_onparse(root, match)
text, href = get_alias_uri(match[2], match[1])
appendChild_inline(root, 'a', {'href'=>href}, text)
end
def img_onparse(root, match)
alt, src = get_alias_uri(match[2], match[1])
appendChild_inline(root, 'img', {'src'=>src, 'alt'=>alt})
end
def dt_onparse(root, match, line, indent, lines)
root = appendChild_block(root, 'dl', nil, nil, indent, APPEND_IF_NOT_EXIST)
appendChild_block(root, 'dt', nil, match[1], indent, APPEND_CHILD)
if @my::RE_DD === lines.first.gsub(/^\s{#{indent}}/, '') then
lines.shift
appendChild_block(root, 'dd', nil, $1+"\n", indent, APPEND_CHILD)
end
end
def tt_ontext(node, outobj='')
outobj << text_format(@my::FM_TT, generateChild(node))
end
def b_ontext(node, outobj='')
outobj << text_format(@my::FM_B, generateChild(node))
end
def code_ontext(node, outobj='')
outobj << text_format(@my::FM_CODE, generateChild(node))
end
def var_ontext(node, outobj='')
outobj << text_format(@my::FM_VAR, generateChild(node))
end
def kbd_ontext(node, outobj='')
outobj << text_format(@my::FM_KBD, generateChild(node))
end
def wikiname_ontext(node, outobj='')
wri = node.getAttribute('wri'); text = generateChild(node)
_alias = node.getAttribute('alias') == "true"
outobj << (_alias ? text_format(@my::FM_ALIASNAME, text, wri) : text_format(@my::FM_BRACKETNAME, wri))
end
def a_ontext(node, outobj='')
href = node.getAttribute('href')
text = generateChild(node)
outobj << ((href == text) ? text_format(@my::FM_URL, href) : text_format(@my::FM_A, text, href))
end
def img_ontext(node, outobj='')
src = node.getAttribute('src')
alt = node.getAttribute('alt')
outobj << ((src == alt) ? text_format(@my::FM_URL, src) : text_format(@my::FM_IMG, alt, src))
end
end
end
|