2017-08-13 13:02:48 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 17:21:59 +00:00
|
|
|
require "redcarpet"
|
|
|
|
require "nokogiri"
|
|
|
|
require "rails_guides/markdown/renderer"
|
2022-07-06 06:59:06 +00:00
|
|
|
require "rails_guides/markdown/epub_renderer"
|
2019-01-04 07:37:24 +00:00
|
|
|
require "rails-html-sanitizer"
|
2012-08-31 16:01:06 +00:00
|
|
|
|
|
|
|
module RailsGuides
|
|
|
|
class Markdown
|
2022-07-06 06:59:06 +00:00
|
|
|
def initialize(view:, layout:, edge:, version:, epub:)
|
2017-02-12 09:21:20 +00:00
|
|
|
@view = view
|
|
|
|
@layout = layout
|
|
|
|
@edge = edge
|
|
|
|
@version = version
|
2012-08-31 21:49:17 +00:00
|
|
|
@index_counter = Hash.new(0)
|
2017-02-12 09:21:20 +00:00
|
|
|
@raw_header = ""
|
|
|
|
@node_ids = {}
|
2022-07-06 06:59:06 +00:00
|
|
|
@epub = epub
|
2012-08-31 16:01:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(body)
|
2012-09-01 20:39:47 +00:00
|
|
|
@raw_body = body
|
|
|
|
extract_raw_header_and_body
|
2012-08-31 21:49:17 +00:00
|
|
|
generate_header
|
2019-01-04 07:37:24 +00:00
|
|
|
generate_description
|
2012-08-31 21:49:17 +00:00
|
|
|
generate_title
|
|
|
|
generate_body
|
|
|
|
generate_structure
|
|
|
|
generate_index
|
|
|
|
render_page
|
2012-08-31 16:01:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-08-31 21:49:17 +00:00
|
|
|
def dom_id(nodes)
|
2012-09-17 03:12:45 +00:00
|
|
|
dom_id = dom_id_text(nodes.last.text)
|
|
|
|
|
2024-02-06 19:45:44 +00:00
|
|
|
# Fix duplicate dom_ids by prefixing the parent node dom_id
|
2012-09-17 03:12:45 +00:00
|
|
|
if @node_ids[dom_id]
|
|
|
|
if @node_ids[dom_id].size > 1
|
|
|
|
duplicate_nodes = @node_ids.delete(dom_id)
|
2024-02-06 19:45:44 +00:00
|
|
|
new_node_id = dom_id_with_parent_node(dom_id, duplicate_nodes[-2])
|
2012-09-17 03:12:45 +00:00
|
|
|
duplicate_nodes.last[:id] = new_node_id
|
|
|
|
@node_ids[new_node_id] = duplicate_nodes
|
|
|
|
end
|
2024-02-06 19:45:44 +00:00
|
|
|
dom_id = dom_id_with_parent_node(dom_id, nodes[-2])
|
2012-09-17 03:12:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@node_ids[dom_id] = nodes
|
|
|
|
dom_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def dom_id_text(text)
|
2014-08-24 03:58:01 +00:00
|
|
|
escaped_chars = Regexp.escape('\\/`*_{}[]()#+-.!:,;|&<>^~=\'"')
|
|
|
|
|
2016-08-06 17:21:59 +00:00
|
|
|
text.downcase.gsub(/\?/, "-questionmark")
|
|
|
|
.gsub(/!/, "-bang")
|
|
|
|
.gsub(/[#{escaped_chars}]+/, " ").strip
|
|
|
|
.gsub(/\s+/, "-")
|
2012-08-31 21:49:17 +00:00
|
|
|
end
|
|
|
|
|
2024-02-06 19:45:44 +00:00
|
|
|
def dom_id_with_parent_node(dom_id, parent_node)
|
|
|
|
if parent_node
|
|
|
|
[parent_node[:id], dom_id].join("-")
|
|
|
|
else
|
|
|
|
dom_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 16:01:06 +00:00
|
|
|
def engine
|
2022-07-06 06:59:06 +00:00
|
|
|
renderer = @epub ? EpubRenderer : Renderer
|
|
|
|
@engine ||= Redcarpet::Markdown.new(renderer,
|
2016-08-09 21:36:39 +00:00
|
|
|
no_intra_emphasis: true,
|
2012-08-31 16:01:06 +00:00
|
|
|
fenced_code_blocks: true,
|
|
|
|
autolink: true,
|
|
|
|
strikethrough: true,
|
2012-09-02 01:37:50 +00:00
|
|
|
superscript: true,
|
2017-02-12 09:21:20 +00:00
|
|
|
tables: true
|
|
|
|
)
|
2012-08-31 16:01:06 +00:00
|
|
|
end
|
|
|
|
|
2012-09-01 20:39:47 +00:00
|
|
|
def extract_raw_header_and_body
|
2020-09-07 11:13:53 +00:00
|
|
|
if /^-{40,}$/.match?(@raw_body)
|
|
|
|
@raw_header, _, @raw_body = @raw_body.partition(/^-{40,}$/).map(&:strip)
|
2012-09-01 20:39:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 21:49:17 +00:00
|
|
|
def generate_body
|
|
|
|
@body = engine.render(@raw_body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_header
|
|
|
|
@header = engine.render(@raw_header).html_safe
|
|
|
|
end
|
|
|
|
|
2019-01-04 07:37:24 +00:00
|
|
|
def generate_description
|
|
|
|
sanitizer = Rails::Html::FullSanitizer.new
|
|
|
|
@description = sanitizer.sanitize(@header).squish
|
|
|
|
end
|
|
|
|
|
2012-08-31 21:49:17 +00:00
|
|
|
def generate_structure
|
2012-09-17 03:12:45 +00:00
|
|
|
@headings_for_index = []
|
2012-09-01 20:39:47 +00:00
|
|
|
if @body.present?
|
2023-06-19 19:13:34 +00:00
|
|
|
document = html_fragment(@body).tap do |doc|
|
2012-09-01 20:39:47 +00:00
|
|
|
hierarchy = []
|
|
|
|
|
2013-10-27 19:14:41 +00:00
|
|
|
doc.children.each do |node|
|
2023-04-18 20:27:03 +00:00
|
|
|
if /^h[2-5]$/.match?(node.name)
|
2012-09-01 20:39:47 +00:00
|
|
|
case node.name
|
2023-04-18 20:27:03 +00:00
|
|
|
when "h2"
|
2012-09-01 20:39:47 +00:00
|
|
|
hierarchy = [node]
|
2012-09-17 03:12:45 +00:00
|
|
|
@headings_for_index << [1, node, node.inner_html]
|
2023-04-18 20:27:03 +00:00
|
|
|
when "h3"
|
2012-09-01 20:39:47 +00:00
|
|
|
hierarchy = hierarchy[0, 1] + [node]
|
2012-09-17 03:12:45 +00:00
|
|
|
@headings_for_index << [2, node, node.inner_html]
|
2023-04-18 20:27:03 +00:00
|
|
|
when "h4"
|
2012-09-01 20:39:47 +00:00
|
|
|
hierarchy = hierarchy[0, 2] + [node]
|
2023-04-18 20:27:03 +00:00
|
|
|
when "h5"
|
2012-09-01 20:39:47 +00:00
|
|
|
hierarchy = hierarchy[0, 3] + [node]
|
|
|
|
end
|
2012-08-31 21:49:17 +00:00
|
|
|
|
2018-09-23 10:57:36 +00:00
|
|
|
node[:id] = dom_id(hierarchy) unless node[:id]
|
2024-03-20 19:50:51 +00:00
|
|
|
node.inner_html = "<span>#{node_index(hierarchy)}</span> #{node.inner_html}"
|
2012-09-01 20:39:47 +00:00
|
|
|
end
|
2012-08-31 21:49:17 +00:00
|
|
|
end
|
2017-04-02 04:35:20 +00:00
|
|
|
|
2023-04-18 20:27:03 +00:00
|
|
|
doc.css("h2, h3, h4, h5").each do |node|
|
2017-04-02 04:35:20 +00:00
|
|
|
node.inner_html = "<a class='anchorlink' href='##{node[:id]}'>#{node.inner_html}</a>"
|
|
|
|
end
|
2022-07-06 06:59:06 +00:00
|
|
|
end
|
|
|
|
@body = @epub ? document.to_xhtml : document.to_html
|
2012-09-01 20:39:47 +00:00
|
|
|
end
|
2012-08-31 21:49:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_index
|
2012-09-17 03:12:45 +00:00
|
|
|
if @headings_for_index.present?
|
2016-08-06 17:21:59 +00:00
|
|
|
raw_index = ""
|
2012-09-17 03:12:45 +00:00
|
|
|
@headings_for_index.each do |level, node, label|
|
|
|
|
if level == 1
|
|
|
|
raw_index += "1. [#{label}](##{node[:id]})\n"
|
|
|
|
elsif level == 2
|
|
|
|
raw_index += " * [#{label}](##{node[:id]})\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-19 19:13:34 +00:00
|
|
|
@index = html_fragment(engine.render(raw_index)).tap do |doc|
|
2016-08-06 17:21:59 +00:00
|
|
|
doc.at("ol")[:class] = "chapters"
|
2012-09-01 20:39:47 +00:00
|
|
|
end.to_html
|
|
|
|
|
|
|
|
@index = <<-INDEX.html_safe
|
2024-03-20 19:50:51 +00:00
|
|
|
<nav id="subCol">
|
|
|
|
<h3 class="chapter">
|
|
|
|
<picture>
|
|
|
|
<!-- Using the `source` HTML tag to set the dark theme image -->
|
|
|
|
<source
|
|
|
|
srcset="images/icon_book-close-bookmark-1-wht.svg"
|
|
|
|
media="(prefers-color-scheme: dark)"
|
|
|
|
/>
|
|
|
|
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
|
|
|
|
</picture>
|
|
|
|
Chapters
|
|
|
|
</h3>
|
2012-09-01 20:39:47 +00:00
|
|
|
#{@index}
|
2024-03-20 19:50:51 +00:00
|
|
|
</nav>
|
2012-09-01 20:39:47 +00:00
|
|
|
INDEX
|
|
|
|
end
|
2012-08-31 21:49:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_title
|
2023-06-19 19:13:34 +00:00
|
|
|
if heading = html_fragment(@header).at(:h1)
|
2013-10-27 19:14:41 +00:00
|
|
|
@title = "#{heading.text} — Ruby on Rails Guides"
|
2012-09-01 20:39:47 +00:00
|
|
|
else
|
|
|
|
@title = "Ruby on Rails Guides"
|
|
|
|
end
|
2012-08-31 21:49:17 +00:00
|
|
|
end
|
2012-08-31 16:01:06 +00:00
|
|
|
|
2012-08-31 21:49:17 +00:00
|
|
|
def node_index(hierarchy)
|
|
|
|
case hierarchy.size
|
|
|
|
when 1
|
|
|
|
@index_counter[2] = @index_counter[3] = @index_counter[4] = 0
|
|
|
|
"#{@index_counter[1] += 1}"
|
|
|
|
when 2
|
|
|
|
@index_counter[3] = @index_counter[4] = 0
|
|
|
|
"#{@index_counter[1]}.#{@index_counter[2] += 1}"
|
|
|
|
when 3
|
|
|
|
@index_counter[4] = 0
|
|
|
|
"#{@index_counter[1]}.#{@index_counter[2]}.#{@index_counter[3] += 1}"
|
|
|
|
when 4
|
|
|
|
"#{@index_counter[1]}.#{@index_counter[2]}.#{@index_counter[3]}.#{@index_counter[4] += 1}"
|
2012-08-31 16:01:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-31 21:49:17 +00:00
|
|
|
def render_page
|
|
|
|
@view.content_for(:header_section) { @header }
|
2019-01-04 07:37:24 +00:00
|
|
|
@view.content_for(:description) { @description }
|
2012-08-31 21:49:17 +00:00
|
|
|
@view.content_for(:page_title) { @title }
|
2012-09-01 20:39:47 +00:00
|
|
|
@view.content_for(:index_section) { @index }
|
2016-10-25 13:18:34 +00:00
|
|
|
@view.render(layout: @layout, html: @body.html_safe)
|
2012-08-31 16:01:06 +00:00
|
|
|
end
|
2023-06-19 19:13:34 +00:00
|
|
|
|
|
|
|
def html_fragment(html)
|
|
|
|
if defined?(Nokogiri::HTML5)
|
|
|
|
Nokogiri::HTML5.fragment(html)
|
|
|
|
else
|
|
|
|
Nokogiri::HTML4.fragment(html)
|
|
|
|
end
|
|
|
|
end
|
2012-08-31 16:01:06 +00:00
|
|
|
end
|
|
|
|
end
|