Latex directives in template are causing `error in unicode escape` - templates

I want to include some LaTEX code in play framework 2.0 template, namely:
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
Of course play complains with error in unicode escape error message because of \us... parts.
How should I escape those pieces of text, so play compiles the template and I get verbatim LaTeX code in result? Tried my luck with #Html(), but it doesn't work either...

Create an method ie in Application.java controller:
public static String latex(String s){
return "\\"+s;
}
So you can use it in the view:
#Application.latex("usepackage[T1]{fontenc}")
#Application.latex("usepackage[latin9]{inputenc}")
#Application.latex("usepackage{babel}")

Or, based on #biesior answer, create a latex.scala.html file containing:
#(latexStatement:String)
#{
"\\" + latexStatement
}
To use it:
#latex("usepackage[T1]{fontenc}")
#latex("usepackage[latin9]{inputenc}")
#latex("usepackage{babel}")

Related

VS Code Hypersnips Extension: snippets defined in math context does not work with rmarkdown (.Rmd) files

I tried to use the VS Code Hypersnips extension (https://github.com/draivin/hsnips) for rmarkdown files. The extension works well with markdown files and latex files, but for some reason I cannot get it to work properly with rmarkdown.
For example, I define the following math contexts in the global setting first:
function math(context) {
return context.scopes.some(s => s.includes("math"));
}
// inline & block math
function inline_math(context){
return context.scopes.some(s => s.includes("math.inline"));
}
function block_math(context){
return context.scopes.some(s => s.includes("math.block")) || context.scopes.some(s => s.includes("math.display"));
}
However, snippets defined in these math context such as the following do not activate when the trigger is typed:
context math(context)
snippet `(;;;|cdot)` "cdots" iA
``rv='\\cdots'``
endsnippet
context math(context)
snippet `par` "partial" iA
``rv="\\partial "``
endsnippet
In contrast, snippets defined without the context definition activate properly, such as:
snippet `\\dm` "display math" bA
$$
$1
$$
$0
endsnippet
I cannot figure out why. I tried to store the snippets in a rmd.hsnips, Rmd.hsnips, rmarkdown.hsnips, and Rmarkdown.hsnips in the designated directory, but the above issue persists.
Thank you for your help.

Visual Studio Snippet: Can I use TM_FILENAME to get a namespace from RELATIVE_FILEPATH?

I am toying with Visual Studio Snippets for a while now, and wonder if/how I can use TM_FILENAME to get a namespace from RELATIVE_FILEPATH. For example, I have:
RELATIVE_FILEPATH = src\model\RegisterModel.php
TM_FILENAME = RegisterModel.php
I want to strip the latter from the first, and the last \ as well, so I will end up with src\model
I can get it working if I use RegisterModel.php as a string, but not if I use TM_FILENAME as a variable. Is that even possible?
This is what works: ${RELATIVE_FILEPATH/(RegisterModel.php)//gi}
But I want something like this: ${RELATIVE_FILEPATH/(TM_FILENAME )//gi}
Thanks in advance!
This will get you what you want:
"filepath": {
"prefix": "rfp",
"body": [
"${RELATIVE_FILEPATH/(\\\\[^\\\\]*)$//i}", // with lots of escapes
],
"description": "directories of current file"
}
${RELATIVE_FILEPATH/${TM_FILENAME}//gi} will not work as according to the grammar only a regex can go into that ${TM_FILENAME} spot in the transform. See snippet grammar.
Relevant part of the grammar: transform ::= '/' regex '/' (format | text)+ '/' options
${RELATIVE_FILEPATH/[${TM_FILENAME}]//gi} results in:
src\od\Rgsrod.php because [${TM_FILENAME}] is treated as a regex (just a alternate list of those characters literally) and so each of those characters is removed from the RELATIVE_FILEPATH.
This is what I was trying to create. This snippet creates a php class template with the proper class name and namespace from the file name and location.
So, snippet
"php class": {
"prefix": "_pclass",
"body": "<?php\n\nnamespace app\\\\${RELATIVE_FILEPATH/(\\\\[^\\\\]*)$//i};\n\nclass $TM_FILENAME_BASE{\n\n\tpublic function __construct(){}\n\n\tpublic function $1($2){\n\n\t\t$3\n\t}\n}",
"description": "PHP class"
}
called in I:\xampp\htdocs\mvc2\src\core\form\Field.php creates
<?php
namespace app\src\core\form;
class Field{
public function __construct(){}
public function (){
}
}
with the cursor on the method name. Saves me quite a bit of time, hope it's just as helpful for others.

Splitting Handlebars template path between lines

I am working on an EmberJS app, and in the tests I have got multiple occurrences of:
this.render(hbs`{{directory1/directory2/directory3/directory4/directory5/directory6/directory1/directory8/ hasMyAction=(action hasMyAction)}}`);
I have got an ESLint max line length of 120. How can I split the line of code above?
// eslint-disable-next-line
this.render(hbs`{{directory1/directory2/directory3/directory4/directory5/directory6/directory1/directory8/
hasMyAction=(action hasMyAction)
}}`);
Can't break up the path, as far as I know, but you can tell ESLint to ignore the line.
maybe something like this:
const context = [
'directory1',
'directory2',
'directory3',
'directory4',
'directory5',
'directory6',
'directory7'
].join('/');
const componentPath = `${context}/component-name`;
this.render(hbs`{{${componentPath} hasMyAction=(action hasMyAction)}}`);
though, personally, I'm not sure if backtick templates do interpolation that way, so maybe this could be an alternative:
this.set('componentPath', componentPath);
this.render(hbs`{{component componentPath hasMyAction=(action hasMyAction)}}`

golang template escape first char

I'm trying to build sitemap XML file with the standard template package.
But the first charset "<" become "&lt ;", and make the XML unreadable for clients.
package main
import (
"bytes"
"fmt"
"html/template"
)
const (
tmplStr = `{{define "indexSitemap"}}<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://www.test.com/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://www.test.com/events-sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://www.test.com/gamesAndTeams-sitemap.xml</loc>
</sitemap>
</sitemapindex>{{end}}`
)
func main() {
// Parse the template and check for error
tmpl, parseErr := template.New("test").Parse(tmplStr)
if parseErr != nil {
fmt.Println(parseErr)
return
}
// Init the writer
buf := new(bytes.Buffer)
// Execute and get the template error if any
tmplExErr := tmpl.ExecuteTemplate(buf, "indexSitemap", nil)
if tmplExErr != nil {
fmt.Println(tmplExErr)
return
}
// Print the content malformed
fmt.Println(buf)
}
playground golang
Is that normal?
How can I make it works normaly.
Thanks in advance
Your example shows you're using the html/template package, which auto-escapes text for html usage.
If you want a raw template engine, use the text/template package instead - the html one just wraps it with context-aware escaping.
However, you'll need to make sure by yourself that the texts you output with the raw template engine are XML-safe. You can do this by exposing some escape function to your template, and passing all texts via this function instead of writing them directly.
[EDIT] It looks like a bug in html/template, if you omit the ? from the xml declaration it works okay. But still my advice stands - if it's not html you're better off using the text/template package. Actually, even better, describe the site map as a struct and don't use a template at all, just XML serialization.
Also see issue #12496 on github which confirms they are not planning to fix this.
https://github.com/golang/go/issues/12496
Probably because this is the HTML templating package and you're trying
to produce XML. I suspect that it doesn't know how to parse the
directives with the question mark there.
You probably want to use the text/template package instead, if you're
not going to be taking advantage of any of the HTML auto-escaping
features.

getElementsByTagName setAttribute and regex javascript

i want to put rel=lightbox to some links that mediabox support using javascript.
i try this and wonder why it's not working?
test: http://jsbin.com/opica
please help edit this: http://jsbin.com/opica/edit
<script type="text/javascript">
var x=xmlDoc.getElementsByTagName("a");
var regexku=/^.+(((twit)|(tweet)|(com/video.+)|(flickr.com.+)|(tube.com.+))|((gif)|(jpe?g)|(png)|(flv)|(swf)|(mp3)|(mp4))$)/;
for(i=0;i<x.length;i++)
{
a=x[i].getAttribute('href');
if (a.match(regexku) != null)
{
x.item(i).setAttribute("rel","lightbox");
}
}
</script>
So if you open the Error Console (Tools -> Error Console in Firefox), you'll see two errors on your page:
Error: xmlDoc is not defined
Source File: http://jsbin.com/opica
Line: 35
Error: invalid regular expression flag v
Source File: http://jsbin.com/opica
Line: 21, Column: 38
Source Code:
var regexku=/^.+(((twit)|(tweet)|(com/video.+)|(flickr.com.+)|(tube.com.+))|((gif)|(jpe?g)|(png)|(flv)|(swf)|(mp3)|(mp4))$)/;
The later is fixed by escaping the slash as Bart suggested (com\/video).
The former says there's no such thing as xmlDoc. You probably meant the page's document, in which case you should replace it with document.
Next the whole thing probably won't work because you should run the script after the page is finished loading. In jQuery it's $(document).ready(function() { /* do your work here */ }), google how to do it using the whatever framework you're using (mootools-yui?).
After that as you can see, the rel attribute is set on the links: http://jsbin.com/elaca/edit. The fact that the whatever library you're using still doesn't work means you're using it wrong. You didn't even link to the page you've downloaded the library from so that someone could look up the documentation for you...
Try escaping the / between com and video.