forked from blackhole89/notekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notebook_highlight.hpp
56 lines (49 loc) · 2.21 KB
/
notebook_highlight.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
std::string CNotebook::GetHighlightProxyDir()
{
if(mkdir("/tmp/notekit.gsv",0777) && errno!=EEXIST) {
printf("Failed to create a temporary directory for syntax highlighting data.\n");
return "";
}
if(!access("/tmp/notekit.gsv/markdownlisting.lang", R_OK)) {
// file already exists
return "/tmp/notekit.gsv";
}
FILE *fl = fopen("/tmp/notekit.gsv/markdownlisting.lang","wb");
if(!fl) {
printf("Failed to create a language definition for syntax highlighting data.\n");
return "";
}
/* start detecting languages */
Glib::RefPtr<Gsv::LanguageManager> lm = Gsv::LanguageManager::get_default();
std::vector<std::string> langs = lm->get_language_ids();
std::vector<std::string> langs_supported;
for(std::string &l : langs) {
if(!lm->get_language(l)->get_hidden()) langs_supported.push_back(l);
}
/* generate code listing proxy lang definition */
fprintf(fl,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
fprintf(fl,"<language id=\"markdownlisting\" name=\"Markdown Code Listing\" version=\"2.0\" hidden=\"true\">\n <definitions>\n");
/* one context for every language detected */
for(std::string &l : langs_supported) {
fprintf(fl," <context id=\"proxy-%s\" class=\"no-spell-check mono\">\n",l.c_str());
fprintf(fl," <start>^(```%s)$</start>\n",l.c_str());
fprintf(fl," <end>^(```)$</end>\n");
fprintf(fl," <include>\n <context id=\"proxy-%s-contents\" extend-parent=\"false\">\n",l.c_str());
fprintf(fl," <start></start>\n");
fprintf(fl," <include>\n <context ref=\"%s:%s\" />\n </include>\n",l.c_str(),l.c_str());
fprintf(fl," </context>\n"
" <context sub-pattern=\"1\" where=\"start\" style-ref=\"markdown:tag\" class=\"hline\" />\n"
" <context sub-pattern=\"1\" where=\"end\" style-ref=\"markdown:tag\" class=\"hline\" />\n"
" </include>\n"
" </context>\n");
}
/* finally, export every context as part of this "language" */
fprintf(fl," <context id=\"markdownlisting\">\n <include>\n");
for(std::string &l : langs_supported) {
fprintf(fl," <context ref=\"proxy-%s\"/>\n",l.c_str());
}
fprintf(fl," </include>\n </context>");
fprintf(fl," </definitions>\n</language>\n");
fclose(fl);
return "/tmp/notekit.gsv";}