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.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/govspeak.rb', line 31

def initialize(source, options = {})
  @source = source ? source.dup : ""
  @images = options.delete(:images) || []
  @attachments = Array(options.delete(:attachments))
  @links = Array(options.delete(:links))
  @contacts = Array(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.



25
26
27
# File 'lib/govspeak.rb', line 25

def attachments
  @attachments
end

#contactsObject (readonly)

Returns the value of attribute contacts.



25
26
27
# File 'lib/govspeak.rb', line 25

def contacts
  @contacts
end

#imagesObject

Returns the value of attribute images.



24
25
26
# File 'lib/govspeak.rb', line 24

def images
  @images
end

Returns the value of attribute links.



25
26
27
# File 'lib/govspeak.rb', line 25

def links
  @links
end

#localeObject (readonly)

Returns the value of attribute locale.



25
26
27
# File 'lib/govspeak.rb', line 25

def locale
  @locale
end

Class Method Details

.devolved_optionsObject



244
245
246
247
248
249
250
251
# File 'lib/govspeak.rb', line 244

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



108
109
110
111
# File 'lib/govspeak.rb', line 108

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

.surrounded_by(open, close = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/govspeak.rb', line 113

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



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

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

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



123
124
125
126
127
128
# File 'lib/govspeak.rb', line 123

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



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

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

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



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

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



93
94
95
96
97
98
99
100
101
# File 'lib/govspeak.rb', line 93

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) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/govspeak.rb', line 204

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

#structured_headersObject



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

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

#t(*args) ⇒ Object



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

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



55
56
57
# File 'lib/govspeak.rb', line 55

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

#to_liquidObject



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

def to_liquid
  to_html
end

#to_sanitized_htmlObject



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

def to_sanitized_html
  HtmlSanitizer.new(to_html).sanitize
end

#to_sanitized_html_without_imagesObject



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

def to_sanitized_html_without_images
  HtmlSanitizer.new(to_html).sanitize_without_images
end

#to_textObject



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

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

#valid?(validation_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


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

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