1 # -*- coding: utf-8 -*-
4 require 'ldblogwriter/entry_manager.rb'
7 # parserは、pukiwikiparser.rbを参考にしています。
8 # http://jp.rubyist.net/magazine/?0010-CodeReview
12 # #プラグイン名(arg1, arg2...)
19 def initialize(conf, plugin, service = nil)
25 def get_entry(src_text)
26 lines = src_text.rstrip.split(/\r?\n/)
27 first_line = lines.shift
29 first_line.gsub!(/^<(.*)>\s+/) do |str|
34 src_text = lines.join("\n")
35 @entry = BlogEntry.new(title, category)
36 if @conf.convert_to_html == true
37 # src_text = check_image_file(filename, src_text)
38 content = to_html(src_text)
39 # if @conf.html_directory != nil
40 # save_html_file(@conf.html_directory, File.basename(filename), content)
45 @entry.content = content
50 str.gsub!(/&/, '&')
51 str.gsub!(/"/, '"')
52 str.gsub!(/</, '<')
53 str.gsub!(/>/, '>')
57 def to_html(src, entry = nil)
62 lines = src.rstrip.split(/\r?\n/).map {|line| line.chomp}
71 when /\A#trackback\(.*\)/
72 buf.push parse_trackback(lines.shift)
74 buf.push parse_img(lines.shift)
76 buf.concat parse_pre(take_block(lines, /\A\s/))
78 buf.concat parse_quote(take_block(lines, /\A>/))
80 buf.concat parse_list('ul', take_block(lines, /\A-/))
82 buf.concat parse_list('ol', take_block(lines, /\A\+/))
84 buf.push parse_plugin(lines.shift)
87 buf.concat parse_p(take_block(lines, /\A(?![*\s>:\-\+]|----|\z)/))
96 def take_block(lines, marker)
99 break unless marker =~ lines.first
100 buf.push lines.shift.sub(marker, '')
105 def syntax_highlight(lines, lang)
106 require 'syntax/convertors/html'
107 convertor = Syntax::Convertors::HTML.for_syntax lang
108 ["<div class=\"ruby\">" + convertor.convert(lines.join("\n")) + "</div>\n"]
113 if lines.first =~ /\Ahighlight\((.*)\)/
115 syntax_highlight(lines, $1)
117 ["<pre>", lines.map {|line| escape_html(line) }.join("\n"),
122 def parse_quote(lines)
123 [ "<blockquote><p>", lines.join("\n"), "</p></blockquote>"]
126 def parse_list(type, lines)
127 marker = ((type == 'ul') ? /\A-/ : /\A\+/)
128 parse_list0(type, lines, marker)
131 def parse_list0(type, lines, marker)
135 if marker =~ lines.first
136 buf.concat parse_list0(type, take_block(lines, marker), marker)
138 buf.push closeli if closeli; closeli = '</li>'
139 buf.push "<li>#{parse_inline(lines.shift)}"
142 buf.push closeli if closeli; closeli = '</li>'
143 buf.push "</#{type}>"
147 def parse_plugin(line)
148 eval_string = line.gsub(/\A#/, "")
149 # regist post process action
150 post_method_name = eval_string[/\A\w+/]+"_post"
151 if @plugin.respond_to?(post_method_name)
152 @plugin.post_process_list.push(eval_string.gsub(/\A(\w+)/) { $1+"_post" })
155 @plugin.eval_src(eval_string)
158 def get_small_img_uri(img_uri)
162 uri = URI.parse(img_uri)
163 new_path = uri.path.gsub(/\.(\w+)$/, '-s.\1')
168 def get_img_html(img_uri, title)
170 small_img_uri = get_small_img_uri(img_uri)
171 result += "<a href=\"#{img_uri}\" target=\"_blank\">"
172 result += "<img src=\"#{small_img_uri}\" alt=\"#{title}\" "
173 result += "hspace=\"5\" class=\"pict\" align=\"left\" />"
178 def parse_trackback(line)
180 line.scan(/\#trackback\((.*)\).*/) do |url|
181 @entry.trackback_url_array += url
190 if line =~ /\A#img\((.*)\).*/
192 img_str.gsub!(/\s+/, "")
194 (img_path, img_title) = img_str.split(",")
196 img_title = File.basename(img_path)
198 img_manager = LDBlogWriter::EntryManager.new(@conf.upload_uri_file)
199 if img_manager.has_entry?(File.basename(img_path)) == false
201 img_uri = @service.post_image(img_path, img_title)
205 img_manager.save_edit_uri(File.basename(img_path), img_uri)
207 img_uri = img_manager.get_edit_uri(File.basename(img_path))
209 buf.push(get_img_html(img_uri, img_title))
215 lines.map {|line| parse_inline(line) }
218 def a_href(uri, label, cssclass)
219 if @conf.auto_trackback == true
221 if f.content_type =~ /^image/
222 return get_img_html(uri, label)
223 elsif f.content_type != "text/html"
228 contents.scan(%r|<rdf:Description\s+([^>]+)>|) do |attr|
229 attr[0].scan(%r|\s+([^=]+)="([^\"]+)"|) do |key, value|
230 trackback_ping << value if key == 'trackback:ping'
234 @entry.trackback_url_array += trackback_ping
238 %Q[<a class="#{cssclass}" href="#{escape_html(uri)}">#{escape_html(label)}</a>]
241 def parse_inline(str)
243 ([&<>"]) # $1: HTML escape characters
244 | \[\[(.+?):\s*(https?://\S+)\s*\]\] # $2: label, $3: URI
245 | (#{URI.regexp('http')}) # $5...: URI autolink
247 str.gsub(@inline_re) {
249 when htmlchar = $1 then escape_html(htmlchar)
250 when bracket = $2 then a_href($3, bracket, 'outlink')
251 # when pagename = $4 then "not support $3" #a_href(page_uri(pagename), pagename, 'pagelink')
252 when uri = $4 then a_href(uri, uri, 'outlink')
254 raise 'must not happen'