Class: Decidim::Proposals::MarkdownToProposals

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
decidim-proposals/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

Constructor Details

#initialize(component, current_user) ⇒ MarkdownToProposals

Public: Initializes the serializer with a proposal.



16
17
18
19
20
21
22
23
# File 'decidim-proposals/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



125
126
127
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 125

def double_emphasis(text)
  "<strong>#{text}</strong>"
end

#emphasis(text) ⇒ Object



121
122
123
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 121

def emphasis(text)
  "<em>#{text}</em>"
end

#header(title, level) ⇒ Object

Recarpet callback to process headers. Creates Paricipatory Text Proposals at Section and Subsection levels.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 52

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



114
115
116
117
118
119
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 114

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

Span-level calls #######################



108
109
110
111
112
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 108

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 80

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



101
102
103
104
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 101

def list_item(text, _list_type)
  @list_items << text.strip
  nil
end

#paragraph(text) ⇒ Object

Recarpet callback to process paragraphs. Creates Paricipatory Text Proposals at Article level.



67
68
69
70
71
72
73
74
75
76
77
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 67

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 'decidim-proposals/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

#preprocess(document) ⇒ Object

Recarpet callback to preprocess the document. Removes the HTML comment from the markdown file



46
47
48
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 46

def preprocess(document)
  document.gsub(/<!--.*-->/, "")
end

#underline(text) ⇒ Object



129
130
131
# File 'decidim-proposals/lib/decidim/proposals/markdown_to_proposals.rb', line 129

def underline(text)
  "<u>#{text}</u>"
end