2021-09-04 23:15:30 +02:00
|
|
|
struct HeaderData
|
|
|
|
getter title : String
|
|
|
|
getter subtitle : String?
|
|
|
|
|
|
|
|
def initialize(@title : String, @subtitle : String?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_content_paragraph_index : Int32
|
|
|
|
if title.blank?
|
|
|
|
0
|
|
|
|
elsif subtitle.nil? || subtitle.blank?
|
|
|
|
1
|
|
|
|
else
|
|
|
|
2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-14 23:36:10 +02:00
|
|
|
class PageConverter
|
2021-09-04 23:15:30 +02:00
|
|
|
def convert(data : PostResponse::Data) : Page
|
|
|
|
paragraphs = data.post.content.bodyModel.paragraphs
|
|
|
|
author = data.post.creator.name
|
|
|
|
header = header_data(paragraphs)
|
|
|
|
if header.first_content_paragraph_index.zero?
|
|
|
|
content = [] of PostResponse::Paragraph
|
|
|
|
else
|
|
|
|
content = paragraphs[header.first_content_paragraph_index..]
|
|
|
|
end
|
|
|
|
Page.new(
|
|
|
|
title: header.title,
|
|
|
|
subtitle: header.subtitle,
|
|
|
|
author: author,
|
|
|
|
nodes: ParagraphConverter.new.convert(content)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def header_data(paragraphs : Array(PostResponse::Paragraph)) : HeaderData
|
|
|
|
if paragraphs.empty?
|
|
|
|
return HeaderData.new("", nil)
|
|
|
|
end
|
2021-08-14 23:36:10 +02:00
|
|
|
first_two_paragraphs = paragraphs.first(2)
|
|
|
|
first_two_types = first_two_paragraphs.map(&.type)
|
|
|
|
if first_two_types == [PostResponse::ParagraphType::H3, PostResponse::ParagraphType::H4]
|
2021-09-04 23:15:30 +02:00
|
|
|
HeaderData.new(
|
2021-08-14 23:36:10 +02:00
|
|
|
title: first_two_paragraphs[0].text,
|
|
|
|
subtitle: first_two_paragraphs[1].text,
|
|
|
|
)
|
|
|
|
else
|
2021-09-04 23:15:30 +02:00
|
|
|
HeaderData.new(
|
2021-08-14 23:36:10 +02:00
|
|
|
title: first_two_paragraphs[0].text,
|
|
|
|
subtitle: nil,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|