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
-
.target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
-
.fill_file_contents(layout, title, content, date) ⇒ String
Replace layout placeholder with content from .content file.
-
.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
.target ⇒ Object
Returns the value of attribute target.
13 14 15 |
# File 'lib/rack/blogengine/document_parser.rb', line 13 def target @target end |
Class Method Details
.fill_file_contents(layout, title, content, date) ⇒ String
Replace layout placeholder with content from .content file
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rack/blogengine/document_parser.rb', line 147 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 |
.get_content_array(content) ⇒ Array
Get Content Array
107 108 109 110 111 112 113 114 115 |
# File 'lib/rack/blogengine/document_parser.rb', line 107 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)
61 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 |
# File 'lib/rack/blogengine/document_parser.rb', line 61 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
122 123 124 125 126 127 128 129 |
# File 'lib/rack/blogengine/document_parser.rb', line 122 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
136 137 138 |
# File 'lib/rack/blogengine/document_parser.rb', line 136 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 |
# 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 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??
172 173 174 175 176 177 178 |
# File 'lib/rack/blogengine/document_parser.rb', line 172 def self.sort(documents) documents.sort! do | a, b | a.date.to_time.to_i <=> b.date.to_time.to_i end documents end |