Commit 76ae9e07 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

formatting doc to be easier to convert to wiki format

parent fcef9938
This diff is collapsed.
...@@ -20,4 +20,5 @@ end ...@@ -20,4 +20,5 @@ end
group :docs do group :docs do
gem 'github-markup' gem 'github-markup'
gem 'RedCloth' gem 'RedCloth'
gem 'nokogiri'
end end
...@@ -28,6 +28,7 @@ GEM ...@@ -28,6 +28,7 @@ GEM
linecache (0.43) linecache (0.43)
linecache19 (0.5.11) linecache19 (0.5.11)
ruby_core_source (>= 0.1.4) ruby_core_source (>= 0.1.4)
nokogiri (1.5.0)
open4 (1.0.1) open4 (1.0.1)
rack (1.3.2) rack (1.3.2)
rake (0.8.7) rake (0.8.7)
...@@ -71,6 +72,7 @@ DEPENDENCIES ...@@ -71,6 +72,7 @@ DEPENDENCIES
github-markup github-markup
jasmine (= 1.0.2.1) jasmine (= 1.0.2.1)
json (= 1.4.3) json (= 1.4.3)
nokogiri
rake rake
ruby-debug ruby-debug
ruby-debug19 ruby-debug19
...@@ -17,6 +17,86 @@ namespace :docs do ...@@ -17,6 +17,86 @@ namespace :docs do
puts "Preview rendered to #{output}" puts "Preview rendered to #{output}"
end end
end end
desc "Convert docs to Nginx wiki format."
task :convert_to_wiki do
require 'redcloth'
require 'nokogiri'
file = "#{base_dir}/README.textile"
puts file
filename = File.basename(file)
readme = File.read(file)
File.open("#{base_dir}/misc/#{filename}.html", 'w') {|f| f.write(RedCloth.new(readme).to_html) }
output = "#{base_dir}/misc/#{filename}.wiki"
File.open(output, 'w') {|f| f.write(convert_to_wiki_syntax(readme)) }
puts "Preview rendered to #{output}"
end
def convert_to_wiki_syntax(text)
doc = Nokogiri::HTML(RedCloth.new(text).to_html)
convert_elements(doc.children.to_a)
end
def convert_elements(nodes)
result = ""
nodes.each do |node|
if node.element? && !node.text?
childrens = node.children.to_a
unless childrens.empty?
result += convert_element(convert_elements(childrens), node)
end
elsif node.text?
result += node.text
end
end
result
end
def convert_element(text, node)
tag = node.name
text ||= ""
case tag
when "strong"
"'''#{text}'''"
when "b"
"'''#{text}'''"
when "em"
"''#{text}''"
when "h1"
"\n= #{text} ="
when "h2"
"\n== #{text} =="
when "h3"
"\n=== #{text} ==="
when "h4"
"\n==== #{text} ===="
when "h5"
"\n===== #{text} ====="
when "h6"
"\n====== #{text} ======"
when "p"
"\n#{text}"
when "a"
if node.attributes['href'].value.start_with?("#")
"[[#{node.attributes['href'].value}|#{text}]]"
else
"[#{node.attributes['href'].value} #{text}]"
end
when "html"
text
when "body"
text
when "span"
text
else
"<#{tag}>#{text}</#{tag}>"
end
end
end end
desc "Run all tests." desc "Run all tests."
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment