babelphish 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/LICENSE +1 -1
- data/README.rdoc +91 -1
- data/Rakefile +17 -31
- data/VERSION +1 -0
- data/babelphish.gemspec +268 -0
- data/bin/babelphish +1 -0
- data/config/website.yml +2 -0
- data/doc/classes/Babelphish.html +206 -0
- data/doc/classes/Babelphish/Exceptions.html +111 -0
- data/doc/classes/Babelphish/Exceptions/GoogleResponseError.html +111 -0
- data/doc/classes/Babelphish/GoogleTranslate.html +565 -0
- data/doc/classes/Babelphish/HtmlTranslator.html +335 -0
- data/doc/classes/Babelphish/Translator.html +311 -0
- data/doc/classes/Babelphish/YmlTranslator.html +402 -0
- data/doc/created.rid +1 -0
- data/doc/files/History_txt.html +110 -0
- data/doc/files/PostInstall_txt.html +108 -0
- data/doc/files/README_rdoc.html +255 -0
- data/doc/files/bin/babelphish.html +109 -0
- data/doc/files/lib/babelphish/exceptions_rb.html +101 -0
- data/doc/files/lib/babelphish/html_translator_rb.html +101 -0
- data/doc/files/lib/babelphish/languages_rb.html +108 -0
- data/doc/files/lib/babelphish/translator_rb.html +101 -0
- data/doc/files/lib/babelphish/yml_translator_rb.html +101 -0
- data/doc/files/lib/babelphish_rb.html +113 -0
- data/doc/files/lib/tasks/babelphish_rake.html +147 -0
- data/doc/fr_class_index.html +33 -0
- data/doc/fr_file_index.html +37 -0
- data/doc/fr_method_index.html +43 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/lib/babelphish.rb +1 -1
- data/lib/babelphish/html_translator.rb +6 -0
- data/lib/babelphish/translator.rb +8 -4
- data/lib/babelphish/yml_translator.rb +23 -14
- data/lib/tasks/babelphish.rake +25 -12
- data/script/txt2html +71 -0
- data/test/html_translations/test.ar.html.erb +4 -0
- data/test/html_translations/test.bg.html.erb +4 -0
- data/test/html_translations/test.ca.html.erb +4 -0
- data/test/html_translations/test.cs.html.erb +4 -0
- data/test/html_translations/test.da.html.erb +4 -0
- data/test/html_translations/test.de.html.erb +4 -0
- data/test/html_translations/test.el.html.erb +4 -0
- data/test/html_translations/test.es.html.erb +4 -0
- data/test/html_translations/test.et.html.erb +4 -0
- data/test/html_translations/test.fa.html.erb +4 -0
- data/test/html_translations/test.fi.html.erb +4 -0
- data/test/html_translations/test.fr.html.erb +4 -0
- data/test/html_translations/test.gl.html.erb +4 -0
- data/test/html_translations/test.hi.html.erb +4 -0
- data/test/html_translations/test.hr.html.erb +4 -0
- data/test/html_translations/test.html.erb +4 -0
- data/test/html_translations/test.hu.html.erb +4 -0
- data/test/html_translations/test.id.html.erb +4 -0
- data/test/html_translations/test.it.html.erb +4 -0
- data/test/html_translations/test.iw.html.erb +4 -0
- data/test/html_translations/test.ja.html.erb +4 -0
- data/test/html_translations/test.ko.html.erb +4 -0
- data/test/html_translations/test.lt.html.erb +4 -0
- data/test/html_translations/test.lv.html.erb +4 -0
- data/test/html_translations/test.mt.html.erb +4 -0
- data/test/html_translations/test.nl.html.erb +4 -0
- data/test/html_translations/test.no.html.erb +4 -0
- data/test/html_translations/test.pl.html.erb +4 -0
- data/test/html_translations/test.pt-PT.html.erb +4 -0
- data/test/html_translations/test.ro.html.erb +4 -0
- data/test/html_translations/test.ru.html.erb +4 -0
- data/test/html_translations/test.sk.html.erb +4 -0
- data/test/html_translations/test.sl.html.erb +4 -0
- data/test/html_translations/test.sq.html.erb +4 -0
- data/test/html_translations/test.sr.html.erb +4 -0
- data/test/html_translations/test.sv.html.erb +4 -0
- data/test/html_translations/test.text.ar.html.erb +5 -0
- data/test/html_translations/test.text.ar.plain.erb +8 -0
- data/test/html_translations/test.text.bg.html.erb +5 -0
- data/test/html_translations/test.text.bg.plain.erb +8 -0
- data/test/html_translations/test.text.ca.html.erb +5 -0
- data/test/html_translations/test.text.ca.plain.erb +8 -0
- data/test/html_translations/test.text.cs.html.erb +5 -0
- data/test/html_translations/test.text.cs.plain.erb +8 -0
- data/test/html_translations/test.text.da.html.erb +5 -0
- data/test/html_translations/test.text.da.plain.erb +8 -0
- data/test/html_translations/test.text.de.html.erb +5 -0
- data/test/html_translations/test.text.de.plain.erb +8 -0
- data/test/html_translations/test.text.el.html.erb +5 -0
- data/test/html_translations/test.text.el.plain.erb +8 -0
- data/test/html_translations/test.text.es.html.erb +5 -0
- data/test/html_translations/test.text.es.plain.erb +8 -0
- data/test/html_translations/test.text.et.html.erb +5 -0
- data/test/html_translations/test.text.et.plain.erb +8 -0
- data/test/html_translations/test.text.fa.html.erb +5 -0
- data/test/html_translations/test.text.fa.plain.erb +8 -0
- data/test/html_translations/test.text.fi.html.erb +5 -0
- data/test/html_translations/test.text.fi.plain.erb +8 -0
- data/test/html_translations/test.text.fr.html.erb +5 -0
- data/test/html_translations/test.text.fr.plain.erb +8 -0
- data/test/html_translations/test.text.gl.html.erb +5 -0
- data/test/html_translations/test.text.gl.plain.erb +8 -0
- data/test/html_translations/test.text.hi.html.erb +5 -0
- data/test/html_translations/test.text.hi.plain.erb +8 -0
- data/test/html_translations/test.text.hr.html.erb +5 -0
- data/test/html_translations/test.text.hr.plain.erb +8 -0
- data/test/html_translations/test.text.html.erb +5 -0
- data/test/html_translations/test.text.hu.html.erb +5 -0
- data/test/html_translations/test.text.hu.plain.erb +8 -0
- data/test/html_translations/test.text.id.html.erb +5 -0
- data/test/html_translations/test.text.id.plain.erb +8 -0
- data/test/html_translations/test.text.it.html.erb +5 -0
- data/test/html_translations/test.text.it.plain.erb +8 -0
- data/test/html_translations/test.text.iw.html.erb +5 -0
- data/test/html_translations/test.text.iw.plain.erb +8 -0
- data/test/html_translations/test.text.ja.html.erb +5 -0
- data/test/html_translations/test.text.ja.plain.erb +8 -0
- data/test/html_translations/test.text.ko.html.erb +5 -0
- data/test/html_translations/test.text.ko.plain.erb +8 -0
- data/test/html_translations/test.text.lt.html.erb +5 -0
- data/test/html_translations/test.text.lt.plain.erb +8 -0
- data/test/html_translations/test.text.lv.html.erb +5 -0
- data/test/html_translations/test.text.lv.plain.erb +8 -0
- data/test/html_translations/test.text.mt.html.erb +5 -0
- data/test/html_translations/test.text.mt.plain.erb +8 -0
- data/test/html_translations/test.text.nl.html.erb +5 -0
- data/test/html_translations/test.text.nl.plain.erb +8 -0
- data/test/html_translations/test.text.no.html.erb +5 -0
- data/test/html_translations/test.text.no.plain.erb +8 -0
- data/test/html_translations/test.text.pl.html.erb +5 -0
- data/test/html_translations/test.text.pl.plain.erb +8 -0
- data/test/html_translations/test.text.plain.erb +8 -0
- data/test/html_translations/test.text.pt-PT.html.erb +5 -0
- data/test/html_translations/test.text.pt-PT.plain.erb +8 -0
- data/test/html_translations/test.text.ro.html.erb +5 -0
- data/test/html_translations/test.text.ro.plain.erb +8 -0
- data/test/html_translations/test.text.ru.html.erb +5 -0
- data/test/html_translations/test.text.ru.plain.erb +8 -0
- data/test/html_translations/test.text.sk.html.erb +5 -0
- data/test/html_translations/test.text.sk.plain.erb +8 -0
- data/test/html_translations/test.text.sl.html.erb +5 -0
- data/test/html_translations/test.text.sl.plain.erb +8 -0
- data/test/html_translations/test.text.sq.html.erb +5 -0
- data/test/html_translations/test.text.sq.plain.erb +8 -0
- data/test/html_translations/test.text.sr.html.erb +5 -0
- data/test/html_translations/test.text.sr.plain.erb +8 -0
- data/test/html_translations/test.text.sv.html.erb +5 -0
- data/test/html_translations/test.text.sv.plain.erb +8 -0
- data/test/html_translations/test.text.th.html.erb +5 -0
- data/test/html_translations/test.text.th.plain.erb +8 -0
- data/test/html_translations/test.text.tl.html.erb +5 -0
- data/test/html_translations/test.text.tl.plain.erb +8 -0
- data/test/html_translations/test.text.tr.html.erb +5 -0
- data/test/html_translations/test.text.tr.plain.erb +8 -0
- data/test/html_translations/test.text.uk.html.erb +5 -0
- data/test/html_translations/test.text.uk.plain.erb +8 -0
- data/test/html_translations/test.text.vi.html.erb +5 -0
- data/test/html_translations/test.text.vi.plain.erb +8 -0
- data/test/html_translations/test.text.zh-CN.html.erb +5 -0
- data/test/html_translations/test.text.zh-CN.plain.erb +8 -0
- data/test/html_translations/test.text.zh-TW.html.erb +5 -0
- data/test/html_translations/test.text.zh-TW.plain.erb +8 -0
- data/test/html_translations/test.text.zh.html.erb +5 -0
- data/test/html_translations/test.text.zh.plain.erb +8 -0
- data/test/html_translations/test.th.html.erb +4 -0
- data/test/html_translations/test.tl.html.erb +4 -0
- data/test/html_translations/test.tr.html.erb +4 -0
- data/test/html_translations/test.uk.html.erb +4 -0
- data/test/html_translations/test.vi.html.erb +4 -0
- data/test/html_translations/test.zh-CN.html.erb +4 -0
- data/test/html_translations/test.zh-TW.html.erb +4 -0
- data/test/html_translations/test.zh.html.erb +4 -0
- data/test/test_yml_translator.rb +3 -2
- data/test/translations/ar.yml +7 -0
- data/test/translations/bg.yml +7 -0
- data/test/translations/ca.yml +7 -0
- data/test/translations/cs.yml +7 -0
- data/test/translations/da.yml +7 -0
- data/test/translations/de.yml +7 -0
- data/test/translations/el.yml +7 -0
- data/test/translations/en.yml +6 -0
- data/test/translations/es.yml +7 -0
- data/test/translations/et.yml +7 -0
- data/test/translations/fa.yml +7 -0
- data/test/translations/fi.yml +7 -0
- data/test/translations/fr.yml +7 -0
- data/test/translations/gl.yml +7 -0
- data/test/translations/hi.yml +7 -0
- data/test/translations/hr.yml +7 -0
- data/test/translations/hu.yml +7 -0
- data/test/translations/id.yml +7 -0
- data/test/translations/it.yml +7 -0
- data/test/translations/iw.yml +7 -0
- data/test/translations/ja.yml +7 -0
- data/test/translations/ko.yml +7 -0
- data/test/translations/lt.yml +7 -0
- data/test/translations/lv.yml +7 -0
- data/test/translations/mt.yml +7 -0
- data/test/translations/nl.yml +7 -0
- data/test/translations/no.yml +8 -0
- data/test/translations/pl.yml +7 -0
- data/test/translations/pt-PT.yml +7 -0
- data/test/translations/ro.yml +7 -0
- data/test/translations/ru.yml +7 -0
- data/test/translations/sk.yml +7 -0
- data/test/translations/sl.yml +7 -0
- data/test/translations/sq.yml +7 -0
- data/test/translations/sr.yml +7 -0
- data/test/translations/sv.yml +7 -0
- data/test/translations/th.yml +7 -0
- data/test/translations/tl.yml +7 -0
- data/test/translations/tr.yml +7 -0
- data/test/translations/uk.yml +7 -0
- data/test/translations/vi.yml +7 -0
- data/test/translations/zh-CN.yml +7 -0
- data/test/translations/zh-TW.yml +7 -0
- data/test/translations/zh.yml +7 -0
- metadata +222 -49
- data/History.txt +0 -3
- data/Manifest.txt +0 -19
- data/PostInstall.txt +0 -2
data/lib/babelphish.rb
CHANGED
|
@@ -46,6 +46,8 @@ module Babelphish
|
|
|
46
46
|
|
|
47
47
|
text = IO.read(source_page)
|
|
48
48
|
|
|
49
|
+
begins_with_html = text['<html>']
|
|
50
|
+
|
|
49
51
|
# Pull out all the code blocks so Google doesn't mess with those
|
|
50
52
|
pattern = /\<\%.+\%\>/
|
|
51
53
|
holder = '{{---}}'
|
|
@@ -72,6 +74,10 @@ module Babelphish
|
|
|
72
74
|
translations.each_key do |locale|
|
|
73
75
|
newline_replacements.each do |r|
|
|
74
76
|
translations[locale].sub!(newline_holder, r)
|
|
77
|
+
if translations[locale]['<html>']
|
|
78
|
+
# Google translate can insert '<html>' at the beginning of the result. Remove it.
|
|
79
|
+
translations[locale]['<html>']= '' unless begins_with_html
|
|
80
|
+
end
|
|
75
81
|
end
|
|
76
82
|
end
|
|
77
83
|
|
|
@@ -44,15 +44,19 @@ module Babelphish
|
|
|
44
44
|
# {"responseData": [{"responseData":{"translatedText":"ciao mondo"},"responseDetails":null,"responseStatus":200},{"responseData":{"translatedText":"Bonjour le Monde"},"responseDetails":null,"responseStatus":200}], "responseDetails": null, "responseStatus": 200}
|
|
45
45
|
#
|
|
46
46
|
def multiple_translate(text, tos, from = 'en', tries = 0)
|
|
47
|
+
return {} if text.strip.empty? # Google doesn't like it when you send them an empty string
|
|
47
48
|
base = Babelphish::GOOGLE_AJAX_URL + 'translate'
|
|
48
49
|
# assemble query params
|
|
49
50
|
params = {
|
|
50
51
|
:q => text,
|
|
51
|
-
:v => 1.0
|
|
52
|
+
:v => 1.0
|
|
52
53
|
}
|
|
53
54
|
query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
|
54
55
|
|
|
55
56
|
tos.each do |to|
|
|
57
|
+
if !Babelphish::GoogleTranslate::LANGUAGES.include?(to)
|
|
58
|
+
raise Exceptions::GoogleResponseError, "#{to} is not a valid Google Translate code. Please be sure language codes are one of: #{Babelphish::GoogleTranslate::LANGUAGES.join(',')}"
|
|
59
|
+
end
|
|
56
60
|
query << "&langpair=" + CGI.escape("#{from}|#{to}")
|
|
57
61
|
end
|
|
58
62
|
|
|
@@ -63,10 +67,10 @@ module Babelphish
|
|
|
63
67
|
results = {}
|
|
64
68
|
json['responseData'].each_with_index do |data, index|
|
|
65
69
|
if data['responseStatus'] == 200
|
|
66
|
-
results[
|
|
70
|
+
results[tos[index]] = data['responseData']['translatedText']
|
|
67
71
|
else
|
|
68
72
|
# retry the single translation
|
|
69
|
-
translate(text,
|
|
73
|
+
translate(text, tos[index], from)
|
|
70
74
|
end
|
|
71
75
|
end
|
|
72
76
|
results
|
|
@@ -75,7 +79,7 @@ module Babelphish
|
|
|
75
79
|
# Try again a few more times
|
|
76
80
|
multiple_translate(text, tos, from, tries+=1)
|
|
77
81
|
else
|
|
78
|
-
raise Exceptions::GoogleResponseError, "A problem occured while translating. #{response}"
|
|
82
|
+
raise Exceptions::GoogleResponseError, "A problem occured while translating. #{response} -- #{response.body} -- From: #{from} -- Text: #{text}"
|
|
79
83
|
end
|
|
80
84
|
end
|
|
81
85
|
end
|
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
module Babelphish
|
|
2
2
|
module YmlTranslator
|
|
3
3
|
|
|
4
|
+
SUBSTITUTION_PLACE_HOLDER = '{{---}}'
|
|
5
|
+
|
|
4
6
|
class << self
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
# Translates the given yml file into the specified languages. Will attempt to auto detect the from langauge
|
|
9
|
+
# using the name of the yml file.
|
|
10
|
+
# yml - Path to the yml file to be translated
|
|
11
|
+
# overwrite - Boolean indicating whether or not existing translations should be overwritten.
|
|
12
|
+
# translate_to - A single language to translate the file into. (Valid values are specified in languages.rb)
|
|
13
|
+
# When this value is nil tos or Babelphish::GoogleTranslate::LANGUAGES is used to determine the languages
|
|
14
|
+
# tos - An array containing the languages to translate the yml file into. If nil or not specified then
|
|
15
|
+
# Babelphish::GoogleTranslate::LANGUAGES is used.
|
|
16
|
+
def translate(yml, overwrite = false, translate_to = nil, tos = nil)
|
|
7
17
|
@yml = yml
|
|
8
18
|
language = File.basename(yml, ".yml")
|
|
9
19
|
if !Babelphish::GoogleTranslate::LANGUAGES.include?(language)
|
|
@@ -15,7 +25,6 @@ module Babelphish
|
|
|
15
25
|
translate_and_write_yml(yml, translate_to, language, overwrite)
|
|
16
26
|
puts "Finished translating #{language} to #{translate_to}"
|
|
17
27
|
else
|
|
18
|
-
tos = Babelphish::GoogleTranslate::LANGUAGES
|
|
19
28
|
translate_and_write_many_yml(yml, tos, language, overwrite)
|
|
20
29
|
end
|
|
21
30
|
end
|
|
@@ -43,14 +52,10 @@ module Babelphish
|
|
|
43
52
|
elsif key == true
|
|
44
53
|
puts "Key #{key} was evaluated as true. Check your yml file and be sure to escape values like yes with 'yes'. ie 'yes': 'Yes'"
|
|
45
54
|
elsif !translate_hash[key].nil?
|
|
46
|
-
|
|
47
|
-
pattern = /\{\{.+\}\}/
|
|
48
|
-
holder = '{{---}}'
|
|
49
|
-
replacements = translate_hash[key].scan(pattern)
|
|
50
|
-
translate_hash[key].gsub!(pattern, holder)
|
|
55
|
+
replacements = parse_substitutions(translate_hash[key])
|
|
51
56
|
translate_hash[key] = Babelphish::Translator.translate(translate_hash[key], to, from)
|
|
52
57
|
replacements.each do |r|
|
|
53
|
-
translate_hash[key].sub!(
|
|
58
|
+
translate_hash[key].sub!(SUBSTITUTION_PLACE_HOLDER, r)
|
|
54
59
|
end
|
|
55
60
|
else
|
|
56
61
|
puts "Key #{key} contains no data"
|
|
@@ -99,15 +104,11 @@ module Babelphish
|
|
|
99
104
|
elsif key == true
|
|
100
105
|
puts "Key #{key} was evaluated as true. Check your yml file and be sure it does not include values like yes: Yes"
|
|
101
106
|
elsif !translate_hash[key].nil?
|
|
102
|
-
|
|
103
|
-
pattern = /\{\{.+\}\}/
|
|
104
|
-
holder = '{{---}}'
|
|
105
|
-
replacements = translate_hash[key].scan(pattern)
|
|
106
|
-
translate_hash[key].gsub!(pattern, holder)
|
|
107
|
+
replacements = parse_substitutions(translate_hash[key])
|
|
107
108
|
translations = Babelphish::Translator.multiple_translate(translate_hash[key], tos, from)
|
|
108
109
|
translations.each_key do |locale|
|
|
109
110
|
replacements.each do |r|
|
|
110
|
-
translations[locale].sub!(
|
|
111
|
+
translations[locale].sub!(SUBSTITUTION_PLACE_HOLDER, r)
|
|
111
112
|
end
|
|
112
113
|
end
|
|
113
114
|
translate_hash[key] = translations
|
|
@@ -118,6 +119,14 @@ module Babelphish
|
|
|
118
119
|
end
|
|
119
120
|
end
|
|
120
121
|
|
|
122
|
+
def parse_substitutions(translate_text)
|
|
123
|
+
# pull out all the string substitutions so that google doesn't translate those
|
|
124
|
+
pattern = /\{\{.+?\}\}/ # non greedy pattern match so that we properly match strings like: "{{name}} on {{application_name}}"
|
|
125
|
+
replacements = translate_text.scan(pattern)
|
|
126
|
+
translate_text.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER)
|
|
127
|
+
replacements
|
|
128
|
+
end
|
|
129
|
+
|
|
121
130
|
end
|
|
122
131
|
end
|
|
123
132
|
end
|
data/lib/tasks/babelphish.rake
CHANGED
|
@@ -7,19 +7,32 @@ task :babelphish do
|
|
|
7
7
|
language = ENV['language']
|
|
8
8
|
overwrite = ENV['overwrite'] == 'yes'
|
|
9
9
|
translate_to = ENV['translate_to'] || nil
|
|
10
|
+
translate_tos = ENV['translate_tos'] || nil
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
STDERR.puts "No source language specified when using 'html' option. Defaulting to 'en'"
|
|
17
|
-
language = 'en'
|
|
18
|
-
end
|
|
19
|
-
STDERR.puts "Translating files in #{html}"
|
|
20
|
-
Babelphish::HtmlTranslator.translate(html, Babelphish::GoogleTranslate::LANGUAGES, language, overwrite)
|
|
21
|
-
else
|
|
22
|
-
STDERR.puts "Please specify the directory where your files live. i.e. babelphish -y ./my/locales or babelphish -h ./some/directory/with/erb_or_html/files"
|
|
12
|
+
begin
|
|
13
|
+
translate_tos = translate_tos.split(',') if translate_tos
|
|
14
|
+
rescue
|
|
15
|
+
STDERR.puts "An error occured while parsing the specified language codes #{translate_tos}. Please be sure to use a comman delimited list. ie es,fr,jp"
|
|
16
|
+
return
|
|
23
17
|
end
|
|
24
18
|
|
|
19
|
+
begin
|
|
20
|
+
if yml
|
|
21
|
+
STDERR.puts "Translating #{yml}"
|
|
22
|
+
translate_tos ||= Babelphish::GoogleTranslate::LANGUAGES
|
|
23
|
+
Babelphish::YmlTranslator.translate(yml, overwrite, translate_to, translate_tos)
|
|
24
|
+
elsif html
|
|
25
|
+
if language.nil?
|
|
26
|
+
STDERR.puts "No source language specified when using 'html' option. Defaulting to 'en'"
|
|
27
|
+
language = 'en'
|
|
28
|
+
end
|
|
29
|
+
STDERR.puts "Translating files in #{html}"
|
|
30
|
+
translate_tos ||= Babelphish::GoogleTranslate::LANGUAGES
|
|
31
|
+
Babelphish::HtmlTranslator.translate(html, translate_tos, language, overwrite)
|
|
32
|
+
else
|
|
33
|
+
STDERR.puts "Please specify the directory where your files live. i.e. babelphish -y ./my/locales or babelphish -h ./some/directory/with/erb_or_html/files"
|
|
34
|
+
end
|
|
35
|
+
rescue Babelphish::Exceptions::GoogleResponseError => ex
|
|
36
|
+
STDERR.puts ex
|
|
37
|
+
end
|
|
25
38
|
end
|
data/script/txt2html
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
load File.dirname(__FILE__) + "/../Rakefile"
|
|
4
|
+
require 'rubyforge'
|
|
5
|
+
require 'redcloth'
|
|
6
|
+
require 'syntax/convertors/html'
|
|
7
|
+
require 'erb'
|
|
8
|
+
|
|
9
|
+
download = "http://rubyforge.org/projects/#{$hoe.rubyforge_name}"
|
|
10
|
+
version = $hoe.version
|
|
11
|
+
|
|
12
|
+
def rubyforge_project_id
|
|
13
|
+
RubyForge.new.configure.autoconfig["group_ids"][$hoe.rubyforge_name]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class Fixnum
|
|
17
|
+
def ordinal
|
|
18
|
+
# teens
|
|
19
|
+
return 'th' if (10..19).include?(self % 100)
|
|
20
|
+
# others
|
|
21
|
+
case self % 10
|
|
22
|
+
when 1: return 'st'
|
|
23
|
+
when 2: return 'nd'
|
|
24
|
+
when 3: return 'rd'
|
|
25
|
+
else return 'th'
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class Time
|
|
31
|
+
def pretty
|
|
32
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def convert_syntax(syntax, source)
|
|
37
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if ARGV.length >= 1
|
|
41
|
+
src, template = ARGV
|
|
42
|
+
template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
|
|
43
|
+
else
|
|
44
|
+
puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
|
|
45
|
+
exit!
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
template = ERB.new(File.open(template).read)
|
|
49
|
+
|
|
50
|
+
title = nil
|
|
51
|
+
body = nil
|
|
52
|
+
File.open(src) do |fsrc|
|
|
53
|
+
title_text = fsrc.readline
|
|
54
|
+
body_text_template = fsrc.read
|
|
55
|
+
body_text = ERB.new(body_text_template).result(binding)
|
|
56
|
+
syntax_items = []
|
|
57
|
+
body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
|
|
58
|
+
ident = syntax_items.length
|
|
59
|
+
element, syntax, source = $1, $2, $3
|
|
60
|
+
syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
|
|
61
|
+
"syntax-temp-#{ident}"
|
|
62
|
+
}
|
|
63
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
|
64
|
+
body = RedCloth.new(body_text).to_html
|
|
65
|
+
body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
|
|
66
|
+
end
|
|
67
|
+
stat = File.stat(src)
|
|
68
|
+
created = stat.ctime
|
|
69
|
+
modified = stat.mtime
|
|
70
|
+
|
|
71
|
+
$stdout << template.result(binding)
|