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

Author:

  • benny

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.contentObject (private)

Returns the value of attribute content.



17
18
19
# File 'lib/rack/blogengine/document_parser.rb', line 17

def content
  @content
end

.dateObject (private)

Returns the value of attribute date.



17
18
19
# File 'lib/rack/blogengine/document_parser.rb', line 17

def date
  @date
end

.htmlObject (private)

Returns the value of attribute html.



17
18
19
# File 'lib/rack/blogengine/document_parser.rb', line 17

def html
  @html
end

.layoutObject (private)

Returns the value of attribute layout.



17
18
19
# File 'lib/rack/blogengine/document_parser.rb', line 17

def layout
  @layout
end

.pathObject (private)

Returns the value of attribute path.



17
18
19
# File 'lib/rack/blogengine/document_parser.rb', line 17

def path
  @path
end

.targetObject

Returns the value of attribute target.



13
14
15
# File 'lib/rack/blogengine/document_parser.rb', line 13

def target
  @target
end

.titleObject (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

Parameters:

  • layout (String)
  • title (String)
  • content (String)
  • date (Date)

Returns:

  • (String)

    html placeholder replaced with content



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

Parameters:

  • target (String)
    Targetfolder in which highlight.css lives


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

Parameters:

  • content (String)
    The Content (.content file)

Returns:

  • (Array)

    contentArray [Splitted Content File]



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)

Parameters:

  • file (String)


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

Parameters:

  • content (String)
    HTML Content
  • seperator (String)
    HTML between seperator will be highlighted

Returns:

  • (Hash)

    :text - HTML to highlight, :brush - Brush via seperator class



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

Parameters:

  • code (String)
    Code to highlight
  • language (String)
    Language to highlight

Returns:

  • (String)

    Highlighted HTML String



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.

Parameters:

  • target (String)

Returns:

  • (Hash)

    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??

Parameters:

  • documents (Array)

Returns:

  • (Array)

    documents (sorted)



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