Sha256: eaa6b5e296944ae0fc9a7c79b6bc6d299586b044350973898921798d79182ad1

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

module GovukMarkdown
  class Renderer < ::Redcarpet::Render::HTML
    def table(header, body)
      <<~HTML
        <table class='govuk-table'>
          <thead class='govuk-table__head'>
            #{header}
          </thead>
          <tbody class='govuk-table__body'>
            #{body}
          </tbody>
        </table>
      HTML
    end

    def table_row(content)
      <<~HTML
        <tr class='govuk-table__row'>
          #{content}
        </tr>
      HTML
    end

    def table_cell(content, _alignment)
      <<~HTML
        <td class='govuk-table__cell'>
          #{content}
        </td>
      HTML
    end

    def header(text, header_level)
      heading_size = case header_level
                     when 1 then "xl"
                     when 2 then "l"
                     when 3 then "m"
                     else "s" end

      id_attribute = @options[:with_toc_data] ? " id=\"#{text.parameterize}\"" : ""

      <<~HTML
        <h#{header_level}#{id_attribute} class="govuk-heading-#{heading_size}">#{text}</h#{header_level}>
      HTML
    end

    def paragraph(text)
      <<~HTML
        <p class="govuk-body-m">#{text}</p>
      HTML
    end

    def list(contents, list_type)
      if list_type == :unordered
        <<~HTML
          <ul class="govuk-list govuk-list--bullet">
            #{contents}
          </ul>
        HTML
      elsif list_type == :ordered
        <<~HTML
          <ol class="govuk-list govuk-list--number">
            #{contents}
          </ol>
        HTML
      else
        raise "Unexpected type #{list_type.inspect}"
      end
    end

    def link(link, title, content)
      title_attribute = title.present? ? " title=\"#{title}\"" : ""
      %(<a href="#{link}" class="govuk-link"#{title_attribute}>#{content}</a>)
    end

    def hrule
      <<~HTML
        <hr class="govuk-section-break govuk-section-break--xl govuk-section-break--visible">
      HTML
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
govuk_markdown-0.3.0 lib/govuk_markdown/renderer.rb