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

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.

Related

How can I generate code from file at compile time using a macro?

I have a CSV file that looks like this:
CountryCode,CountryName
AD,Andorra
AE,United Arab Emirates
AF,Afghanistan
AG,Antigua and Barbuda
// -- snip -- //
and a class that looks like this:
module OpenData
class Country
def initialize(#code : String, #name : String)
end
end
end
and I want to have a class variable within the module automatically loaded at compile time like this:
module OpenData
##countries : Array(Country) = {{ run "./sources/country_codes.cr" }}
end
I tried to use the "run" macro above with the following code:
require "csv"
require "./country"
content = File.read "#{__DIR__}/country-codes.csv"
result = [] of OpenData::Country
CSV.new(content, headers: true).each do |row|
result.push OpenData::Country.new(row["CountryCode"], row["CountryName"])
end
result
but this results in
##countries : Array(Country) = {{ run "./sources/country_codes.cr" }}
^
Error: class variable '##countries' of OpenData must be Array(OpenData::Country), not Nil
All my other attempts somehow failed due to various reasons, like not being able to call .new within a macro or stuff like that. This is something I regularly do in Elixir and other languages that support macros and is something I would suspect Crystal can also achieve... I'd also take any other way that accomplishes the task at compile time!
Basically there are several more files I want to process this way, and they`re longer/more complex... thanks in advance!
EDIT:
Found the issue. It seems that I have to return a string that includes actual crystal code from the "run" macro. So, the code in the "run" file becomes:
require "csv"
content = File.read "#{__DIR__}/country-codes.csv"
lines = [] of String
CSV.new(content, headers: true).each do |row|
lines << "Country.new(\"#{row["CountryCode"]}\", \"#{row["CountryName"]}\")"
end
puts "[#{lines.join(", ")}]"
and everything works!
You already found your answer, but for completeness, here are the docs, from: https://crystal-lang.org/api/1.2.2/Crystal/Macros.html#run%28filename%2C%2Aargs%29%3AMacroId-instance-method
Compiles and execute a Crystal program and returns its output
as a MacroId.
The file denoted by filename must be a valid Crystal program.
This macro invocation passes args to the program as regular
program arguments. The program must output a valid Crystal expression.
This output is the result of this macro invocation, as a MacroId.
The run macro is useful when the subset of available macro methods
are not enough for your purposes and you need something more powerful.
With run you can read files at compile time, connect to the internet
or to a database.
A simple example:
# read.cr
puts File.read(ARGV[0])
# main.cr
macro read_file_at_compile_time(filename)
{{ run("./read", filename).stringify }}
end
puts read_file_at_compile_time("some_file.txt")
The above generates a program that will have the contents of some_file.txt.
The file, however, is read at compile time and will not be needed at runtime.

EJS: Testing of included ejs files

my express app uses one path ("/") to display a huge page which was build using several includes. To avoid confusion, I would like to test each and every ejs file on its own. But as these ejs-includes are not reachable from the express app, I have no clue how to perform that task.
- index.ejs
- mainMenu.ejs
- systemsTable.ejs
- systemRow.ejs
- systemStatusIcon.ejs
- systemName.ejs
- ....
The more complex the index.ejs file gets the more I would like to test its parts. But how can I test the result of systemStatusIcon.ejs?
Thanks
Without any knowledge of what your code looks like, the I suggest setting up a configuration JSON file of test data objects each containing:
the EJS file path to be tested
render input data
an array of regular expressions to test against the rendered output
Then your test code can iterate over these objects and render and test each in turn.
Example Template Rendering
A simple piece of code to render a template looks like this:
const ejs = require('ejs');
let template = `
value is: <%- value %>
`;
const renderData = {
value: 123
};
const output = ejs.render(template, renderData);
console.log(output);
With output:
> node index.js
value is: 123
What renderData will be required is dependent on your existing templates. Template errors will be thrown as standard JS errors.
You could also use the renderFile function to do the loading for you.
ejs.renderFile(filename, data, options, function(err, str){
// str => Rendered HTML string
});
EJS docs can be found at: https://ejs.co/

SugarCRM customization of Basic template

I need to add a field in basic template. Can anyone help me how can i add another field in include/SugarObjects/templates/basic/vardefs.php in upgrade safe manner.
In VardefManager's function addTemplate not like general standards of Sugar it is not requiring the custom paths
include/SugarObjects/VardefManager.php near line 107 SugarCE6.5.5:
if(empty($templates[$template])){
$path = 'include/SugarObjects/templates/' . $template . '/vardefs.php';
if(file_exists($path)){
require($path);
$templates[$template] = $vardefs;
}else{
$path = 'include/SugarObjects/implements/' . $template . '/vardefs.php';
if(file_exists($path)){
require($path);
$templates[$template] = $vardefs;
}
}
}
Really waiting for awesome responses.
Create a file at the path custom/include/SugarObjects/VardefManager.php with the name VardefManager.php and in that file include your mail file it is include/SugarObjects/VardefManager.php.
Here you will create a class with same and and create a function with the name
static function addTemplate
with same the arguments pass in the main file. and override the method here with your custom code (as you want to add some lines of code in that).
This will be upgrade safe and will be workable to you.

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

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}")

Why is php_template_preprocess_page function not called in Drupal 6x?

From another forum I found the following example:
"I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.
in the template.php file for your theme:
function phptemplate_preprocess_page(&$vars) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$vars['template_file'] = 'page-ajax';
}
}
then create page-ajax.tpl.php in your theme directory with this content:
<?php print $content; ?>
"
This seems like the logical way to do it and I did this, but the phptemplate_preprocess_page function is never called ... any suggestions?
I figured it out for myself from a Drupal Support Theme Development page:
"Maybe this helps
leahcim.2707 - May 29, 2008 - 05:40
I was trying to get the same thing done and for me this works, but I'm not sure if it is the correct way as I'm still new to Drupal:
in "template.php" I added the following function:
function phptemplate_preprocess_page(&$vars)
{
$css = $vars['css'];
unset($css['all']['module']['modules/system/system.css']);
unset($css['all']['module']['modules/system/defaults.css']);
$vars['styles'] = drupal_get_css($css);
}
I think after adding the function you need to go to /admin/build/themes so that Drupal recognises the function."
The part in bold is what did the trick ... you have to re-save the configuration so it recognizes that you've added a new function to the template.