Sha256: e6490c19d4ac12ea594d1afc1a0874c46ab7e4b4e8e785e7e5a0ec8584b11ca7

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module GovukMarkdown
  class Preprocessor
    attr_reader :output

    def initialize(document)
      @output = document
    end

    def inject_inset_text
      output.gsub!(build_regexp("inset-text")) do
        <<~HTML
          <div class="govuk-inset-text">
            #{Regexp.last_match(1)}
          </div>
        HTML
      end
      self
    end

    def inject_details
      output.gsub!(build_regexp("details")) do
        summary, details = *construct_details_from(Regexp.last_match(1))

        <<~HTML
          <details class="govuk-details" data-module="govuk-details">
            <summary class="govuk-details__summary">
              <span class="govuk-details__summary-text">
              #{summary}
              </span>
            </summary>
            <div class="govuk-details__text">
              #{details}
            </div>
          </details>
        HTML
      end
      self
    end

  private

    def build_regexp(tag_name, pre_tag: "{", post_tag: "}", closing: "/")
      start_tag = pre_tag + tag_name + post_tag
      end_tag = pre_tag + closing + tag_name + post_tag
      pattern = [Regexp.quote(start_tag), "(.*?)", Regexp.quote(end_tag)].join

      Regexp.compile(pattern, Regexp::EXTENDED | Regexp::MULTILINE)
    end

    def construct_details_from(match_string, partition_characters: %w[? .])
      summary_text, match, details = match_string.partition(Regexp.union(*partition_characters))

      summary = [summary_text, format_punctuation(match)].compact.join

      [summary, details].compact.map(&:strip)
    end

    def format_punctuation(match)
      return if match.include?(".")

      match
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
govuk_markdown-2.0.0b1 lib/govuk_markdown/preprocessor.rb