Class: Gemtext::Parser
- Inherits:
-
Object
- Object
- Gemtext::Parser
- Defined in:
- lib/gemtext/parser.rb
Instance Method Summary collapse
-
#initialize(io) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Constructor Details
Instance Method Details
#parse ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gemtext/parser.rb', line 11 def parse @io.each do |line| # Always strip lines to handle "\r\n" stripped = line.strip # First check if we're toggling preformatted mode if stripped =~ /^```/ if @preformatted # Closing out the preformatted text @preformatted.content.delete_suffix!("\n") # Remove the extra newline from the last line @document.push @preformatted @preformatted = false elsif stripped == '```' # Starting preformatted text without caption @preformatted = Preformatted[""] else # Starting preformatted text with caption @preformatted = Preformatted["", stripped.delete_prefix('```').strip] end next end # Once we're in a preformatted block add to the block until we # break out of it if @preformatted @preformatted.content << line next end @document.push( case stripped when /^#\s+/ Heading[stripped.delete_prefix('#').strip] when /^##\s+/ SubHeading[stripped.delete_prefix('##').strip] when /^###\s+/ SubSubHeading[stripped.delete_prefix('###').strip] when "" Whitespace[nil] when /^=>/ without_arrow = stripped.delete_prefix('=>').strip pieces = without_arrow.split(/\s+/) url = pieces.first description = pieces.drop(1).join ' ' # TODO: Preserve whitespace in description Link[url, description] when /^\*\s/ ListItem[stripped.delete_prefix('*').strip] when /^>/ Quote[stripped.delete_prefix('>').strip] else Text[line.strip] end ) end @document.dup end |