Class: Decidim::Proposals::MarkdownToProposals

Inherits:
Redcarpet::Render::Base
  • Object
show all
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

Constructor Details

#initialize(component, current_user) ⇒ MarkdownToProposals

Public: Initializes the serializer with a proposal.



16
17
18
19
20
21
22
# 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
end

Instance Method Details

#header(title, level) ⇒ Object

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 36

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

ignore images



64
65
66
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 64

def image(_link, _title, _alt_text)
  ""
end

#paragraph(text) ⇒ Object

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



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

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



24
25
26
27
28
# File 'lib/decidim/proposals/markdown_to_proposals.rb', line 24

def parse(document)
  renderer = self
  parser = ::Redcarpet::Markdown.new(renderer)
  parser.render(document)
end