X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/097a414b22a9068cd9af6ec758eb2876da3dd654..aa8dd75e5ec959e377f469a83bf67898472a67e5:/lib/rich_text.rb diff --git a/lib/rich_text.rb b/lib/rich_text.rb index f554e5ab5..b720f33e7 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -109,6 +109,11 @@ module RichText @image_element.attr["alt"] if @image_element end + def description + @paragraph_element = first_paragraph_element(document.root) unless defined? @paragraph_element + text_content(@paragraph_element) if @paragraph_element + end + private def document @@ -124,9 +129,37 @@ module RichText end end + def first_paragraph_element(element) + return element if paragraph?(element) + + element.children.find do |child| + nested_paragraph = first_paragraph_element(child) + break nested_paragraph if nested_paragraph + end + end + + def text_content(element) + text = "" + + append_text = lambda do |child| + if child.type == :text + text << child.value + else + child.children.each { |c| append_text.call(c) } + end + end + append_text.call(element) + + text + end + def image?(element) element.type == :img || (element.type == :html_element && element.value == "img") end + + def paragraph?(element) + element.type == :p + end end class Text < Base