Parsing key:value note-tags

● ARCHIVED · READ-ONLY
Started by Tsukihime 8 posts View original ↗
  1. This is a note-tag that I enjoy using

    <notetag>option1: value1option2: value2option3: value3</notetag>How would you parse this so that1. Options are optional

    2. Values can span multiple lines

    Examples

    Code:
    <notetag>name: himeclass: empressdescription: some really long descriptionthat spans multiple lines and should beparsed as one thing. New-lines should beinserted explicitly using \n in the tag</notetag>
    Perhaps additional characters should be added to the note-tag to enforce structure and to be able to distinguish between keys and values? Maybe just use hash notation and eval the whole thing as a hash and let ruby deal with the parsing?
  2. That is partially the motivation behind the format since it's easy to write and not verbose (don't have to worry about braces and commas and all the other syntax stuff).


    If there was a YAML parser compatible with RM that might work.
  3. If you had a clearer beginning and ending for each option then this would be a lot easier.

    Maybe something like <option: value_possible_spanning_multiple_lines>?

    Or a comma/semi-colon/whatever separating the options?

    Then it's a simple matter of either using a regular expression, or splitting the string at the separator.
  4. IMP1 said:
    If you had a clearer beginning and ending for each option then this would be a lot easier.

    Maybe something like <option: value_possible_spanning_multiple_lines>?

    Or a comma/semi-colon/whatever separating the options?

    Then it's a simple matter of either using a regular expression, or splitting the string at the separator.
    So for example something like

    <notetag> <name: hime> <class: empress> <description: some really long description that spans multiple lines and should be parsed as one thing. New-lines should be inserted explicitly using \n in the tag></notetag>I like it. Since they're using angle braces already it shouldn't be too much of an issue to add a few more.One of the problems I ran into was formulas.

    Code:
    <notetag>  <condition: a.hp > 20 && a.hp < 40 >  <class ... ></notetag>
    I'm not sure how to write a regex to parse that correctly! :(
  5. The easiest solution I could think of would be wrapping such sequences in special characters like quotes, braces or square brackets or explicitely escape the additional > characters.

    When you only allow inherent text within your notetags, you probably could also look ahead if the start of a new tag is follwing the >, since it's not impossible, but quite uncommon to use >< in a ruby expression:

    /<formula:(.*?)>(?=\s*<)/m

    (Didn't test this example)

    EDIT: While I rarely work with regular expressions I tried to work out a method for it, although it's far from perfect:

    Spoiler
    Code:
    class String  def extract_item_note(tag, features)    # Create regular expression:    feature_patterns = features.map { |name| "(?:#{ name }\\s??<#{ name }>.*?))" }    regexp = /<#{ tag }>\s*(?:<(?:#{ feature_patterns.join("|") }|(?:.?*))>\s*)*<\/#{ tag }>/im    # Match note:    match = self.match(regexp)    return match == nil ? nil : features.map { |name| match[name] && match[name].strip }  endendnote = "This is a notebox \n" \       "<notetag>\n" \       "  <name: Heinz Reiher> \n" \       "  <condition: 25 > 36 and 48 is my favorite number >:-)> \n" \       "<name: Her Mann Kuntz> \n" \       "</notetag>"p note.extract_item_note("notetag", ["name", "class", "condition"])
  6. I'm not sure how to write a regex to parse that correctly!
    possibly just do another <start><end> kind of tag for formulas

    so that would be

    Code:
    <notetag>  <condition>     a.hp > 20 && a.hp < 40  </condition>  <class ... ></notetag>
  7. Engr. Adiktuzmiko said:
    possibly just do another <start><end> kind of tag for formulas

    so that would be

    <notetag> <condition> a.hp > 20 && a.hp < 40 </condition> <class ... ></notetag>
    Or just end the closing tag with a forward slash (or something else), like

    Code:
    <condition: a.hp > 20 && a.hp < 40 />