Skip to content

Commit

Permalink
Layout corrects: Whitespace and indentation
Browse files Browse the repository at this point in the history
I have run safe rubocop autocorrect on the repo and, for ease of review,
I have separated whitespace and indentation correction, the safest of
all the corrections.
  • Loading branch information
damiendillon committed Nov 12, 2024
1 parent 36a300c commit 9fc378f
Show file tree
Hide file tree
Showing 34 changed files with 127 additions and 73 deletions.
5 changes: 3 additions & 2 deletions lib/article_json/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def register_element_exporters(exporter, type_class_mapping)
if !type_class_mapping.is_a?(Hash) ||
type_class_mapping.keys.any? { |key| !key.is_a? Symbol } ||
type_class_mapping.values.any? { |value| !value.is_a? Class }
raise ArgumentError, '`type_class_mapping` has to be a Hash with '\
'symbolized keys and classes as values but is '\

raise ArgumentError, '`type_class_mapping` has to be a Hash with ' \
'symbolized keys and classes as values but is ' \
"`#{type_class_mapping.inspect}`"
end

Expand Down
1 change: 0 additions & 1 deletion lib/article_json/elements/heading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ def parse_hash(hash)
end
end
end

1 change: 0 additions & 1 deletion lib/article_json/elements/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,3 @@ def parse_hash(hash)
end
end
end

1 change: 0 additions & 1 deletion lib/article_json/elements/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ def parse_hash(hash)
end
end
end

2 changes: 1 addition & 1 deletion lib/article_json/elements/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def blank?
# @return [Integer]
def length
return 0 if empty?

@content.reduce(0) do |sum, element|
sum + (element.respond_to?(:length) ? element.length : 0)
end
Expand All @@ -52,4 +53,3 @@ def parse_hash(hash)
end
end
end

1 change: 0 additions & 1 deletion lib/article_json/elements/quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ def parse_hash(hash)
end
end
end

2 changes: 1 addition & 1 deletion lib/article_json/elements/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def blank?
# @return [Integer]
def length
return 0 if blank?

content.length
end
alias size length
Expand All @@ -63,4 +64,3 @@ def parse_hash(hash)
end
end
end

1 change: 0 additions & 1 deletion lib/article_json/elements/text_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ def parse_hash(hash)
end
end
end

Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,3 @@ def custom_element_script_mapping(custom_element_tag)
end
end
end

72 changes: 42 additions & 30 deletions lib/article_json/export/amp/elements/embed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,61 +42,73 @@ def embedded_object

# @return [Nokogiri::XML::Element]
def youtube_node
create_element('amp-youtube',
'data-videoid' => @element.embed_id,
width: default_width,
height: default_height)
create_element(
'amp-youtube',
'data-videoid' => @element.embed_id,
width: default_width,
height: default_height
)
end

# @return [Nokogiri::XML::Element]
def vimeo_node
create_element('amp-vimeo',
'data-videoid' => @element.embed_id,
width: default_width,
height: default_height)
create_element(
'amp-vimeo',
'data-videoid' => @element.embed_id,
width: default_width,
height: default_height
)
end

# @return [Nokogiri::XML::Element]
def tweet_node
# The embed_id of a tweet is stored as "<handle>/<tweet_id>" but
# the `amp-twitter` tag only takes the `tweet_id` part
tweet_id = @element.embed_id.split('/').last
create_element('amp-twitter',
'data-tweetid': tweet_id,
width: default_width,
height: default_height)
create_element(
'amp-twitter',
'data-tweetid': tweet_id,
width: default_width,
height: default_height
)
end

# @return [Nokogiri::XML::Element]
def facebook_node
url = "#{@element.oembed_data[:author_url]}/videos/#{@element.embed_id}"
create_element('amp-facebook',
'data-embedded-as' => 'video',
'data-href' => url,
width: default_width,
height: default_height)
create_element(
'amp-facebook',
'data-embedded-as' => 'video',
'data-href' => url,
width: default_width,
height: default_height
)
end

def soundcloud_node
src = Nokogiri::HTML(@element.oembed_data[:html])
.xpath('//iframe/@src').first.value
track_id = src.match(/tracks%2F(\d+)/)[1]
create_element('amp-soundcloud',
layout: 'fixed-height',
'data-trackid': track_id,
'data-visual': true,
width: 'auto',
height: default_height)
.xpath('//iframe/@src').first.value
track_id = src.match(%r{tracks%2F(\d+)})[1]
create_element(
'amp-soundcloud',
layout: 'fixed-height',
'data-trackid': track_id,
'data-visual': true,
width: 'auto',
height: default_height
)
end

# @return [Nokogiri::XML::Element]
def iframe_node
node = Nokogiri::HTML(@element.oembed_data[:html]).xpath('//iframe')
create_element('amp-iframe',
src: node.attribute('src').value,
width: node.attribute('width').value,
height: node.attribute('height').value,
frameborder: '0',)
create_element(
'amp-iframe',
src: node.attribute('src').value,
width: node.attribute('width').value,
height: node.attribute('height').value,
frameborder: '0'
)
end

# @return [String]
Expand Down
12 changes: 7 additions & 5 deletions lib/article_json/export/amp/elements/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class Image < Base

