Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions app/helpers/govspeak_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,6 @@ def prepare_attachments(attachments, alternative_format_contact_email)
end
end

def govspeak_headers(govspeak, level = (2..2))
build_govspeak_document(govspeak).headers.select do |header|
level.cover?(header.level)
end
end

def govspeak_header_hierarchy(govspeak)
headers = []
govspeak_headers(govspeak, 2..3).each do |header|
case header.level
when 2
headers << { header:, children: [] }
when 3
raise Govspeak::OrphanedHeadingError, header.text if headers.none?

headers.last[:children] << header
end
end
headers
end

def inline_attachment_code_tags(number)
tag.code("!@#{number}") <<
" or ".html_safe <<
tag.code("[InlineAttachment:#{number}]")
end

def fraction_image(numerator, denominator)
denominator.downcase! if %w[X Y].include? denominator
if numerator.present? && denominator.present? && asset_exists?("fractions/#{numerator}_#{denominator}.png")
Expand All @@ -108,17 +81,14 @@ def fraction_image(numerator, denominator)
def bare_govspeak_to_html(govspeak, images = [], attachments = [], options = {}, &block)
# pre-processors
govspeak = convert_attachment_syntax(govspeak, attachments)
govspeak = remove_extra_quotes_from_blockquotes(govspeak)
govspeak = render_embedded_contacts(govspeak, options[:contact_heading_tag])
govspeak = render_embedded_fractions(govspeak)
govspeak = set_classes_for_charts(govspeak)

