Skip to content

Commit

Permalink
Separate element definition and parsing: List
Browse files Browse the repository at this point in the history
Extract general stuff into element class that is supposed
to be re-used by other import / export modules.
  • Loading branch information
dsager committed Sep 13, 2017
1 parent f3490c2 commit d2091c9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/article_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative 'article_json/elements/text'
require_relative 'article_json/elements/heading'
require_relative 'article_json/elements/paragraph'
require_relative 'article_json/elements/list'

require_relative 'article_json/import/google_doc/html/css_analyzer'
require_relative 'article_json/import/google_doc/html/node_analyzer'
Expand Down
26 changes: 26 additions & 0 deletions lib/article_json/elements/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module ArticleJSON
module Elements
class List
attr_reader :type, :content, :list_type

# @param [Array[ArticleJSON::Elements::Paragraph]] content
# @param [Symbol] list_type
def initialize(content:, list_type: :unordered)
@type = :list
@content = content
@list_type = list_type
end

# Hash representation of this heading element
# @return [Hash]
def to_h
{
type: type,
list_type: list_type,
content: content.map(&:to_h),
}
end
end
end
end

14 changes: 6 additions & 8 deletions lib/article_json/import/google_doc/html/list_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def list_type
end

# Parse the list's sub nodes to get a set of paragraphs
# @return [Array[ArticleJSON::Import::GoogleDoc::HTML::ParagraphParser]]
# @return [Array[ArticleJSON::Elements::Paragraph]]
def content
@node
.children
Expand All @@ -32,14 +32,12 @@ def content
end
end

# Hash representation of this list
# @return [Hash]
def to_h
{
type: :list,
# @return [ArticleJSON::Elements::List]
def element
ArticleJSON::Elements::List.new(
list_type: list_type,
content: content.map(&:to_h),
}
content: content
)
end
end
end
Expand Down
11 changes: 5 additions & 6 deletions lib/article_json/import/google_doc/html/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,18 @@ def parse_heading
HeadingParser.new(node: @current_node.node).element
end

# @return [ArticleJSON::Import::GoogleDoc::HTML::ParagraphParser]
# @return [ArticleJSON::Elements::Paragraph]
def parse_paragraph
ParagraphParser
.new(node: @current_node.node, css_analyzer: @css_analyzer)
.element
end

# @return [ArticleJSON::Import::GoogleDoc::HTML::ListParser]
# @return [ArticleJSON::Elements::List]
def parse_list
ListParser.new(
node: @current_node.node,
css_analyzer: @css_analyzer
)
ListParser
.new(node: @current_node.node, css_analyzer: @css_analyzer)
.element
end

# @return [ArticleJSON::Import::GoogleDoc::HTML::ImageElement]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def parse_sub_node(node)
.new(node: node, css_analyzer: @css_analyzer)
.element
when :list
ListParser.new(node: node, css_analyzer: @css_analyzer)
ListParser.new(node: node, css_analyzer: @css_analyzer).element
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/article_json/elements/list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
describe ArticleJSON::Elements::List do
subject(:element) { described_class.new(**params) }
let(:params) { { content: [content], list_type: :ordered } }
let(:content) do
ArticleJSON::Elements::Paragraph.new(
content: [ArticleJSON::Elements::Text.new(content: 'Foo Bar')]
)
end

describe '#to_h' do
subject { element.to_h }
it { should be_a Hash }
it { should eq params.merge(type: :list, content: [content.to_h]) }
end
end
16 changes: 8 additions & 8 deletions spec/article_json/import/google_doc/list_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@
end
end

describe 'to_h' do
subject { element.to_h }
describe '#element' do
subject { element.element }
let(:list_tag) { 'ol' }

it 'returns a proper Hash' do
expect(subject).to be_a Hash
expect(subject[:type]).to eq :list
expect(subject[:list_type]).to eq :ordered
expect(subject[:content]).to be_an Array
expect(subject[:content]).to all(be_a Hash)
it 'returns a proper list element' do
expect(subject).to be_a ArticleJSON::Elements::List
expect(subject.type).to eq :list
expect(subject.list_type).to eq :ordered
expect(subject.content).to be_an Array
expect(subject.content).to all be_a ArticleJSON::Elements::Paragraph
end
end
end
6 changes: 2 additions & 4 deletions spec/article_json/import/google_doc/text_box_element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@
expect(subject).to be_an Array
expect(subject.size).to eq 2

expect(subject[0])
.to be_a ArticleJSON::Elements::Heading
expect(subject[0]).to be_a ArticleJSON::Elements::Heading
expect(subject[0].content).to eq 'Text box including a list!'

expect(subject[1])
.to be_a ArticleJSON::Import::GoogleDoc::HTML::ListParser
expect(subject[1]).to be_a ArticleJSON::Elements::List
end
end
end
Expand Down

0 comments on commit d2091c9

Please sign in to comment.