# @return [Nokogiri::HTML::NodeSet]
def image_node
create_element('amp-img',
src: @element.source_url,
width: default_width,
height: default_height,
layout: :responsive)
create_element(
'amp-img',
src: @element.source_url,
width: default_width,
height: default_height,
layout: :responsive
)
end

def default_width
Expand Down
6 changes: 4 additions & 2 deletions lib/article_json/export/amp/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ class Exporter
# @return [Array[Symbol]]
def custom_element_tags
return @custom_element_tags if defined? @custom_element_tags

@custom_element_tags =
element_exporters
.flat_map { |element| element.custom_element_tags }
.uniq
.flat_map { |element| element.custom_element_tags }
.uniq
end

# Return an array with all the javascript libraries needed for some
# special AMP tags (like amp-facebook or amp-iframe)
# @return [Array<String>]
def amp_libraries
return @amp_libraries if defined? @amp_libraries

@amp_libraries =
CustomElementLibraryResolver.new(custom_element_tags).script_tags
end
Expand Down
2 changes: 1 addition & 1 deletion lib/article_json/export/apple_news/elements/embed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def caption_text
def text
@element.caption.map do |child_element|
text_exporter.new(child_element)
.export
.export
end.join
end

Expand Down
3 changes: 2 additions & 1 deletion lib/article_json/export/apple_news/elements/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def export
end

private

# Image
# @return [Hash]
def image
Expand Down Expand Up @@ -48,7 +49,7 @@ def caption_text
def text
@element.caption.map do |child_element|
text_exporter.new(child_element)
.export
.export
end.join
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/article_json/export/apple_news/elements/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def text_exporter
def text
@element.content.map do |child_element|
text_exporter.new(child_element)
.export
.export
end.join
end
end
Expand Down
17 changes: 15 additions & 2 deletions lib/article_json/export/apple_news/elements/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ class Text < Base
include ArticleJSON::Export::Common::HTML::Elements::Base
include ArticleJSON::Export::Common::HTML::Elements::Text

UNSUPPORTED_HTML_TAGS = %w[title meta script noscript style link applet object iframe
noframes form select option optgroup
UNSUPPORTED_HTML_TAGS = %w[
title
meta
script
noscript
style
link
applet
object
iframe
noframes
form
select
option
optgroup
].freeze

# A Nokogiri object is returned with`super`, which is is then
Expand Down
2 changes: 1 addition & 1 deletion lib/article_json/export/apple_news/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def to_json
private

# Generate an array with the plain text representation of all elements
#
#
# @return [Array]
def components
@components ||=
Expand Down
3 changes: 2 additions & 1 deletion lib/article_json/export/common/html/elements/embed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ def export
private

def embed_node
type = @element.embed_type.to_s.tr('_','-')
type = @element.embed_type.to_s.tr('_', '-')
create_element(:div, class: "embed #{type}") do |div|
div.add_child(embedded_object)
end
end

def embedded_object
return unavailable_node unless @element.oembed_data

Nokogiri::HTML.fragment(@element.oembed_data[:html])
end

Expand Down
3 changes: 2 additions & 1 deletion lib/article_json/export/common/html/elements/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def export
# @return [Nokogiri::XML::NodeSet]
def figure_node
create_element(:figure, node_opts) do |figure|
node = @element&.href ? href_node : image_node
node = @element&.href ? href_node : image_node
figure.add_child(node)
if @element.caption&.any?
figure.add_child(caption_node(:figcaption))
Expand All @@ -42,6 +42,7 @@ def href_node
# @return [Hash]
def node_opts
return if floating_class.nil?

{ class: floating_class }
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/article_json/export/common/html/elements/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def export
return bold_and_italic_node if @element.bold && @element.italic
return bold_node if @element.bold
return italic_node if @element.italic

content_node
end

Expand Down Expand Up @@ -38,6 +39,7 @@ def bold_and_italic_node
# @return [Nokogiri::XML::NodeSet]
def content_node
return create_text_nodes(@element.content) if @element.href.nil?

create_element(:a, href: @element.href) do |a|
a.add_child(create_text_nodes(@element.content))
end
Expand Down
1 change: 1 addition & 0 deletions lib/article_json/import/google_doc/html/embedded_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def parsers
def find_parser(text)
text = text.strip.downcase
return nil if text.empty?

parsers.find { |klass| klass.matches?(text) }
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/article_json/import/google_doc/html/heading_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def content
# @return [Integer]
def level
case @node.name
when 'h1' then 1
when 'h2' then 2
when 'h3' then 3
when 'h4' then 4
when 'h5' then 5
when 'h1' then 1
when 'h2' then 2
when 'h3' then 3
when 'h4' then 4
when 'h5' then 5
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/article_json/import/google_doc/html/image_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def float
# @return [String]
def href
return if @caption_node.nil?

match = @caption_node.content.strip.match(href_regexp)
return if match.nil?

remove_image_link_tag
match[:url]
end
Expand Down
4 changes: 2 additions & 2 deletions lib/article_json/import/google_doc/html/list_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def initialize(node:, css_analyzer:)
# @return [Symbol]
def list_type
case @node.name
when 'ol' then :ordered
when 'ul' then :unordered
when 'ol' then :ordered
when 'ul' then :unordered
end
end

Expand Down
Loading

0 comments on commit 9fc378f

Please sign in to comment.