Skip to content

Instantly share code, notes, and snippets.

@miwarin
Created April 30, 2014 11:12
Show Gist options
  • Save miwarin/b6fd605a189143f2ddfb to your computer and use it in GitHub Desktop.
Save miwarin/b6fd605a189143f2ddfb to your computer and use it in GitHub Desktop.
fswikiからMarkdown形式への変換をがんばろうとした
#!/usr/bin/ruby
# -*- encoding: utf-8 -*-
require 'uri'
require 'pp'
=begin
FreeStyleWiki 入門2 - A Guide to FreeStyle Wiki powered by FSWiki.com http://koitaro.fswiki.com/wiki/FreeStyleWiki+%C6%FE%CC%E72
Convert markdown to hiki / markdown テキストから hiki 形式に変換するスクリプト(適当) — Gist https://gist.github.com/1956916
Markdown - Wikipedia http://ja.wikipedia.org/wiki/Markdown
Markdownの高度な使用 http://nakama.cc.sophia.ac.jp/help.php?file=advanced_markdown.html
fswiki Markdown
^!!! # 見出し1
^!! ## 見出し2
^! ### 見出し3
^---- 同じ 水平線
^+ 1. 番号付きの箇条書き1
^++ 1. 番号付きの箇条書き2
^+++ 1. 番号付きの箇条書き3
^* * 箇条書き1
^** * 箇条書き2
^*** * 箇条書き3
[text|url] [text](url) 任意のURLへのリンク
^ + ^____ 整形済みテキスト
''text'' *text* 文字の修飾
'''text''' **text** 文字の修飾
^"" >_ 引用
ページへのリンク [[page]] は処理しない。面倒くさすぎる
=end
def main(argv)
hikifile = argv[0]
lines = File.open(hikifile, "r:EUC-JP:UTF-8").readlines
markdown = lines.map {|l|
l.
sub(/^(!!!)/, '#').
sub(/^(!!)/, '##').
sub(/^(!)/, '###').
sub(/^(\+{3})/, ' 1. ').
sub(/^(\+{2})/, ' 1. ').
sub(/^(\+{1})/, '1. ').
sub(/^(\*{3})/, ' * ').
sub(/^(\*{2})/, ' * ').
sub(/^(\*{1})/, '* ').
gsub(/^ /, ' ').
gsub(/'''([^']+)'''/, '**\1**').
gsub(/''([^']+)''/, '*\1*').
gsub(/^""/, '> ').
gsub(/\[([^\]]+)\|([^\]]+)\]/, '[\1](\2)') # [text|url] => [text](url)
}
mdfile = URI.decode(hikifile) + ".md"
File.open(mdfile.encode("UTF-8"), "w:EUC-JP:UTF-8").write(markdown.join)
end
main(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment