Class: GovukPublishingComponents::ComponentExample
- Inherits:
-
Object
- Object
- GovukPublishingComponents::ComponentExample
- Defined in:
- app/models/govuk_publishing_components/component_example.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#embed ⇒ Object
readonly
Returns the value of attribute embed.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #black_background? ⇒ Boolean
- #contains_html?(input) ⇒ Boolean
- #dark_background? ⇒ Boolean
- #data? ⇒ Boolean
- #has_block? ⇒ Boolean
- #has_embed? ⇒ Boolean
- #highlight_code(string_to_format = "") ⇒ Object
- #html_description ⇒ Object
- #html_safe_data ⇒ Object
-
#html_safe_strings(obj) ⇒ Object
Iterate through data object and recursively mark any found string as html_safe.
-
#initialize(id, example) ⇒ ComponentExample
constructor
A new instance of ComponentExample.
- #name ⇒ Object
- #pretty_data ⇒ Object
- #right_to_left? ⇒ Boolean
Constructor Details
#initialize(id, example) ⇒ ComponentExample
Returns a new instance of ComponentExample.
12 13 14 15 16 17 18 19 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 12 def initialize(id, example) @id = id @data = example["data"] || {} @context = example["context"] || {} @description = example["description"] || false @block = @data.delete(:block) || false @embed = example["embed"] end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
5 6 7 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 5 def block @block end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
5 6 7 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 5 def context @context end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 5 def data @data end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 5 def description @description end |
#embed ⇒ Object (readonly)
Returns the value of attribute embed.
5 6 7 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 5 def @embed end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 5 def id @id end |
Instance Method Details
#black_background? ⇒ Boolean
82 83 84 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 82 def black_background? !!context["black_background"] end |
#contains_html?(input) ⇒ Boolean
42 43 44 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 42 def contains_html?(input) ActionController::Base.helpers.(input) != input end |
#dark_background? ⇒ Boolean
78 79 80 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 78 def dark_background? context["dark_background"].present? end |
#data? ⇒ Boolean
46 47 48 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 46 def data? data.any? end |
#has_block? ⇒ Boolean
90 91 92 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 90 def has_block? block.present? end |
#has_embed? ⇒ Boolean
94 95 96 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 94 def .present? end |
#highlight_code(string_to_format = "") ⇒ Object
25 26 27 28 29 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 25 def highlight_code(string_to_format = "") formatter = Rouge::Formatters::HTML.new lexer = Rouge::Lexers::ERB.new formatter.format(lexer.lex(string_to_format)).html_safe end |
#html_description ⇒ Object
86 87 88 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 86 def html_description markdown_to_html(description) if description.present? end |
#html_safe_data ⇒ Object
50 51 52 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 50 def html_safe_data html_safe_strings(data.dup) end |
#html_safe_strings(obj) ⇒ Object
Iterate through data object and recursively mark any found string as html_safe
Safe HTML can be passed to components, simulate by marking any string that comes from YAML as safe
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 59 def html_safe_strings(obj) case obj when String obj.html_safe when Hash obj.each do |key, value| obj[key] = html_safe_strings(value) end when Array obj.map! { |e| html_safe_strings(e) } else obj end end |
#name ⇒ Object
21 22 23 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 21 def name id.humanize end |
#pretty_data ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 31 def pretty_data json_key_regex = /"(\w*)":/ # matches quoted keys ending with a colon, i.e. "key": output = JSON.pretty_generate(data).gsub('\\n', "\n ").gsub(json_key_regex, '\1:') quoted_string_regex = /"((?:[^"\\]|\\.)*)"/ # matches "some text" - ignores escaped quotes, i.e. \" output.gsub(quoted_string_regex) do |group| match = Regexp.last_match[1] contains_html?(match) ? "sanitize(#{group})" : group end end |
#right_to_left? ⇒ Boolean
74 75 76 |
# File 'app/models/govuk_publishing_components/component_example.rb', line 74 def right_to_left? context["right_to_left"].present? end |