Module: Rack::Blogengine::DocumentParser
- Defined in:
- lib/rack/blogengine/document_parser.rb
Overview
Prepares the documents for the response Reads in layout.html and .content file -> merged html Sort documents by date Execute Content Operator after all documents has been parsed in
Class Attribute Summary collapse
-
.content ⇒ Object
private
Returns the value of attribute content.
-
.date ⇒ Object
private
Returns the value of attribute date.
-
.html ⇒ Object
private
Returns the value of attribute html.
-
.layout ⇒ Object
private
Returns the value of attribute layout.
-
.path ⇒ Object
private
Returns the value of attribute path.
-
.target ⇒ Object
Returns the value of attribute target.
-
.title ⇒ Object
private
Returns the value of attribute title.
Class Method Summary collapse
-
.fill_file_contents(layout, title, content, date) ⇒ String
Replace layout placeholder with content from .content file.
-
.generate_highlight_css(target) ⇒ Object
Populates highlight.css with specific highlight css.
-
.get_content_array(content) ⇒ Array
Get Content Array.
-
.get_file_contents(file) ⇒ Object
Get File Contents (path, title, content).
-
.get_highlight_code(content, seperator) ⇒ Hash
Get Highlight Code from Content.
-
.highlight(code, language) ⇒ String
Highlight Code in specific language.
-
.parse_in_documents(target) ⇒ Hash
Parse in .content Documents.
-
.sort(documents) ⇒ Array
Sort documents array by date of each documenthash Should it be sorted in Core or in the operator??.
Class Attribute Details
.content ⇒ Object (private)
Returns the value of attribute content.
17 18 19 |
# File 'lib/rack/blogengine/document_parser.rb', line 17 def content @content end |
.date ⇒ Object (private)
Returns the value of attribute date.
17 18 19 |
# File 'lib/rack/blogengine/document_parser.rb', line 17 def date @date end |
.html ⇒ Object (private)
Returns the value of attribute html.
17 18 19 |
# File 'lib/rack/blogengine/document_parser.rb', line 17 def html @html end |
.layout ⇒ Object (private)
Returns the value of attribute layout.
17 18 19 |
# File 'lib/rack/blogengine/document_parser.rb', line 17 def layout @layout end |
.path ⇒ Object (private)
Returns the value of attribute path.
17 18 19 |
# File 'lib/rack/blogengine/document_parser.rb', line 17 def path @path end |
.target ⇒ Object
Returns the value of attribute target.
13 14 15 |
# File 'lib/rack/blogengine/document_parser.rb', line 13 def target @target end |
.title ⇒ Object (private)
Returns the value of attribute title.
17 18 19 |
# File 'lib/rack/blogengine/document_parser.rb', line 17 def title @title end |
Class Method Details
.fill_file_contents(layout, title, content, date) ⇒ String
Replace layout placeholder with content from .content file
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rack/blogengine/document_parser.rb', line 162 def self.fill_file_contents(layout, title, content, date) html = layout.dup html.gsub! '{title}', title html['{content}'] = content html.gsub! '{date}', date.strftime('%d.%m.%Y') html = Nokogiri::HTML(html) seperator = Rack::Blogengine.config['pygments_seperator'] html.css(seperator).map do |replace_html| highlight_code = get_highlight_code(replace_html.to_s, seperator) highlighted = highlight(highlight_code[:text], highlight_code[:brush]) replace_html.replace(highlighted) end html.to_s end |
.generate_highlight_css(target) ⇒ Object
Populates highlight.css with specific highlight css
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rack/blogengine/document_parser.rb', line 143 def self.generate_highlight_css(target) cli = Rack::Blogengine::CommandLineInterface system("rm #{target}/assets/style/highlight.css") if ::File.exist?("#{target}/assets/style/highlight.css") cli.send(:setup, 'highlight.css', "#{target}/assets/style", false) path = "#{target}/assets/style" css = Pygments.css(style: Rack::Blogengine.config['pygments_style']) ::File.open("#{path}/highlight.css", 'w') { |file| file.write(css) } end |
.get_content_array(content) ⇒ Array
Get Content Array
108 109 110 111 112 113 114 115 116 |
# File 'lib/rack/blogengine/document_parser.rb', line 108 def self.get_content_array(content) # Replace Closing tags content['/path'] = '/close' content['/title'] = '/close' content['/content'] = '/close' content['/date'] = '/close' content.split('[/close]') end |
.get_file_contents(file) ⇒ Object
Get File Contents (path, title, content)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rack/blogengine/document_parser.rb', line 62 def self.get_file_contents(file) content_file = ::File.open("#{target}/#{file}") content = content_file.read contentarray = get_content_array(content) contentarray.each do |contentblock| if contentblock.include? '[path]:' contentblock['[path]:'] = '' @path = "/#{contentblock}" elsif contentblock.include? '[title]:' contentblock['[title]:'] = '' if contentblock.strip.empty? fail "Title in #{file} is empty" else @title = contentblock.strip end elsif contentblock.include? '[content]:' contentblock['[content]:'] = '' if contentblock.strip.empty? fail "Content in #{file} is empty" else @content = contentblock.strip end elsif contentblock.include? '[date]:' contentblock['[date]:'] = '' if /\d/.match(contentblock) datearray = contentblock.split(',') datearray = datearray.map do |date| date.to_i end @date = Date.new(datearray[0], datearray[1], datearray[2]) else fail "Invalid Date in #{file}\n [date]:#{contentblock}[/date]" end end end end |
.get_highlight_code(content, seperator) ⇒ Hash
Get Highlight Code from Content
123 124 125 126 127 128 129 130 |
# File 'lib/rack/blogengine/document_parser.rb', line 123 def self.get_highlight_code(content, seperator) html = ::Nokogiri::HTML(content) klass = html.css(seperator).attr('class') brush = klass.to_s.split(':')[1] # return { text: html.css(seperator).text, brush: brush } end |
.highlight(code, language) ⇒ String
Highlight Code in specific language
137 138 139 |
# File 'lib/rack/blogengine/document_parser.rb', line 137 def self.highlight(code, language) Pygments.highlight(code, lexer: language.to_sym) end |
.parse_in_documents(target) ⇒ Hash
Parse in .content Documents.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rack/blogengine/document_parser.rb', line 23 def self.parse_in_documents(target) @target = target documents = [] layout_file = ::File.open("#{target}/assets/layout/layout.html", 'r') layout = layout_file.read Dir.foreach("#{target}/") do |item| extension = item.split('.')[1] next if item == '.' || item == '..' || extension != 'content' get_file_contents(item) html = fill_file_contents(layout, title, content, date) @document = Document.new @document.path = path @document.html = html @document.title = title @document.date = date documents << @document end generate_highlight_css(target) sort(documents) # Has to exec operator after all docs were read, # so documents are available for operators (list all docs, etc...) documents.each do |document| document.exec_content_operator(documents, target) end documents.map do |document| document.to_hash end end |
.sort(documents) ⇒ Array
Sort documents array by date of each documenthash Should it be sorted in Core or in the operator??
187 188 189 190 191 192 193 |
# File 'lib/rack/blogengine/document_parser.rb', line 187 def self.sort(documents) documents.sort! do | a, b | a.date.to_time.to_i <=> b.date.to_time.to_i end documents end |