Class: MarkdownRecord::Rendering::Nodes::HtmlBase

Inherits:
Object
  • Object
show all
Includes:
ContentDsl, PathUtilities
Defined in:
lib/markdown_record/rendering/nodes/html_base.rb

Direct Known Subclasses

HtmlDirectory, HtmlFile

Constant Summary collapse

HTML_SUBSTITUTIONS =
{
  /<!---/ => "<!--",
  /&lt;!---/ => "&lt;!--"
}
HTML_MACROS =
{
  /<code(?:.*?)>((?:.|\s)*?)<\/code>/ => lambda { |match, first| match.gsub(first, CGI.unescapeHTML(CGI.escapeHTML(first))) }
}

Constants included from ContentDsl

ContentDsl::HTML_COMMENT_REGEX

Constants included from ContentDsl::Enable

ContentDsl::Enable::ENCODED_REGEX, ContentDsl::Enable::REGEX

Constants included from ContentDsl::Disable

ContentDsl::Disable::ENCODED_REGEX, ContentDsl::Disable::REGEX

Constants included from ContentDsl::UseLayout

ContentDsl::UseLayout::ENCODED_REGEX, ContentDsl::UseLayout::REGEX

Constants included from ContentDsl::Fragment

ContentDsl::Fragment::ENCODED_REGEX, ContentDsl::Fragment::REGEX

Constants included from ContentDsl::DirectoryFragment

ContentDsl::DirectoryFragment::ENCODED_REGEX, ContentDsl::DirectoryFragment::REGEX

Constants included from ContentDsl::EndModel

ContentDsl::EndModel::ENCODED_REGEX, ContentDsl::EndModel::REGEX

Constants included from ContentDsl::EndAttribute

ContentDsl::EndAttribute::ENCODED_REGEX, ContentDsl::EndAttribute::REGEX

Constants included from ContentDsl::Attribute

ContentDsl::Attribute::ENCODED_REGEX, ContentDsl::Attribute::REGEX

Constants included from ContentDsl::Model

ContentDsl::Model::ENCODED_REGEX, ContentDsl::Model::REGEX

Constants included from ContentDsl::Scope

ContentDsl::Scope::ENCODED_REGEX, ContentDsl::Scope::REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContentDsl

#remove_html_dsl_command, #remove_json_dsl_commands

Methods included from ContentDsl::Enable

#enable_dsl, remove_dsl

Methods included from ContentDsl::Disable

#disable_dsl, remove_dsl

Methods included from ContentDsl::UseLayout

remove_dsl, #use_layout_dsl

Methods included from ContentDsl::Fragment

#fragment_dsl, remove_dsl

Methods included from ContentDsl::DirectoryFragment

#directory_fragment_dsl, remove_dsl

Methods included from ContentDsl::EndModel

#end_model_dsl, remove_dsl

Methods included from ContentDsl::EndAttribute

#end_attribute_dsl, remove_dsl

Methods included from ContentDsl::Attribute

#attribute_dsl, remove_dsl

Methods included from ContentDsl::Model

#model_dsl, remove_dsl

Methods included from ContentDsl::Scope

remove_dsl, #scope_dsl

Methods included from PathUtilities

#base_content_root_name, #base_rendered_path, #base_rendered_root, #clean_path, #erb_locals_from_path, #fragment_attributes_from_path, #full_path_to_parts, #path_to_fragment_id, #remove_prefix, #rendered_path, #scoped_id_to_parts, #to_scoped_id

Constructor Details

#initialize(pathname, markdown, options) ⇒ HtmlBase

Returns a new instance of HtmlBase.



21
22
23
24
25
26
27
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 21

def initialize(pathname, markdown, options)
  @markdown = markdown
  @pathname = pathname
  @options = options
  @name = @pathname.relative_path_from(MarkdownRecord.config.content_root.parent).to_s
  @sorter = MarkdownRecord.config.filename_sorter
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 8

def name
  @name
end

#processed_htmlObject (readonly)

Returns the value of attribute processed_html.



10
11
12
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 10

def processed_html
  @processed_html
end

#rendered_htmlObject (readonly)

Returns the value of attribute rendered_html.



9
10
11
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 9

def rendered_html
  @rendered_html
end

Instance Method Details

#finalize_htmlObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 40

def finalize_html
  return unless @processed_html.present?

  locals = erb_locals_from_path(@name)
  final_html = remove_html_dsl_command(@processed_html) 
  final_html = render_erb(global_layout, locals.merge(html: final_html)) if global_layout

  HTML_SUBSTITUTIONS.each do |find, replace|
    final_html = final_html.gsub(find, replace)
  end

  HTML_MACROS.each do |regex, macro|
    final_html = final_html.gsub(regex) do |match|
      macro.call(match, $1).html_safe
    end
  end

  final_html = final_html.squeeze("\n")
  @final_html = HtmlBeautifier.beautify(final_html)
end

#global_layoutObject



90
91
92
93
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 90

def global_layout
  global_layout_path = ::MarkdownRecord.config.global_layout_path
  @global_layout ||= global_layout_path ? load_layout(global_layout_path) : nil
end

#layoutObject



77
78
79
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 77

def layout
  nil
end

#load_layout(path) ⇒ Object



95
96
97
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 95

def load_layout(path)
  File.read(::MarkdownRecord.config.layout_directory.join(path))
end

#process_htmlObject



36
37
38
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 36

def process_html
  @processed_html = render_erbs(@rendered_html)
end

#render(file_saver) ⇒ Object



29
30
31
32
33
34
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 29

def render(file_saver)
  @rendered_html = ""
  process_html
  finalize_html
  save(file_saver)
end

#render_erb(html, locals) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 81

def render_erb(html, locals)
  render_controller = ::MarkdownRecord.config.render_controller || ::ApplicationController
  rendered_erb = render_controller.render(
    inline: html,
    locals: locals
  ).to_str
  rendered_erb
end

#render_erbs(html) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 68

def render_erbs(html)
  processed_html = html.gsub(/<p>(\&lt;%(\S|\s)*?%\&gt;)<\/p>/){ CGI.unescapeHTML($1) }
  processed_html = processed_html.gsub(/(\&lt;%(\S|\s)*?%\&gt;)/){ CGI.unescapeHTML($1) }
  locals = erb_locals_from_path(name)
  processed_html = render_erb(processed_html, locals) if name.ends_with?(".md.erb")
  processed_html = render_erb(layout, locals.merge(html: processed_html)) if layout
  processed_html
end

#save(file_saver) ⇒ Object



61
62
63
64
65
66
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 61

def save(file_saver)
  return unless @final_html.present?

  path = clean_path(@name)
  file_saver.save_to_file(@final_html, "#{path}.html", @options)
end

#sort_valueObject



99
100
101
# File 'lib/markdown_record/rendering/nodes/html_base.rb', line 99

def sort_value
  @sorter.path_to_sort_value(@name)
end