Code snippets/blocks within YFM variables - yaml

I'm trying to take a code snippet from Litmus to use within my Assemble.io project (HTML emails). A typical code block looks like this:
<style>#media print{ #_t { background-image: url('https://0me4e2bg.emltrk.com/0me4e2bg?p&d=%%Email%%');}} div.OutlookMessageHeader {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')} table.moz-email-headers-table {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')} blockquote #_t {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')} #MailContainerBody #_t {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')}</style><div id="_t"></div>
<img src="https://0me4e2bg.emltrk.com/0me4e2bg?d=%%Email%%" width="1" height="1" border="0" />
Ideally, I would love to just stick this whole thing into a YFM variable, which I've tried unsuccessfully. I believe the parser is getting stuck on the #, the quotes, the curly braces, or any combination of the above. I've tried wrapping the code block in '', "", ``, and ```, none of which work. Right now I've taken the variable part of that block (in this case, 0me4e2bg) and used just that in my YFM, which works well enough but I'm sure using CSS and HTML blocks/snippets in YFM has happened to someone else and I'm curious if there is a solution? Is it just that I'm not escaping it properly? Thanks!
EDIT: After trying the answer suggested by Anthon, I get the following error
can not read an implicit mapping pair; a colon is missed
which looks like it's triggered by the # in #media?

A scalar in YAML doesn't need quotes unless it has special characters, and in your case it does. Quoted scalars can use single quotes in which existing single quotes would need to be repeated, or double quotes in which you can use backslash escapes.
If you want your string as is using literal block quoting is most of the best approach, the only thing that has problems with is starting whitespace and end-of-line blanks (i.e. before the newline). You should be able to assign your code block to the variable code as follows:
---
title: YAML Front Matter
description: A very simple way to add structured data to a page.
code: |
<style>#media print{ #_t { background-image: url('https://0me4e2bg.emltrk.com/0me4e2bg?p&d=%%Email%%');}} div.OutlookMessageHeader {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')} table.moz-email-headers-table {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')} blockquote #_t {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')} #MailContainerBody #_t {background-image:url('https://0me4e2bg.emltrk.com/0me4e2bg?f&d=%%Email%%')}</style><div id="_t"></div>
<img src="https://0me4e2bg.emltrk.com/0me4e2bg?d=%%Email%%" width="1" height="1" border="0" />
---
<h1> {{ title }} </h1>
You have to make sure the indentation under code is consistent, which is normally easier than parsing the string for characters to escape.
You can e.g. check online that the first part is valid YAML.

Related

Regexp to convert tags (similar to BBCode) to HTML

I have set of strings with nested [quote] tags in following format:
[quote name="John"]Some text. [quote name="Piter"]Inner quote.[/quote][/quote]
As you see it is not like ordinary BBCode. So I can't find a suitable regexp for gsub in Ruby to convert them to strings like this:
<blockquote>
<p>Some text.
<blockquote>
<p>Inner quote.</p>
<small>Piter</small>
</blockquote>
</p>
<small>John</small>
</blockquote>
Can anybody please help me with such regexp?
I'm pretty sure that regexes fundamentally can't cope with nesting. What you could do is make it do a minimal match (e.g. only the inner quote levels), replace them, and then repeat as long as you have more matches. Once you've replaced a level it will just be HTML so will not match the regex any more.

How can I write two separate blockquotes in sequence using markdown?

I need to place two blockquotes in sequence, however markdown combines them into a single blockquote. The only way I can get them to separate is placing some junk text between them. As this textfield allows me to use Markdown I can demonstrate:
> First Quote
> Second Quote
Results in:
First Quote
Second Quote
While using junk text:
> First Quote
.
> Second Quote
Results in:
First Quote
.
Second Quote
I cannot use HTML tags or HTML entities. Only Markdown.
You can separate blockquotes without html elements by using comment markup <!-- --> with an extra whiteline between the blocks:
> Imagination is more important than knowledge.
<!-- -->
> Never think of the future. It comes soon enough.
<!-- -->
> Anyone who has never made a mistake has never tried anything new.
Of course you can use any HTML elements you like as well (as noted by #pepoloan):
> Imagination is more important than knowledge.
<div></div>
> Never think of the future. It comes soon enough.
<div></div>
> Anyone who has never made a mistake has never tried anything new.
An empty header, #, followed by a space also works.
> Hello World
#
> Goodbye world
Unfortunately it isn't much better than <!-- --> But I kind of like it more, and use it.
>If I try a non breaking space
>it line breaks; don't ask me how
Result:
If I try a non breaking space
it line breaks; don't ask me how
Try this:
Use a U+200B character in-between the quotes, which is defined as a zero-length space to break up the quotes.
> Quote
​
> Quote
It looks like this:
> Quote
​
> Quote
You could use pandoc, which parses your original input as two consecutive blockquotes.
From experimenting, I found these to work also. Note they are not legal HTML or Markdown, but they seem do to the job:
<!
<$
</
<a
One way to do so is by just adding </> after first blockquote followed by a newline. And by this way nothing gets printed as well.
First quote
Second Quote
You have something like your first block quote, then 2 enters and on the second enter give a # symbol for Heading but leave the heading blank and then you next quote in next line.
This is one quote
This is another quote

Ruby Regex to replace all occurrences EXCEPT if an HTML attribute

I need to wrap all instances of %{ ... %} with <span code='notranslate'>...</span> UNLESS the %{ ... } appears within an HTML tag. For example, this:
"Or %{register_text} for a new account by <a href='%{path}'>clicking here</a>."
needs to become this
"Or <span code='notranslate'>%{register_text}</span> for a new account by <a href='%{path}'>clicking here</a>."
my current regex doesn't take into account the HTML tag situation:
x.gsub(/[?<!]%\{([a-zA-Z0-9_\-]*)\}[?>!]/i) {|s| "<span class='notranslate'>#{s}</span>"}
so I am wondering how to do this in Ruby with regex.
Any takers?
I am not sure about the input space, so this is the best that I can come up with. I also clean up the regex a bit along the way.
/%\{[\w-]+\}(?![^<>]>)/
For a well-formed HTML, it will only match tokens that are outside tag. If the HTML is malformed, I don't think I'm up to the task to write the regex.
I also assume that there is no embedded Javascript in the page, since > and < in Javascript is not escaped.

Space in element attribute in xpath

We have this XPath defined in a YML file:
message_form_show: //div[#class='cart']/div[#class='message']/form[#id='message-form' and not contains(#style, 'display:none')]
However we'd like to change to 'display: none' instead, but when adding the space, Netbeans complains saying the YML is not parsed correctly and running the Selenium test also fails on it.
Any ideas how this could be addressed?
The problem is that after the change, YAML parses display: (with colon followed by a space) as a new array which contains none')]. This usually enables YAML to contain e.g. lists of arrays of arrays, but in this situation, it makes things go wrong.
The solution is to specifically delimit the string as a block so YAML won't try to resolve it:
message_form_show: >
//div[#class='cart']/div[#class='message']/form[#id='message-form' and not contains(#style, 'display: none')]
or just quote it with double quotes:
message_form_show: "//div[#class='cart']/div[#class='message']/form[#id='message-form' and not contains(#style, 'display: none')]"

regex selection

I have a string like this.
<p class='link'>try</p>bla bla</p>
I want to get only <p class='link'>try</p>
I have tried this.
/<p class='link'>[^<\/p>]+<\/p>/
But it doesn't work.
How can I can do this?
Thanks,
If that is your string, and you want the text between those p tags, then this should work...
/<p\sclass='link'>(.*?)<\/p>/
The reason yours is not working is because you are adding <\/p> to your not character range. It is not matching it literally, but checking for not each character individually.
Of course, it is mandatory I mention that there are better tools for parsing HTML fragments (such as a HTML parser.)
'/<p[^>]+>([^<]+)<\/p>/'
will get you "try"
It looks like you used this block: [^<\/p>]+ intending to match anything except for </p>. Unfortunately, that's not what it does. A [] block matches any of the characters inside. In your case, the /<p class='link'>[^<\/p>]+ part matched <p class='link'>try</, but it was not immediately followed by the expected </p>, so there was no match.
Alex's solution, to use a non-greedy qualifier is how I tend to approach this sort of problem.
I tried to make one less specific to any particular tag.
(<[^/]+?\s+[^>]*>[^>]*>)
this returns:
<p class='link'>try</p>

Resources