lib/mdl/doc.rb
##
# Extracts the text from an element whose children consist of text
# elements and other things
Extracts the text from an element whose children consist of text
elements and other things
Extracts the text from an element whose children consist of text elements and other things
def extract_text(element, prefix = '', restore_whitespace = true)
      quotes = {
        :rdquo => '"',
        :ldquo => '"',
        :lsquo => "'",
        :rsquo => "'",
      }
      lines = element.children.map do |e|
        if e.type == :text
          e.value
        elsif %i{strong em p codespan}.include?(e.type)
          extract_text(e, prefix, restore_whitespace).join("\n")
        elsif e.type == :smart_quote
          quotes[e.value]
        end
      end.join.split("\n")
      lines[0] = element_line(element).sub(prefix, '') if restore_whitespace
      lines
    end
def extract_text ( element , prefix = '' , restore_whitespace = true ) quotes = { :rdquo => '"' , :ldquo => '"' , :lsquo => "'" , :rsquo => "'" , } lines = element . children . map do | e | if e . type == :text e . value elsif %i{ strong em p codespan } . include? ( e . type ) extract_text ( e , prefix , restore_whitespace ) . join ( "\n" ) elsif e . type == :smart_quote quotes [ e . value ] end end . join . split ( "\n" ) lines [ 0 ] = element_line ( element ) . sub ( prefix , '' ) if restore_whitespace lines end
Extracts the text from an element whose children consist of text
elements and other things
Extracts the text from an element whose children consist of text elements and other things
# If anything goes amiss here, e.g. unknown type, then nil will be # returned and we'll just not catch that part of the text, which seems # like a sensible failure mode. # Text blocks have whitespace stripped, so we need to add it back in at # the beginning. Because this might be in something like a blockquote, # we optionally strip off a prefix given to the function.
element
<not_specific>
prefix
<not_specific>
restore_whitespace
<not_specific>