def each_section(file)   
      title = 'index' # Default to 'index' if no title given
      text = ''

      # TODO This could be more efficient by applying regular expressions to the whole file, finding
      # breakpoints and processing them independently. The naive approach will do for now
      while line = file.gets do
        marker = line.match(/((^([*]{3}\s)|^([*]\s){3})|^([*]{5}\s)|^([-]\s){3}|^([-]{39}))(?<title>.*)/)

        if marker
          yield title, text if !text.empty? 
          # start next section
          text = ''
          title = marker[:title].chomp
        else
          text += line
        end    
      end
      yield title, text if !text.empty? # Process any remaining text as a single section  
    end
def each_section(file)   
      title = 'index' # Default to 'index' if no title given
      text = ''

      # TODO This could be more efficient by applying regular expressions to the whole file, finding
      # breakpoints and processing them independently. The naive approach will do for now
      while line = file.gets do
        marker = line.match(/((^([*]{3}\s)|^([*]\s){3})|^([*]{5}\s)|^([-]\s){3}|^([-]{39}))(?<title>.*)/)

        if marker
          yield title, text if !text.empty? 
          # start next section
          text = ''
          title = marker[:title].chomp
        else
          text += line
        end    
      end
      yield title, text if !text.empty? # Process any remaining text as a single section  
    end
Utility methods