Skip to content

Instantly share code, notes, and snippets.

@muhqu
Created June 18, 2014 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhqu/e862362b71fae156ce3e to your computer and use it in GitHub Desktop.
Save muhqu/e862362b71fae156ce3e to your computer and use it in GitHub Desktop.
How to quick'n'dirty parse yml comments as descriptions

Input (sample.en.yml)

---
sample:
  plural-key:
    # This is a sample pluralisation key.
    zero: "There are no monkeys"
    one: "There is one monkey"
    two: "There are two monkeys"
    other: "There are {num} monkeys"
  regular-key: "There are a bunch of monkeys" # This is a sample for a regular key.

Preprocess YAML to convert comments to ...___phrase_description keys entries:

cat sample.en.yml \
  | php -R 'echo preg_replace("/^((\s*\S+)\s*:.*)#(.*)$/","\\1\n\\2___phrase_description: \"\\3\"",
                 preg_replace("/^(\s*)#([^#]+)$/","\\1___phrase_description: \"\\2\"",$argn))."\n";'

Output:

---
sample:
  plural-key:
    ___phrase_description: " This is a sample pluralisation key."
    zero: "There are no monkeys"
    one: "There is one monkey"
    two: "There are two monkeys"
    other: "There are {num} monkeys"
  regular-key: "There are a bunch of monkeys"
  regular-key___phrase_description: " This is a sample for a regular key."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment