How do I prevent Kramdown from getting confused by a C++ include? - jekyll

I have written this plugin:
module Jekyll
module Tags
class Prism < Liquid::Block
def initialize(tag_name, text, tokens)
#arg = text.strip
super
end
def render(context)
output = super(context)
"<pre><code class=\"language-#{#arg}\">#{output}</code></pre>"
end
end
end
end
Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)
This is how I use it:
{% prism cpp %}
#include <iostream>
// Hello World
int main()
{
cout << "hello world" << endl;
int a = 10;
}
{% endprism %}
Now, the problem is, that I mostly use C++ Code on my website. When I now generate this markdown with Jekyll, then all text after {% endprism %} would still be within the <pre> Tag, because Kramdown is getting confused by <iostream> If I escape it, (\<iostream\>), then my plugin works as expected, but my Javascript Highlighter is getting confused.
How can I solve this situation without enabling Jekyll's highlighter?

I think your trying to use fenced code block used in GitHub Flavored Markdown.
Why don't you use the Jekyll code highlighting works pefectly out of the box for cpp ? A base css for highlight is available here
Try :
{% highlight cpp %}
#include <iostream>
// Hello World
int main()
{
cout << "hello world" << endl;
int a = 10;
}
{% endhighlight %}

There is a function in the CGI Module, that allows for escaping HTML much like htmlspecialchars from PHP. I changed the liquid plugin to this and it works:
require 'cgi'
module Jekyll
module Tags
class Prism < Liquid::Block
def initialize(tag_name, text, tokens)
#arg = text.strip
super
end
def render(context)
output = super(context)
output = CGI.escapeHTML(output);
"<pre><code class=\"language-#{#arg}\">#{output}</code></pre>"
end
end
end
end
Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)
It escapes all <> to html special chars. Therefore Kramdown does not get confused and prism.js is still able to highlight the code correctly.

Related

Support for std::tuple in swig?

