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

.targetObject

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

Parameters:

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

Returns:

  • (String)

    html placeholder replaced with content



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

Parameters:

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

Returns:

  • (Array)

    contentArray [Splitted Content File]



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)

Parameters:

  • file (String)


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

Parameters:

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

Returns:

  • (Hash)

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



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

Parameters:

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

Returns:

  • (String)

    Highlighted HTML String



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.

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

Parameters:

  • documents (Array)

Returns:

  • (Array)

    documents (sorted)



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