Class: Decidim::Proposals::MarkdownToProposals
- Inherits:
-
Redcarpet::Render::Base
- Object
- Redcarpet::Render::Base
- Decidim::Proposals::MarkdownToProposals
- Defined in:
- lib/decidim/proposals/markdown_to_proposals.rb
Overview
This class parses a participatory text document in markdown and produces Proposals in the form of sections and articles.
This implementation uses Redcarpet Base renderer. Redcarpet::Render::Base performs a callback for every block it finds, what MarkdownToProposals does is to implement callbacks for the blocks which it is interested in performing some actions.
Instance Method Summary collapse
- #double_emphasis(text) ⇒ Object
- #emphasis(text) ⇒ Object
-
#header(title, level) ⇒ Object
Recarpet callback to process headers.
- #image(link, title, alt_text) ⇒ Object
-
#initialize(component, current_user) ⇒ MarkdownToProposals
constructor
Public: Initializes the serializer with a proposal.
-
#link(link, title, content) ⇒ Object
Span-level calls #######################.
-
#list(_contents, list_type) ⇒ Object
Render the list as a whole.
-
#list_item(text, _list_type) ⇒ Object
do not render list items, save them for rendering with the whole list.
-
#paragraph(text) ⇒ Object
Recarpet callback to process paragraphs.
- #parse(document) ⇒ Object
- #underline(text) ⇒ Object
Constructor Details
#initialize(component, current_user) ⇒ MarkdownToProposals
Public: Initializes the serializer with a proposal.
16 17 18 19 20 21 22 23 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 16 def initialize(component, current_user) super() @component = component @current_user = current_user @last_position = 0 @num_sections = 0 @list_items = [] end |
Instance Method Details
#double_emphasis(text) ⇒ Object
119 120 121 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 119 def double_emphasis(text) "<strong>#{text}</strong>" end |
#emphasis(text) ⇒ Object
115 116 117 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 115 def emphasis(text) "<em>#{text}</em>" end |
#header(title, level) ⇒ Object
Recarpet callback to process headers. Creates Participatory Text Proposals at Section and Subsection levels.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 46 def header(title, level) participatory_text_level = if level > 1 Decidim::Proposals::ParticipatoryTextSection::LEVELS[:sub_section] else Decidim::Proposals::ParticipatoryTextSection::LEVELS[:section] end create_proposal(title, title, participatory_text_level) @num_sections += 1 title end |
#image(link, title, alt_text) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 108 def image(link, title, alt_text) attrs = %(src="#{link}") attrs += %( alt="#{alt_text}") if alt_text.present? attrs += %( title="#{title}") if title.present? "<img #{attrs}/>" end |
#link(link, title, content) ⇒ Object
Span-level calls #######################
102 103 104 105 106 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 102 def link(link, title, content) attrs = %(href="#{link}") attrs += %( title="#{title}") if title.present? "<a #{attrs}>#{content}</a>" end |
#list(_contents, list_type) ⇒ Object
Render the list as a whole
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 74 def list(_contents, list_type) return if @list_items.empty? body = case list_type when :ordered @list_items.collect.with_index { |item, idx| "#{idx + 1}. #{item}\n" }.join else @list_items.collect { |item| "- #{item}\n" }.join end # reset items for the next list @list_items = [] create_proposal( (@last_position + 1 - @num_sections).to_s, body, Decidim::Proposals::ParticipatoryTextSection::LEVELS[:article] ) body end |
#list_item(text, _list_type) ⇒ Object
do not render list items, save them for rendering with the whole list
95 96 97 98 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 95 def list_item(text, _list_type) @list_items << text.strip nil end |
#paragraph(text) ⇒ Object
Recarpet callback to process paragraphs. Creates Participatory Text Proposals at Article level.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 61 def paragraph(text) return if text.blank? create_proposal( (@last_position + 1 - @num_sections).to_s, text, Decidim::Proposals::ParticipatoryTextSection::LEVELS[:article] ) text end |
#parse(document) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 25 def parse(document) renderer = self extensions = { # no lax_spacing so that it is easier to group paragraphs in articles. lax_spacing: false, fenced_code_blocks: true, autolink: true, underline: true } parser = ::Redcarpet::Markdown.new(renderer, extensions) parser.render(document) end |
#underline(text) ⇒ Object
123 124 125 |
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 123 def underline(text) "<u>#{text}</u>" end |