When calling a swig generated function returning std::tuple, i get a swig object of that std::tuple.
Is there a way to use type-maps or something else to extract the values? I have tried changing the code to std::vector for a small portion of the code, and that works. (using %include <std_vector.i> and templates) But i don't want to make too many changes in the C++ part.
Edit: here is a minimal reproducible example:
foo.h
#pragma once
#include <tuple>
class foo
{
private:
double secret1;
double secret2;
public:
foo();
~foo();
std::tuple<double, double> return_thing(void);
};
foo.cpp
#include "foo.h"
#include <tuple>
foo::foo()
{
secret1 = 1;
secret2 = 2;
}
foo::~foo()
{
}
std::tuple<double, double> foo::return_thing(void) {
return {secret1, secret2};
}
foo.i
%module foo
%{
#include"foo.h"
%}
%include "foo.h"
When compiled on my linux using
-:$ swig -python -c++ -o foo_wrap.cpp foo.i
-:$ g++ -c foo.cpp foo_wrap.cpp '-I/usr/include/python3.8' '-fPIC' '-std=c++17' '-I/home/simon/Desktop/test_stack_overflow_tuple'
-:$ g++ -shared foo.o foo_wrap.o -o _foo.so
I can import it in python as shown:
test_module.ipynb
import foo as f
Foo = f.foo()
return_object = Foo.return_thing()
type(return_object)
print(return_object)
Outputs is
SwigPyObject
<Swig Object of type 'std::tuple< double,double > *' at 0x7fb5845d8420>
Hopefully this is more helpful, thank you for responding
To clarify i want to be able to use the values in python something like this:
main.cpp
#include "foo.h"
#include <iostream>
//------------------------------------------------------------------------------'
using namespace std;
int main()
{
foo Foo = foo();
auto [s1, s2] = Foo.return_thing();
cout << s1 << " " << s2 << endl;
}
//------------------------------------------------------------------------------
Github repo if anybody is interested
https://github.com/simon-cmyk/test_stack_overflow_tuple
Our goal is to make something like the following SWIG interface work intuitively:
%module test
%include "std_tuple.i"
%std_tuple(TupleDD, double, double);
%inline %{
std::tuple<double, double> func() {
return std::make_tuple(0.0, 1.0);
}
%}
We want to use this within Python in the following way:
import test
r=test.func()
print(r)
print(dir(r))
r[1]=1234
for x in r:
print(x)
i.e. indexing and iteration should just work.
By re-using some of the pre-processor tricks I used to wrap std::function (which were themselves originally from another answer here on SO) we can define a neat macro that "just wraps" std::tuple for us. Although this answer is Python specific it should in practice be fairly simple to adapt for most other languages too. I'll post my std_tuple.i file, first and then annotate/explain it after:
// [1]
%{
#include <tuple>
#include <utility>
%}
// [2]
#define make_getter(pos, type) const type& get##pos() const { return std::get<pos>(*$self); }
#define make_setter(pos, type) void set##pos(const type& val) { std::get<pos>(*$self) = val; }
#define make_ctorargN(pos, type) , type v##pos
#define make_ctorarg(first, ...) const first& v0 FOR_EACH(make_ctorargN, __VA_ARGS__)
// [3]
#define FE_0(...)
#define FE_1(action,a1) action(0,a1)
#define FE_2(action,a1,a2) action(0,a1) action(1,a2)
#define FE_3(action,a1,a2,a3) action(0,a1) action(1,a2) action(2,a3)
#define FE_4(action,a1,a2,a3,a4) action(0,a1) action(1,a2) action(2,a3) action(3,a4)
#define FE_5(action,a1,a2,a3,a4,a5) action(0,a1) action(1,a2) action(2,a3) action(3,a4) action(4,a5)
#define GET_MACRO(_1,_2,_3,_4,_5,NAME,...) NAME
%define FOR_EACH(action,...)
GET_MACRO(__VA_ARGS__, FE_5, FE_4, FE_3, FE_2, FE_1, FE_0)(action,__VA_ARGS__)
%enddef
// [4]
%define %std_tuple(Name, ...)
%rename(Name) std::tuple<__VA_ARGS__>;
namespace std {
struct tuple<__VA_ARGS__> {
// [5]
tuple(make_ctorarg(__VA_ARGS__));
%extend {
// [6]
FOR_EACH(make_getter, __VA_ARGS__)
FOR_EACH(make_setter, __VA_ARGS__)
size_t __len__() const { return std::tuple_size<std::decay_t<decltype(*$self)>>{}; }
%pythoncode %{
# [7]
def __getitem__(self, n):
if n >= len(self): raise IndexError()
return getattr(self, 'get%d' % n)()
def __setitem__(self, n, val):
if n >= len(self): raise IndexError()
getattr(self, 'set%d' % n)(val)
%}
}
};
}
%enddef
This is just the extra includes we need for our macro to work
These apply to each of the type arguments we supply to our %std_tuple macro invocation, we need to be careful with commas here to keep the syntax correct.
This is the mechanics of our FOR_EACH macro, which invokes each action per argument in our variadic macro argument list
Finally the definition of %std_tuple can begin. Essentially this is manually doing the work of %template for each specialisation of std::tuple we care to name inside of the std namespace.
We use our macro for each magic to declare a constructor with arguments for each element of the correct type. The actual implementation here is the default one from the C++ library which is exactly what we need/want though.
We use our FOR_EACH macro twice to make a member function get0, get1, getN of the correct type of each tuple element and the correct number of them for the template argument size. Likewise for setN. Doing it this way allows the usual SWIG typemaps for double, etc. or whatever types your tuple contains to be applied automatically and correctly for each call to std::get<N>. These are really just an implementation detail, not intended to be part of the public interface, but exposing them makes no real odds.
Finally we need an implementation of __getitem__ and a corresponding __setitem__. These simply look up and call the right getN/setN function on the class and call that instead. We take care to raise IndexError instead of the default exception if an invalid index is used as this will stop iteration correctly when we try to iterate of the tuple.
This is then sufficient that we can run our target code and get the following output:
$ swig3.0 -python -c++ -Wall test.i && g++ -shared -o _test.so test_wrap.cxx -I/usr/include/python3.7 -m32 && python3.7 run.py
<test.TupleDD; proxy of <Swig Object of type 'std::tuple< double,double > *' at 0xf766a260> >
['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__swig_getmethods__', '__swig_setmethods__', '__weakref__', 'get0', 'get1', 'set0', 'set1', 'this']
0.0
1234.0
Generally this should work as you'd hope in most input/output situations in Python.
There are a few improvements we could look to make:
Implement repr
Implement slicing so that tuple[n:m] type indexing works
Handle unpacking like Python tuples.
Maybe do some more automatic conversions for compatible types?
Avoid calling __len__ for every get/setitem call, either by caching the value in the class itself, or postponing it until the method lookup fails?

how to create a swig custom typemap for std::set to be wrapped by native python set?

The default typemap provided by swig expects a python list as input for constructing a set instead of native Python set (see here). Indeed,
%include "std_set.i"
%template(IntSet) std::set<int>;
will trigger the following behavior in python:
>>> import MyClass
>>> MyClass.IntSet([1,2,3,4]) # works
>>> MyClass.IntSet({1,2,3,4}) # does not work
I tried to create a custom typemap to do so but I failed so far. Here my current swig file to do so:
%module MyClass
%{
#include "MyClass.h"
%}
%include <typemaps.i>
%typemap(in) setin {$1 = PySequence_List($input);}
%typemap(out) setout {$result = PySet_New($1);}
%include "std_set.i"
%template(IntSet) std::set<int>;
%include "MyClass.h"
Would you know how to make it ?

package require with static lib

I am working on app which uses tcl package implemented in C++ and linked as static library (app is developed long time ago). It does following:
// Library code
extern "C" int testlib_SafeInit _ANSI_ARGS_((Tcl_Interp *interp))
{
return Tcl_PkgProvide(interp, "testlib", "1.6");
}
extern "C" int testlib_Init _ANSI_ARGS_((Tcl_Interp *interp))
{
return testlib_SafeInit(interp);
}
// Application code
extern "C" int testlib_SafeInit _ANSI_ARGS_((Tcl_Interp *interp));
extern "C" int testlib_Init _ANSI_ARGS_((Tcl_Interp *interp));
int main()
{
Tcl_Interp* interp = Tcl_CreateInterp();
Tcl_Init(interp);
Tcl_PkgProvide(interp, "testlib", "1.6");
Tcl_StaticPackage(interp, "testlib", testlib_Init, testlib_SafeInit);
Tcl_Eval(interp, "package require testlib");
std::cout << "Res = " << Tcl_GetStringResult(interp);
return 0;
}
When I am removing line Tcl_PkgProvide(interp, "testlib", "1.6"); from main, package becomes invisible. Also I have noticed that testlib_Init and testlib_SafeInit are not called. I am expecting that they must be called from package require testlib. As I understand from docs each package must have pkgIndex.tcl in auto_path or tcl_pkgPath which must contain line
(package ifneeded testlib 1.6 {load {} testlib}), but here both variables does not contain such index file.
Is this a correct way of providing packages? Is there a documentation related with providing packages using static libraries?
Well, the simplest technique for statically providing a package is to just install it directly. The package init code should be the one calling Tcl_PkgProvide — you don't do so from main() usually — and you probably don't need Tcl_StaticPackage at all unless you're wanting to install the code into sub-interpreters.
int main(int argc, char*argv[])
{
Tcl_FindExecutable(argv[0]);
Tcl_Interp* interp = Tcl_CreateInterp();
Tcl_Init(interp);
testlib_Init(interp);
// OK, setup is now done
Tcl_Eval(interp, "package require testlib");
std::cout << "Res = " << Tcl_GetStringResult(interp) << "\n";
return 0;
}
However, we can move to using Tcl_StaticPackage. That allows code to say “instead of loading a DLL with this sort of name, I already know that code: here are its entry points”. If you are doing that, you need to also install a package ifneeded script; those are done through the script API only.
int main(int argc, char*argv[])
{
Tcl_FindExecutable(argv[0]);
Tcl_Interp* interp = Tcl_CreateInterp();
Tcl_Init(interp);
Tcl_StaticPackage(interp, "testlib", testlib_Init, testlib_SafeInit);
Tcl_Eval(interp, "package ifneeded testlib 1.6 {load {} testlib}");
// OK, setup is now done
Tcl_Eval(interp, "package require testlib");
std::cout << "Res = " << Tcl_GetStringResult(interp) << "\n";
return 0;
}
The testlib in the load call needs to match the testlib in the Tcl_StaticPackage call. The testlib in the package require, package ifneeded and Tcl_PkgProvide also need to all match (as do the occurrences of 1.6, the version number).
Other minor issues
Also, you don't need to use the _ANSI_ARGS_ wrapper macro. That's utterly obsolete, for really ancient and crappy compilers that we don't support any more. Just replace _ANSI_ARGS_((Tcl_Interp *interp)) with (Tcl_Interp *interp). And remember to call Tcl_FindExecutable first to initialise the static parts of the Tcl library. If you don't have argv[0] available to pass into it, use NULL instead; it affects a couple of more obscure introspection systems on some platforms, but you probably don't care about them. However, initialising the library overall is very useful: for example, it lets you make sure that the filesystem's filename encoding scheme is correctly understood! That can be a little important to code…

How to label code block evaluations in orgmode on html export?

If I have a code block in orgmode, I can export its evaluation by using the :exports both option.
#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src
#+RESULTS:
: Hello there
When I export to html (C-c C-e h o), the result block follows the code block. However, I would like to more explicitly show that the 2nd block is the result of the first, with say, a simple label.
If I modify the above, like so:
#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src
Output:
#+RESULTS:
: Hello there
then the label "Output:" appears, but the result block appears twice -- once before the label, and once after. What's worse is that if I run the code within orgmode (C-c C-c), then a second result block is placed before the text label, "Output:". I suspect this is what's happening on export as well.
I have also noticed that when exporting to html, the result blocks are placed in a pre tags of class example. I thought I could modify the css with something like:
pre.example::before { content: "Output:"; }
but unfortunately, this places the text inside the pre block, and I can't add any line breaks.
Is there any simple way to add text labels to result blocks in either orgmode itself, or perhaps through css? I'd like to avoid javascript if possible.
This should work on reasonably recent org:
#+name: foo
#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src
Output:
#+RESULTS: foo
: Hello there
You can use a derived backend for this like this:
(defun my-results (fixed-width contents info)
"Transform a results block to make it more visible."
(let ((results (org-element-property :results fixed-width))
(format (elt (plist-get info :back-end) 2))
(value (org-element-property :value fixed-width)))
(cond
((eq 'html format)
(format "<pre>Output:<br> %s</pre>" value)))))
(org-export-define-derived-backend 'my-html 'html
:translate-alist '((fixed-width . my-results)))
(browse-url (org-export-to-file 'my-html (concat (file-name-base (buffer-file-name)) ".html")))

Hunspell c++ and russian language

i'v got some problems using hunspell spell checker with russian dictionaries. The problem is that my project works well with english, but if i'm going to connect russian language and trying to check spelling of my words it is always returns 0 (mean NO RESULT).
Here is my code (works well for english)
char *aff = "c:\\ru_RU.aff";
char *dic = "c:\\ru_RU.dic";
Hunspell *spellObj = new Hunspell(aff,dic);
char *words = "собака"
int result = spellObj->spell(words);
result is "0". Probably the probem in encoding. I'v tried UTF-8, KOI8-R dictionaries. When using UTF-8 dictionary it can't read the "words", when using KOI8-R it's result 0.
its so bad, what i have to make it work well.
p.s. last version of hunspell+vs2008 c++
New dictionaries are usually encoded to UTF-8. Same example compiled in MSYS2/mingw64 gives the correct result=1 with new UTF-8 dictionaries.
// UTF-8 file "main.cpp"
#include <iostream>
#include <hunspell.hxx>
int main()
{
char *aff = "ru_RU.aff";
char *dic = "ru_RU.dic";
Hunspell *spellObj = new Hunspell(aff,dic);
char *words = "собака";
int result = spellObj->spell(words);
std::cout << "result=" << result << std::endl;
return result;
}
Precompiled package was used. For installation, you need to enter in mingw64.exe environment pacman -Su mingw-w64-x86_64-hunspell. The content of Makefile is as follows:
PKGS=hunspell
CFLAGS=$(shell pkg-config --cflags $(PKGS)) -std=gnu++98
LIBS=$(shell pkg-config --libs $(PKGS))
all: main
%: %.cpp
$(CXX) $(CFLAGS) -o $# $< $(LIBS)