How should i add this syntax to my html antlr4 grammar? - html

First of all this is an example of what I'm supposed to do
<p cp-for="x in t;i=index"> {{x}}, {{i}} </p>
I need to parse the mustache syntax separately from HTML text
(Which means the comma must not be parsed as html text).
And this is part of the grammar i wrote:
OPEN_MUSTACHE: '{{' -> pushMode (MUSTACHE_SYNTAX)
mode MUSTACHE_SYNTAX;
//there are other tokens describing the syntax but i didn't write it
CLOSE_MUSTACHE: '}}' -> popMode
So if i want to add another mustache tag like in the example, how can i tell the lexer to look ahead and not pop immediately after it sees MUSTACHE_CLOSE?
P.S
this is my first time asking a question on this website;
I apologize if my question is not clear
Edit: i now understand that i got the grammar syntax wrong,
The comma in {{ x }} , {{ i }} is actually plain html text

So if i want to add another mustache tag like in the example, how can i tell the lexer to look ahead and not pop immediately after it sees MUSTACHE_CLOSE?
For the input {{#check}}, {{/check}}, the lexer should create the following tokens:
{{: open token
#: open tag token
check: name/id token
}}: close token
,: plain HTML token
: plain HTML token
{{: open token
/: close tag token
check: name/id token
}}: close token
So you can just pop back to the default mode when you encounter }} inside your MUSTACHE_SYNTAX mode.
And then in your parser, you do something like this:
parser grammar MustacheParser;
options {
tokenVocab=MustacheLexer;
}
template
: template_contents EOF
;
template_contents
: template_part*
;
template_part
: html
| mustache
;
html
: HTML+
;
mustache
: section
| ...
;
section
: '{{' '#' NAME '}}' template_contents '{{' '/' NAME '}}'
;
(Of course the literal tokens, like '{{', '#' etc., are not allowed inside a parser grammar, it's just pseudo code. Replace them with the tokens from your lexer grammar.)

Related

What does a variable written immediately after "href=" do in html?

I can't find a good explanation for this. What is the code after href= doing?
The variable here$ is a URL. The variable result is a directory name.
echo "<h1><a href=$here/$result>$result</a></h1>";
I understand this is html embedded in php. The echo gives it away.
The question is, what is this:
href=$here/$result
I do not recognize this code in html.
Usually href is referring to the url of a source, in this case its url + directory, href is necessary to the <a> tag which represents a link in HTML
This is not HTML, but PHP that write an HTML string. You can place a variable inside double quoted strings. PHP will parse the string and replace the variable by its value.
For example:
$example = "world";
echo "Hello, $example!";
// Outputs: "Hello, world!"
Notice that single quoted strings have not this behaviour.
In the case of your question, $here and $result will be replaced by the variable value, as follow:
$here = "LOCATION"; // just for example purposes
$result = "RESULT"; // just for example purposes
echo "<h1><a href=$here/$result>$result</a></h1>";
// Outputs: "<h1><a href=LOCATION/RESULT>RESULT</a></h1>"
It will output a link (<a>) where the href attribute is the address the link points to. If you don't have spaces or > symbols in the link, it will work without quotation marks ("), but you can as well write it between quotes:
echo "<h1>$result</h1>";
// Outputs: "<h1>RESULT</h1>"
The best way I know to understand the <a> and its href attribute behaviour is to try it in the browser and see it in action by yourself.

Why do browsers parse "<? ?>" as "<!--? ?-->"?

I have a file with following the HTML code:
<p><? comment ?></p>
Curl returns a normal response:
$ curl file:///path/to/the/file.html
<p><? comment ?></p>
But when I parse that response with Firefox 69 or Chrome 77, nothing is shown to me, because the HTML code is as follows:
<html><head></head><body><p><!--? comment ?--></p></body></html>
It looks very strange for me. Why does it happen?
Thanks.
That's part of HTML tokenizations rules.
The < character made your browser enter the tag-open-state.
12.2.5.6 Tag open state
Consume the next input character:
U+0021 EXCLAMATION MARK (!)
Switch to the markup declaration open state.
U+002F SOLIDUS (/)
Switch to the end tag open state.
ASCII alpha
Create a new start tag token, set its tag name to the empty string. Reconsume in the tag name state.
U+003F QUESTION MARK (?)
This is an unexpected-question-mark-instead-of-tag-name parse error. Create a comment token whose data is the empty string.
Reconsume in the bogus comment state.
...
So your ? character is handled as a known error, and then the parser switches to the bogus comment state, which will put everything until the next > character inside the comment token.

Yahoo Pipes and Regex with an html formatting issue

I am struggling to see how to use the regex to add a non-printable carriage return character into an html string.
Its a WordPress thing in that to auto-embed a video I need to put the URL on its own line in the html.
First I use a regex:
In item.vid_src replace ($) with \\r$1
s is checked.
After which I am using a loop with a string builder in it - I am prefixing vid_src to the start of description thus:
item.vid_src
<br><br>
item.description
assign results to item.description
Before I include the Regex module in the pipe I get this:
http://www.youtube.com/watch?v=THA_5cqAfCQ<br><br><p><h1 class="MsoNormal">Cheetahs on
the edge</h1>
But I need this:
http://www.youtube.com/watch?v=THA_5cqAfCQ
<br><br><p><h1 class="MsoNormal">Cheetahs on the edge</h1>
Adding the regex module I get this:
http://www.youtube.com/watch?v=THA_5cqAfCQ\r<br><br><p><h1 class="MsoNormal">
Cheetahs on the edge</h1>
Clearly its inserting exactly what I have asked for, but It is not what I was expecting, I need to get the html formatted with the newline. Does anybody have an insight as to how to tackle the problem?

Newlines not being interpreted when getting from sqlite db?

I'm using Flask and Sqlite.
I take some string, which contains newlines, and store it in the db. At some later point I get it from the db and include it on some page, and the string shows up without newlines. What's with that?
For example if I have
{{ entry.content }}
in my template, and the entry that was stored had content "hello\nhello", it displays "hellohello" on the page.
However if I have
{{ entry.content.replace('\r\n','<br />') }}
or
{{ entry.content.replace('\r\n','
') }}
in my template, it will display "hellohello" or "hello
hello" on the page.
So my impression is that the newline characters just aren't being interpreted and displayed by the browser. What am I doing wrong?
Try {{ entry.content|safe }} so Flask/Jinja doesn't escape your HTML.
(Be careful, though, as any user entered content, including script tags, will be output as-is. If you really want to be cautious and only allow tags you might want to do write your own scrubber: Jinja2 escape all HTML but img, b, etc)

Error in Mako: "expected %>", escaping %

I'm currently writing Javascript in a mako file, and on one line, I have to check whether two strings are equal. The string I'm checking against has "<%text" within it, so I used to get an error saying there's no tag named text. I escaped that by adding a second % to get "<%%text". But now, I'm getting the following error.
SyntaxException: Expected: %> in file file.mako
What is the problem?
"<%" and "%>" are reserved symbols in Mako. If they appear in your template, Mako will assume that you mean to escape a python code block. Here is an example of what I mean:
"""
<%
some_var = 'foo'
other_var = '{0} bar'.format(some_var)
%>
"""
Take a look at http://docs.makotemplates.org/en/latest/syntax.html#python-blocks for more details
Excuse the quotes, "<%" and "%>" are also reserved symbols in the Stack Overflow WYSIWYG editor.
If the Javascript variables you are comparing contain reserved symbols, you will have to find another way of comparing them. Perhaps you could use the unicode entity for the percent sign:
For example:
if ('<%' == '<\u0025') {
alert('success!');
}
I just had a broken string variable which broke the logic, and fail on the un-related <% ... %>
Example:
<%
variable = 'this won't work'
# ^^^
%>