Module: Coradoc::Markdown::ParserUtil::IalParser
- Defined in:
- lib/coradoc/markdown/parser_util.rb
Overview
Parser for IAL (Inline Attribute List) syntax
IAL syntax: #id key=“value” Supports:
-
Classes: .classname or .-classname
-
IDs: #idname
-
Key-value pairs: key=“value”, key=‘value’, or key=value
Class Method Summary collapse
-
.parse_to_hash(content) ⇒ Hash
Parse IAL content into a hash.
-
.tokenize(content) ⇒ Array<Hash>
Tokenize an IAL string into its components.
Class Method Details
.parse_to_hash(content) ⇒ Hash
Parse IAL content into a hash
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/coradoc/markdown/parser_util.rb', line 48 def self.parse_to_hash(content) result = { id: nil, classes: [], attributes: {} } return result if content.nil? || content.empty? tokens = tokenize(content) tokens.each do |token| case token[:type] when :class result[:classes] << token[:value] when :id result[:id] = token[:value] when :attribute result[:attributes][token[:key]] = token[:value] end end result end |
.tokenize(content) ⇒ Array<Hash>
Tokenize an IAL string into its components
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/coradoc/markdown/parser_util.rb', line 21 def self.tokenize(content) tokens = [] scanner = StringScanner.new(content.to_s) until scanner.eos? scanner.skip(/\s+/) break if scanner.eos? if scanner.scan(/\.(-?\w[\w-]*)/) tokens << { type: :class, value: scanner[1] } elsif scanner.scan(/#(\w[\w-]*)/) tokens << { type: :id, value: scanner[1] } elsif scanner.scan(/(\w[\w-]*)\s*=\s*/) key = scanner[1] value = extract_quoted_value(scanner, handle_escapes: true) tokens << { type: :attribute, key: key, value: value } elsif scanner.scan(/\S+/) # Skip unknown tokens end end tokens end |