Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you put HTML or XML into a YAML?

I would like to store HTML snippets inside a YAML file. What is the best way to do that?

Something like:

myhtml: |   <div>     <a href="#">whatever</a>   </div> 
like image 344
Piotr Zolnierek Avatar asked Nov 23 '09 19:11

Piotr Zolnierek


People also ask

How convert XML to YAML?

XML To YAML Converter lets you convert XML to YAML online. Select an XML file or Load XML from url or Enter XML data and convert it to YAML. After conversion, you can download converted YAML data to your device.

Is YAML the same as XML?

As such, YAML is defined as a labeled tree structure. It differs from XML in many ways: Only elements are supported, no PI, Comment, Attribute, etc. Element abbreviated syntax, <tag/>, is allowed.

Is YAML HTML?

YAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML. YAML uses Python-style indentation to indicate nesting.


1 Answers

Example

Here is a sample record from a YAML-based snippet management system I created years ago:

- caption:    fieldset msie5   tinycap:    fieldset   domain:     html   desc:       fieldset and legend tag   wwbody: |       <fieldset>       <legend>legend</legend>        </fieldset> 

You can repeat that or something like it for all the snippets you want to manage. This particular system stores the snippets as an array of name-value pairs (Perl people would call this an AoH). If you do not need all this extra information, just two name-value pairs will suffice (e.g., caption + body).

The nice thing about this system: YAML indentation prevents "delimiter collision" problems. You never have to use clumsy escape sequences inside your snippet body.

Text Editor or IDE alternative

Note: Increasingly, text editors and IDEs support flexible snippet management options natively, so you may want to consider using the format of a text editor rather than re-inventing your own. If you do re-invent your own, you can write a script to translate your YAML format into the native format of a text editor if you later decide you want to do that.

See also:

  • http://en.wikipedia.org/wiki/Snippet_%28programming%29
  • http://en.wikipedia.org/wiki/Delimiter#Delimiter_collision
  • http://perldoc.perl.org/perldsc.html#ARRAYS-OF-HASHES (Perl AoH)
  • http://www.perlmonks.org/index.pl/?node_id=347905
like image 150
dreftymac Avatar answered Oct 02 '22 15:10

dreftymac