このコミットが含まれているのは:
@@ -0,0 +1,114 @@
|
||||
class TagGroupMarkdownRenderer
|
||||
class UnsupportedStructure < StandardError
|
||||
;
|
||||
end
|
||||
|
||||
def self.render group
|
||||
new(group).render
|
||||
end
|
||||
|
||||
def initialize group
|
||||
@group = group
|
||||
end
|
||||
|
||||
def render
|
||||
placements = @group.fetch('placements')
|
||||
validate_membership!(placements)
|
||||
blocks = []
|
||||
blocks << clean_text(@group['description']) if @group['description'].present?
|
||||
|
||||
sections = placements.select { _1.key?('section_name') }
|
||||
if sections.any? && sections.size != placements.size
|
||||
raise UnsupportedStructure,
|
||||
'最上位の見出し Section と Placement の混在は Markdown にできません.'
|
||||
end
|
||||
|
||||
if placements.all? { _1.key?('section_name') }
|
||||
blocks.concat(placements.map { render_heading(_1, 1) })
|
||||
else
|
||||
blocks << render_list(placements, 0)
|
||||
end
|
||||
|
||||
blocks.compact.reject(&:empty?).join("\n\n").sub(/\n+\z/, '')
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_membership! placements
|
||||
placed = placement_members(placements).uniq.sort
|
||||
members = @group.fetch('members').map do |member|
|
||||
if member['tag']
|
||||
['tag', member.fetch('tag').fetch('id')]
|
||||
else
|
||||
['tag_group', member.fetch('tag_group').fetch('id')]
|
||||
end
|
||||
end.uniq.sort
|
||||
missing = members - placed
|
||||
raise UnsupportedStructure, '掲載位置のない membership は Markdown にできません.' if missing.any?
|
||||
end
|
||||
|
||||
def placement_members nodes
|
||||
nodes.flat_map do |node|
|
||||
own = if node['tag']
|
||||
[['tag', node.fetch('tag').fetch('id')]]
|
||||
elsif node['tag_group']
|
||||
[['tag_group', node.fetch('tag_group').fetch('id')]]
|
||||
else
|
||||
[]
|
||||
end
|
||||
own + placement_members(node.fetch('children'))
|
||||
end
|
||||
end
|
||||
|
||||
def render_heading section, level
|
||||
header = "#{ '#' * level } #{ clean_text(section.fetch('section_name')) }"
|
||||
blocks = [header]
|
||||
blocks << clean_text(section['description']) if section['description'].present?
|
||||
children = section.fetch('children')
|
||||
section_indexes = children.each_index.select { |index| children[index].key?('section_name') }
|
||||
if section_indexes.any?
|
||||
first_section = section_indexes.first
|
||||
if children[first_section..].any? { !_1.key?('section_name') }
|
||||
raise UnsupportedStructure,
|
||||
'見出し Section と兄弟 Placement の交互配置は Markdown にできません.'
|
||||
end
|
||||
list_nodes = children[0...first_section]
|
||||
blocks << render_list(list_nodes, 0) if list_nodes.any?
|
||||
blocks.concat(children[first_section..].map { render_heading(_1, level + 1) })
|
||||
elsif children.any?
|
||||
blocks << render_list(children, 0)
|
||||
end
|
||||
blocks.join("\n\n")
|
||||
end
|
||||
|
||||
def render_list nodes, depth
|
||||
nodes.map do |node|
|
||||
indent = ' ' * depth
|
||||
if node.key?('section_name')
|
||||
raise UnsupportedStructure,
|
||||
'リスト内 Section の説明文は Markdown にできません.' if node['description'].present?
|
||||
|
||||
children = node.fetch('children')
|
||||
head = "#{ indent }- #{ clean_text(node.fetch('section_name')) }:"
|
||||
tail = render_list(children, depth + 1) if children.any?
|
||||
[head, tail].compact.join("\n")
|
||||
else
|
||||
label = if node['tag']
|
||||
node.fetch('tag').fetch('name')
|
||||
else
|
||||
"#{ TagGroupRepr::PREFIX }#{ node.fetch('tag_group').fetch('name') }"
|
||||
end
|
||||
raise UnsupportedStructure, '名称のない Placement は Markdown にできません.' if label.blank?
|
||||
|
||||
head = "#{ indent }- #{ clean_text(label) }"
|
||||
children = node.fetch('children')
|
||||
tail = render_list(children, depth + 1) if children.any?
|
||||
[head, tail].compact.join("\n")
|
||||
end
|
||||
end.join("\n")
|
||||
end
|
||||
|
||||
def clean_text value
|
||||
value.to_s.gsub(/\r\n?/, "\n").lines.map(&:rstrip).join("\n").strip
|
||||
end
|
||||
end
|
||||
新しいイシューから参照
ユーザーをブロックする