Class: AugustsFancyBlogPostParser
- Inherits:
-
Object
- Object
- AugustsFancyBlogPostParser
- Defined in:
- lib/augusts_fancy_blog_post_parser.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .escape_html(html) ⇒ Object
- .format_code(code, lang) ⇒ Object
- .parse_body(body) ⇒ Object
- .parse_html_attrs(attrs) ⇒ Object
- .separate_on(str, re, result = []) ⇒ Object
Instance Method Summary collapse
- #display_date ⇒ Object
- #html_date ⇒ Object
- #id ⇒ Object
-
#initialize(cwd, path) ⇒ AugustsFancyBlogPostParser
constructor
A new instance of AugustsFancyBlogPostParser.
- #reload! ⇒ Object
- #timestamp ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(cwd, path) ⇒ AugustsFancyBlogPostParser
Returns a new instance of AugustsFancyBlogPostParser.
63 64 65 66 67 68 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 63 def initialize(cwd, path) @path = path @cwd = cwd @url = "/" + @path[0...-(File.extname(@path).length)] reload! end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 7 def body @body end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 6 def url @url end |
Class Method Details
.escape_html(html) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 9 def self.escape_html(html) html .gsub(/&(?!\w+;)/, '&') .gsub(/</, '<') .gsub(/>/, '>') .gsub(/"/, '"') end |
.format_code(code, lang) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 30 def self.format_code(code, lang) if lang formatted = nil Open3.popen3("pygmentize", "-l", lang, "-f", "html", "-P", "nowrap=true") do |stdin, stdout, stderr, wait_thr| stdin.write(code) stdin.close_write lines = [] until (line = stdout.gets).nil? lines.push(line) end formatted = lines.join("").rstrip end "<code class=\"highlight\">#{formatted}</code>" else "<code>" + escape_html(code) + "</code>" end end |
.parse_body(body) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 70 def self.parse_body(body) chunks = separate_on(body, /\<code(.*?)\>(.*?)\<\/code\>/m) Parallel.map(chunks, :in_threads => chunks.length) do |chunk| case chunk when String chunk when MatchData attrs = parse_html_attrs(chunk[1]) code = chunk[2] format_code(code, attrs["data-lang"]) else raise ArgumentError.new("Unexpected chunk #{chunk.class}.") end end.join("") end |
.parse_html_attrs(attrs) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 17 def self.parse_html_attrs(attrs) if attrs.empty? return {} else Nokogiri::HTML("<div#{attrs}></div>").css("div")[0].attributes.inject({}) do |prev, curr| key = curr[0] value = curr[1] prev[key] = value.value prev end end end |
.separate_on(str, re, result = []) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 51 def self.separate_on(str, re, result = []) match_data = str.match(re) if match_data result.push(match_data.pre_match) result.push(match_data) separate_on(match_data.post_match, re, result) else result.push(str) result end end |
Instance Method Details
#display_date ⇒ Object
121 122 123 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 121 def display_date .strftime("%B %d, %Y") end |
#html_date ⇒ Object
117 118 119 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 117 def html_date .strftime("%Y-%m-%d") end |
#id ⇒ Object
105 106 107 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 105 def id url.gsub("/", ":") end |
#reload! ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 86 def reload! html = File.read(@cwd + "/" + @path) @headers = {} num_header_chars = 0 html.each_line.each do |line| if line =~ /[a-z]+\:./ key, _, value = *line.partition(":") @headers[key] = value.chomp num_header_chars += line.length else break end end @body = self.class.parse_body(html[num_header_chars..-1]) end |
#timestamp ⇒ Object
113 114 115 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 113 def Time.parse(@headers["date"]) end |
#title ⇒ Object
109 110 111 |
# File 'lib/augusts_fancy_blog_post_parser.rb', line 109 def title @headers["title"].strip end |