Class: Govspeak::Document
- Inherits:
-
Object
- Object
- Govspeak::Document
- Defined in:
- lib/govspeak.rb
Constant Summary collapse
- Parser =
Kramdown::Parser::KramdownWithAutomaticExternalLinks
- PARSER_CLASS_NAME =
Parser.name.split("::").last
- @@extensions =
[]
Instance Attribute Summary collapse
-
#attachments ⇒ Object
readonly
Returns the value of attribute attachments.
-
#contacts ⇒ Object
readonly
Returns the value of attribute contacts.
-
#images ⇒ Object
Returns the value of attribute images.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#locale ⇒ Object
readonly
Returns the value of attribute locale.
Class Method Summary collapse
- .devolved_options ⇒ Object
- .extension(title, regexp = nil, &block) ⇒ Object
- .surrounded_by(open, close = nil) ⇒ Object
- .to_html(source, options = {}) ⇒ Object
- .wrap_with_div(class_name, character, parser = Kramdown::Document) ⇒ Object
Instance Method Summary collapse
- #headers ⇒ Object
-
#initialize(source, options = {}) ⇒ Document
constructor
A new instance of Document.
- #insert_strong_inside_p(body, parser = Govspeak::Document) ⇒ Object
- #preprocess(source) ⇒ Object
- #render_image(url, alt_text, caption = nil) ⇒ Object
- #structured_headers ⇒ Object
- #t(*args) ⇒ Object
- #to_html ⇒ Object
- #to_liquid ⇒ Object
- #to_sanitized_html ⇒ Object
- #to_sanitized_html_without_images ⇒ Object
- #to_text ⇒ Object
- #valid?(validation_options = {}) ⇒ Boolean
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, = {}) @source = source ? source.dup : "" @images = .delete(:images) || [] @attachments = Array(.delete(:attachments)) @links = Array(.delete(:links)) @contacts = Array(.delete(:contacts)) @locale = .fetch(:locale, "en") @options = {input: PARSER_CLASS_NAME}.merge() @options[:entity_output] = :symbolic i18n_load_paths end |
Instance Attribute Details
#attachments ⇒ Object (readonly)
Returns the value of attribute attachments.
25 26 27 |
# File 'lib/govspeak.rb', line 25 def @attachments end |
#contacts ⇒ Object (readonly)
Returns the value of attribute contacts.
25 26 27 |
# File 'lib/govspeak.rb', line 25 def contacts @contacts end |
#images ⇒ Object
Returns the value of attribute images.
24 25 26 |
# File 'lib/govspeak.rb', line 24 def images @images end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
25 26 27 |
# File 'lib/govspeak.rb', line 25 def links @links end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
25 26 27 |
# File 'lib/govspeak.rb', line 25 def locale @locale end |
Class Method Details
.devolved_options ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/govspeak.rb', line 244 def self. { '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, = {}) new(source, ).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
#headers ⇒ Object
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_headers ⇒ Object
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) = args.last.is_a?(Hash) ? args.last.dup : {} key = args.shift I18n.t(key, .merge(locale: locale)) end |
#to_html ⇒ Object
55 56 57 |
# File 'lib/govspeak.rb', line 55 def to_html @html ||= Govspeak::PostProcessor.process(kramdown_doc.to_html) end |
#to_liquid ⇒ Object
59 60 61 |
# File 'lib/govspeak.rb', line 59 def to_liquid to_html end |
#to_sanitized_html ⇒ Object
69 70 71 |
# File 'lib/govspeak.rb', line 69 def to_sanitized_html HtmlSanitizer.new(to_html).sanitize end |
#to_sanitized_html_without_images ⇒ Object
73 74 75 |
# File 'lib/govspeak.rb', line 73 def to_sanitized_html_without_images HtmlSanitizer.new(to_html).sanitize_without_images end |
#to_text ⇒ Object
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
81 82 83 |
# File 'lib/govspeak.rb', line 81 def valid?( = {}) Govspeak::HtmlValidator.new(@source, ).valid? end |