Class: Govspeak::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/govspeak.rb

Constant Summary collapse

Parser =
Kramdown::Parser::KramdownWithAutomaticExternalLinks
PARSER_CLASS_NAME =
Parser.name.split("::").last
@@extensions =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Document

Returns a new instance of Document.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/govspeak.rb', line 34

def initialize(source, options = {})
  options = options.dup.deep_symbolize_keys
  @source = source ? source.dup : ""
  @images = options.delete(:images) || []
  @attachments = Array.wrap(options.delete(:attachments))
  @links = Array.wrap(options.delete(:links))
  @contacts = Array.wrap(options.delete(:contacts))
  @locale = options.fetch(:locale, "en")
  @options = {input: PARSER_CLASS_NAME}.merge(options)
  @options[:entity_output] = :symbolic
  i18n_load_paths
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



28
29
30
# File 'lib/govspeak.rb', line 28

def attachments
  @attachments
end

#contactsObject (readonly)

Returns the value of attribute contacts.



28
29
30
# File 'lib/govspeak.rb', line 28

def contacts
  @contacts
end

#imagesObject

Returns the value of attribute images.



27
28
29
# File 'lib/govspeak.rb', line 27

def images
  @images
end

Returns the value of attribute links.



28
29
30
# File 'lib/govspeak.rb', line 28

def links
  @links
end

#localeObject (readonly)

Returns the value of attribute locale.



28
29
30
# File 'lib/govspeak.rb', line 28

def locale
  @locale
end

Class Method Details

.devolved_optionsObject



298
299
300
301
302
303
304
305
# File 'lib/govspeak.rb', line 298

def self.devolved_options
 { 'scotland' => 'Scotland',
   'england' => 'England',
   'england-wales' => 'England and Wales',
   'northern-ireland' => 'Northern Ireland',
   'wales' => 'Wales',
   'london' => 'London' }
end

.extension(title, regexp = nil, &block) ⇒ Object



112
113
114
115
# File 'lib/govspeak.rb', line 112

def self.extension(title, regexp = nil, &block)
  regexp ||= %r${::#{title}}(.*?){:/#{title}}$m
  @@extensions << [title, regexp, block]
end

.surrounded_by(open, close = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/govspeak.rb', line 117

def self.surrounded_by(open, close=nil)
  open = Regexp::escape(open)
  if close
    close = Regexp::escape(close)
    %r+(?:\r|\n|^)#{open}(.*?)#{close} *(\r|\n|$)?+m
  else
    %r+(?:\r|\n|^)#{open}(.*?)#{open}? *(\r|\n|$)+m
  end
end

.to_html(source, options = {}) ⇒ Object



30
31
32
# File 'lib/govspeak.rb', line 30

def self.to_html(source, options = {})
  new(source, options).to_html
end

.wrap_with_div(class_name, character, parser = Kramdown::Document) ⇒ Object



127
128
129
130
131
132
# File 'lib/govspeak.rb', line 127

def self.wrap_with_div(class_name, character, parser=Kramdown::Document)
  extension(class_name, surrounded_by(character)) { |body|
    content = parser ? parser.new("#{body.strip}\n").to_html : body.strip
    %{\n<div class="#{class_name}">\n#{content}</div>\n}
  }
end

Instance Method Details

#headersObject



89
90
91
# File 'lib/govspeak.rb', line 89

def headers
  Govspeak::HeaderExtractor.convert(kramdown_doc).first
end

#insert_strong_inside_p(body, parser = Govspeak::Document) ⇒ Object



134
135
136
# File 'lib/govspeak.rb', line 134

def insert_strong_inside_p(body, parser=Govspeak::Document)
  parser.new(body.strip).to_html.sub(/^<p>(.*)<\/p>$/,"<p><strong>\\1</strong></p>")
end

#preprocess(source) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/govspeak.rb', line 97

def preprocess(source)
  source = Govspeak::BlockquoteExtraQuoteRemover.remove(source)
  @@extensions.each do |title,regexp,block|
    source.gsub!(regexp) {
      instance_exec(*Regexp.last_match.captures, &block)
    }
  end
  source
end

#render_image(url, alt_text, caption = nil, id = nil) ⇒ Object

As of version 1.12.0 of Kramdown the block elements (div & figcaption) inside this html block will have it’s < > converted into HTML Entities when ever this code is used inside block level elements.

To resolve this we have a post-processing task that will convert this back into HTML (I know - it’s ugly). The way we could resolve this without ugliness would be to output only inline elements which rules out div and figcaption

This issue is not considered a bug by kramdown: github.com/gettalong/kramdown/issues/191



257
258
259
260
261
262
263
264
265
# File 'lib/govspeak.rb', line 257

def render_image(url, alt_text, caption = nil, id = nil)
  id_attr = id ? %{ id="attachment_#{id}"} : ""
  lines = []
  lines << %{<figure#{id_attr} class="image embedded">}
  lines << %Q{<div class="img"><img src="#{encode(url)}" alt="#{encode(alt_text)}"></div>}
  lines << %Q{<figcaption>#{caption.strip}</figcaption>} if caption && !caption.strip.empty?
  lines << '</figure>'
  lines.join
end

#structured_headersObject



93
94
95
# File 'lib/govspeak.rb', line 93

def structured_headers
  Govspeak::StructuredHeaderExtractor.new(self).call
end

#t(*args) ⇒ Object



67
68
69
70
71
# File 'lib/govspeak.rb', line 67

def t(*args)
  options = args.last.is_a?(Hash) ? args.last.dup : {}
  key = args.shift
  I18n.t(key, options.merge(locale: locale))
end

#to_htmlObject



59
60
61
# File 'lib/govspeak.rb', line 59

def to_html
  @html ||= Govspeak::PostProcessor.process(kramdown_doc.to_html)
end

#to_liquidObject



63
64
65
# File 'lib/govspeak.rb', line 63

def to_liquid
  to_html
end

#to_sanitized_htmlObject



73
74
75
# File 'lib/govspeak.rb', line 73

def to_sanitized_html
  HtmlSanitizer.new(to_html).sanitize
end

#to_sanitized_html_without_imagesObject



77
78
79
# File 'lib/govspeak.rb', line 77

def to_sanitized_html_without_images
  HtmlSanitizer.new(to_html).sanitize_without_images
end

#to_textObject



81
82
83
# File 'lib/govspeak.rb', line 81

def to_text
  HTMLEntities.new.decode(to_html.gsub(/(?:<[^>]+>|\s)+/, " ").strip)
end

#valid?(validation_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/govspeak.rb', line 85

def valid?(validation_options = {})
  Govspeak::HtmlValidator.new(@source, validation_options).valid?
end