-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Descriptions can be added to reports that support markdown formatting…
…, addresses #9.
- Loading branch information
1 parent
3aaaac0
commit 7a2a1e5
Showing
11 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
# Renders text written with markdown. | ||
module MarkdownHelper | ||
def simple_markdown(text, target_blank: true, table_class: "", allow_links: true, allow_images: true, allow_tables: true, allow_lists: true, pretend_links: false) | ||
result = text.to_s | ||
result = replace_numbers_with_ascii(result) unless allow_lists | ||
result = redcarpet_markdown.render(result) | ||
result = result.encode("UTF-16", undef: :replace, invalid: :replace, replace: "").encode("UTF-8") | ||
result = add_table_class(result, table_class) unless table_class.blank? | ||
result = remove_links(result) unless allow_links | ||
result = pretend_links(result) if pretend_links | ||
if allow_images | ||
result = wrap_images(result) | ||
else | ||
result = remove_images(result) | ||
end | ||
result = remove_tables(result) unless allow_tables | ||
result = target_link_as_blank(result) if target_blank | ||
result.html_safe | ||
end | ||
|
||
def target_link_as_blank(text) | ||
text.to_s.gsub(/<a(.*?)>/m, "<a\\1 target=\"_blank\">").html_safe | ||
end | ||
|
||
def remove_links(text) | ||
text.to_s.gsub(%r{<a[^>]*? href="(.*?)">(.*?)</a>}m, "\\2") | ||
end | ||
|
||
def pretend_links(text) | ||
text.to_s.gsub(%r{<a[^>]*? href="(.*?)">(.*?)</a>}m, "<span class=\"text-primary\">\\2</span>") | ||
end | ||
|
||
def wrap_images(text) | ||
text.to_s.gsub(/(<img.*?>)/m, "<div class=\"img-zoom-message\">\\1</div>") | ||
end | ||
|
||
def remove_images(text) | ||
text.to_s.gsub(/<img src="(.*?)"(.*?)>/m, "<div>\\1</div>") | ||
end | ||
|
||
def remove_tables(text) | ||
text.to_s.gsub(%r{<table(.*?)>(.*?)</table>}m, "") | ||
end | ||
|
||
def add_table_class(text, table_class) | ||
text.to_s.gsub(/<table>/m, "<table class=\"#{table_class}\">").html_safe | ||
end | ||
|
||
def replace_numbers_with_ascii(text) | ||
text.gsub(/^[ \t]*(\d)/) { |m| ascii_number($1) } | ||
end | ||
|
||
def ascii_number(number) | ||
"&##{number.to_i + 48};" | ||
end | ||
|
||
def redcarpet_markdown | ||
Redcarpet::Markdown.new( | ||
Redcarpet::Render::HTML, | ||
no_intra_emphasis: true, | ||
fenced_code_blocks: true, | ||
autolink: true, | ||
strikethrough: true, | ||
superscript: true, | ||
tables: true, | ||
lax_spacing: true, | ||
space_after_headers: true, | ||
underline: true, | ||
highlight: true, | ||
footnotes: true | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.dashboard-container.dashboard-container-border-accent | ||
= simple_markdown report.description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddDescriptionToReports < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :reports, :description, :text | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters