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, 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.
- #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.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/govspeak.rb', line 34 def initialize(source, = {}) = .dup.deep_symbolize_keys @source = source ? source.dup : "" @images = .delete(:images) || [] @attachments = Array.wrap(.delete(:attachments)) @links = Array.wrap(.delete(:links)) @contacts = Array.wrap(.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.
28 29 30 |
# File 'lib/govspeak.rb', line 28 def @attachments end |
#contacts ⇒ Object (readonly)
Returns the value of attribute contacts.
28 29 30 |
# File 'lib/govspeak.rb', line 28 def contacts @contacts end |
#images ⇒ Object
Returns the value of attribute images.
27 28 29 |
# File 'lib/govspeak.rb', line 27 def images @images end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
28 29 30 |
# File 'lib/govspeak.rb', line 28 def links @links end |
#locale ⇒ Object (readonly)
Returns the value of attribute locale.
28 29 30 |
# File 'lib/govspeak.rb', line 28 def locale @locale end |
Class Method Details
.devolved_options ⇒ Object
298 299 300 301 302 303 304 305 |
# File 'lib/govspeak.rb', line 298 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
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, = {}) new(source, ).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
#headers ⇒ Object
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, = 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>#{.strip}</figcaption>} if && !.strip.empty? lines << '</figure>' lines.join end |
#structured_headers ⇒ Object
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) = args.last.is_a?(Hash) ? args.last.dup : {} key = args.shift I18n.t(key, .merge(locale: locale)) end |
#to_html ⇒ Object
59 60 61 |
# File 'lib/govspeak.rb', line 59 def to_html @html ||= Govspeak::PostProcessor.process(kramdown_doc.to_html) end |
#to_liquid ⇒ Object
63 64 65 |
# File 'lib/govspeak.rb', line 63 def to_liquid to_html end |
#to_sanitized_html ⇒ Object
73 74 75 |
# File 'lib/govspeak.rb', line 73 def to_sanitized_html HtmlSanitizer.new(to_html).sanitize end |
#to_sanitized_html_without_images ⇒ Object
77 78 79 |
# File 'lib/govspeak.rb', line 77 def to_sanitized_html_without_images HtmlSanitizer.new(to_html).sanitize_without_images end |
#to_text ⇒ Object
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
85 86 87 |
# File 'lib/govspeak.rb', line 85 def valid?( = {}) Govspeak::HtmlValidator.new(@source, ).valid? end |