markup_to_nokogiri_doc(govspeak, images, attachments)
.tap { |nokogiri_doc|
# post-processors
replace_internal_admin_links_in(nokogiri_doc, &block)
add_class_to_links(nokogiri_doc)
add_class_to_last_blockquote_paragraph(nokogiri_doc)

case options[:heading_numbering]
when :auto
Expand All @@ -145,10 +115,6 @@ def asset_exists?(path)
Sprockets::Railtie.build_environment(Rails.application).find_asset(path)
end

def remove_extra_quotes_from_blockquotes(govspeak)
Whitehall::ExtraQuoteRemover.new.remove(govspeak)
end

def wrapped_in_govspeak_div(html_string)
tag.div(html_string.html_safe, class: "govspeak")
end
Expand Down Expand Up @@ -177,36 +143,10 @@ def render_embedded_fractions(govspeak)
end
end

def set_classes_for_charts(govspeak)
return govspeak if govspeak.blank?

govspeak.gsub(GovspeakHelper::BARCHART_REGEXP) do
stacked = ".mc-stacked" if Regexp.last_match(1).include? "stacked"
compact = ".compact" if Regexp.last_match(1).include? "compact"
negative = ".mc-negative" if Regexp.last_match(1).include? "negative"

[
"{:",
".js-barchart-table",
stacked,
compact,
negative,
".mc-auto-outdent",
"}",
].join(" ")
end
end

def replace_internal_admin_links_in(nokogiri_doc, &block)
Govspeak::AdminLinkReplacer.new(nokogiri_doc).replace!(&block)
end

def add_class_to_last_blockquote_paragraph(nokogiri_doc)
nokogiri_doc.css("blockquote p:last-child").map do |el|
el[:class] = "last-child"
end
end

def add_class_to_links(nokogiri_doc)
nokogiri_doc.css("a").map do |el|
el[:class] = "govuk-link" unless el[:class] =~ /button/
Expand Down
23 changes: 23 additions & 0 deletions app/models/detailed_guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ def validate(record)
rescue Govspeak::OrphanedHeadingError => e
record.errors.add(:body, "must have a level-2 heading (h2 - ##) before level-3 heading (h3 - ###): '#{e.heading}'")
end

private

def govspeak_headers(govspeak, level)
build_govspeak_document(govspeak).headers.select do |header|
level.cover?(header.level)
end
end

def govspeak_header_hierarchy(govspeak)
headers = []
govspeak_headers(govspeak, 2..3).each do |header|
case header.level
when 2
headers << { header:, children: [] }
when 3
raise Govspeak::OrphanedHeadingError, header.text if headers.none?

headers.last[:children] << header
end
end
headers
end
end

validates_with HeadingHierarchyValidator
Expand Down
2 changes: 0 additions & 2 deletions lib/whitehall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
module Whitehall
autoload :Random, "whitehall/random"
autoload :RandomKey, "whitehall/random_key"
autoload :FormBuilder, "whitehall/form_builder"
autoload :Uploader, "whitehall/uploader"
autoload :ExtraQuoteRemover, "whitehall/extra_quote_remover"
autoload :GovspeakRenderer, "whitehall/govspeak_renderer"

mattr_accessor :content_store
Expand Down
12 changes: 0 additions & 12 deletions lib/whitehall/extra_quote_remover.rb

This file was deleted.

42 changes: 0 additions & 42 deletions test/unit/app/helpers/govspeak_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ class GovspeakHelperTest < ActionView::TestCase
assert_select_within_html html, "a[href='#{public_url}']", text: "that"
end

test "should only extract level two headers by default" do
text = "# Heading 1\n\n## Heading 2\n\n### Heading 3"
headers = govspeak_headers(text)
assert_equal [Govspeak::Header.new("Heading 2", 2, "heading-2")], headers
end

test "should extract header hierarchy from level 2+3 headings" do
text = "# Heading 1\n\n## Heading 2a\n\n### Heading 3a\n\n### Heading 3b\n\n#### Ignored heading\n\n## Heading 2b"
headers = govspeak_header_hierarchy(text)
Expand Down Expand Up @@ -300,20 +294,6 @@ def image_data.image_kind_config = Whitehall::ImageKind.new(
assert_select_within_html html, ".govspeak figure.image.embedded img[src='https://some.cdn.com/image.jpg']"
end

test "should remove extra quotes from blockquote text" do
remover = stub("remover")
remover.expects(:remove).returns("remover return value")
Whitehall::ExtraQuoteRemover.stubs(:new).returns(remover)
edition = build(:published_news_article, body: %(He said:\n> "I'm not sure what you mean!"\nOr so we thought.))
assert_match %r{remover return value}, govspeak_edition_to_html(edition)
end

test "should add class to last paragraph of blockquote" do
input = "\n> firstline\n>\n> lastline\n"
output = '<div class="govspeak"> <blockquote> <p>firstline</p> <p class="last-child">lastline</p> </blockquote></div>'
assert_equivalent_html output, govspeak_to_html(input).gsub(/\s+/, " ")
end

test "adds numbers to h2 headings" do
input = "# main\n\n## first\n\n## second"
output = '<div class="govspeak"><h1 id="main">main</h1> <h2 id="first"> <span class="number">1. </span>first</h2> <h2 id="second"> <span class="number">2. </span>second</h2></div>'
Expand Down Expand Up @@ -389,28 +369,6 @@ def image_data.image_kind_config = Whitehall::ImageKind.new(
end
end

test "will add a barchart class to a marked table" do
input = <<~INPUT
|col|
|---|
|val|
{barchart}
INPUT
html = govspeak_to_html(input)
assert_select_within_html html, "table.js-barchart-table"
end

test "will add a stacked, compact, negative barchart class to a marked table" do
input = <<~INPUT
|col|
|---|
|val|
{barchart stacked compact negative}
INPUT
html = govspeak_to_html(input)
assert_select_within_html html, "table.mc-stacked.js-barchart-table.mc-negative.compact"
end

test "fraction image paths include the public asset host and configured asset prefix" do
prefix = Rails.application.config.assets.prefix
path = "#{Whitehall.public_root}#{prefix}/fractions/1_2.png"
Expand Down
100 changes: 0 additions & 100 deletions test/unit/lib/whitehall/extra_quote_remover_test.rb

This file was deleted.

Loading