Browse Source

source highlight working

master
alistair 3 years ago
parent
commit
380a0fa037
  1. 216
      default-templates/style.css
  2. 104
      lexbor_base.h
  3. 18
      main.cpp
  4. 2
      srchighlight.hpp

216
default-templates/style.css

@ -1,14 +1,23 @@ @@ -1,14 +1,23 @@
:root {
--primary-fg: black;
--primary-bg: white;
--primary-link: #0066cc;
--primary-link: #4271ae;
--secondary-bg: #eee;
--borders: #bbb;
}
code {
white-space: pre-wrap;
--c-bg: white;
--c-comment: #8e908c;
--c-fg: #4d4d4c;
--c-red: #c82829;
--c-orange: #f5871f;
--c-yellow: #eab700;
--c-green: #718c00;
--c-blue: #4271ae;
--c-purple: #8959a8;
--c-aqua: #70c0b1;
}
code pre {
border:none;
background: var(--secondary-bg);
border-color: var(--secondary-bg);
border-radius: 6px;
@ -17,21 +26,14 @@ code { @@ -17,21 +26,14 @@ code {
border-right-width: 4px;
border-top-width:1px;
border-bottom-width:1px;
display: block;
overflow-x: auto;
background: var(--c-bg);
color: var(--c-fg);
padding: 0.5em;
}
a {
}
.title {
margin-top:18px;
}
div.sourceCode {
background: var(--secondary-bg);
border-radius: 7px;
}
pre code {
code pre {
border-style: none;
background: none;
}
@ -42,11 +44,20 @@ code { @@ -42,11 +44,20 @@ code {
pre {
font-family: 'Bitstream Vera Sans Mono', 'Monospace', monospace;
background: var(--secondary-bg);
background: var(--primary-bg);
border-radius:6px;
padding: 0.7em;
/* Do shadows */
}
.title {
margin-top:18px;
}
span.smallcaps {
font-variant: small-caps;
} span.underline {
@ -252,18 +263,179 @@ td { @@ -252,18 +263,179 @@ td {
font-size:0.85em;
}
/* SOURCE HIGHLIGHTING */
/* the color for context lines (when specified with line ranges) */
.context
{
color: #efefef;
}
.function,
.regexp,
.preproc,
.variable,
.italics {
color: var(--c-red);
}
.usertype,
.type,
.classname,
.math,
.atom
{
color: var(--c-orange);
}
.keyword
{
color: var(--c-purple);
}
.keyword,
.preproc,
.function,
.todo,
.bold {
font-weight: bold;
}
.string,
.specialchar,
.selector {
color: var(--c-green);
}
.comment {
color: var(--c-comment);
}
.number,
.cbracket {
color: var(--c-fg);
}
.linenum {
color: var(--primary-fg);
}
.url {
color: var(--primary-link);
}
.date,
.time,
.file,
.ip,
.italics,
.meta
{
text-decoration: italic;
}
.underline,
.url {
text-decoration: underline;
}
/* for LaTeX */
.fixed,
.bibtex
{
color:var(--c-blue);
}
.argument,
.optionalargument {
color: var(--c-yellow);
}
/* for diffs */
.oldfile {
color: var(--c-green);
}
.newfile {
color: var(--c-blue);
}
.difflines {
color: var(--c-red);
}
/* for css */
.property {
color: var(--c-aqua);
}
.value {
color: var(--c-blue);
font-style: italic;
}
/* for feature/cucumber files */
.cuketag {
color: var(--c-green);
}
.gherken {
color: var(--c-blue);
}
.given {
color: var(--c-red);
}
.when {
color: var(--c-purple);
}
.then {
color: var(--c-yellow);
}
.and_but {
color: var(--c-yellow);
}
.symbol,
.table,
.normal {
color: var(--c-fg);
}
@media (prefers-color-scheme: dark) {
:root {
--primary-fg: white;
--primary-link: #90e0ff;
--primary-link: #6699cc;
--primary-bg: black;
--secondary-bg: #222;
--borders: #bbb;
}
--c-bg: #2d2d2d;
--c-comment: #999;
--c-fg: #ccc;
--c-red: #f2777a;
--c-orange: #f99157;
--c-yellow: #ffcc66;
--c-green: #99c999;
--c-aqua: #66cccc;
--c-purple: #cc99cc;
--c-blue: #6699cc;
}
dl dt {
font-size:0.6em;
font-weight:400;

104
lexbor_base.h

@ -0,0 +1,104 @@ @@ -0,0 +1,104 @@
/*
* Copyright (C) 2018 Alexander Borisov
*
* Author: Alexander Borisov <borisov@lexbor.com>
*/
/*
*
* Helpful header from the lexbor examples folder.
*
*/
#ifndef LEXBOR_EXAMPLES_BASE_H
#define LEXBOR_EXAMPLES_BASE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "lexbor/html/html.h"
#define FAILED(...) \
do { \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(EXIT_FAILURE); \
} \
while (0)
#define PRINT(...) \
do { \
fprintf(stdout, __VA_ARGS__); \
fprintf(stdout, "\n"); \
} \
while (0)
lxb_inline lxb_status_t
serializer_callback(const lxb_char_t *data, size_t len, void *ctx)
{
printf("%.*s", (int) len, (const char *) data);
return LXB_STATUS_OK;
}
lxb_inline lxb_html_document_t *
parse(const lxb_char_t *html, size_t html_len)
{
lxb_status_t status;
lxb_html_parser_t *parser;
lxb_html_document_t *document;
/* Initialization */
parser = lxb_html_parser_create();
status = lxb_html_parser_init(parser);
if (status != LXB_STATUS_OK) {
FAILED("Failed to create HTML parser");
}
/* Parse */
document = lxb_html_parse(parser, html, html_len);
if (document == NULL) {
FAILED("Failed to create Document object");
}
/* Destroy parser */
lxb_html_parser_destroy(parser);
return document;
}
lxb_inline void
serialize(lxb_dom_node_t *node)
{
lxb_status_t status;
status = lxb_html_serialize_pretty_tree_cb(node,
LXB_HTML_SERIALIZE_OPT_UNDEF,
0, serializer_callback, NULL);
if (status != LXB_STATUS_OK) {
FAILED("Failed to serialization HTML tree");
}
}
lxb_inline void
serialize_node(lxb_dom_node_t *node)
{
lxb_status_t status;
status = lxb_html_serialize_pretty_cb(node, LXB_HTML_SERIALIZE_OPT_UNDEF,
0, serializer_callback, NULL);
if (status != LXB_STATUS_OK) {
FAILED("Failed to serialization HTML tree");
}
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* LEXBOR_EXAMPLES_BASE_H */

18
main.cpp

@ -102,6 +102,7 @@ struct runtime_config { @@ -102,6 +102,7 @@ struct runtime_config {
bool save_config = false;
bool test = false;
bool comp_katex = true;
bool comp_code = true;
std::set<std::string> readonly_properties {"source_root"};
std::string port = TEST_PORT;
};
@ -424,8 +425,8 @@ class builder { @@ -424,8 +425,8 @@ class builder {
if (!clname)
continue; // skip tags with no class
auto language = shl.get_lang(std::string(clname, clname + len));
const std::string dec_lang {clname, clname + len};
auto language = shl.get_lang(dec_lang);
if (!language)
continue; // skip tags with unsupported language
@ -438,7 +439,10 @@ class builder { @@ -438,7 +439,10 @@ class builder {
lxb_html_element_t * ele = lxb_html_document_create_element(document, local_name,
(size_t)4, nullptr);
auto elem = lxb_dom_interface_element(ele);
lxb_dom_element_t * elem = lxb_dom_interface_element(ele);
highlighted = "<code class=\"" + dec_lang + "\">" + highlighted + "</code>";
lxb_dom_node_t *n = lxb_html_document_parse_fragment(document,
elem, (const unsigned char *)highlighted.c_str(), highlighted.length());
@ -456,7 +460,6 @@ class builder { @@ -456,7 +460,6 @@ class builder {
FAILED("Failed to serialization HTML tree");
}
std::string result = {res.data, res.data + res.length};
lxb_dom_collection_destroy(collection, true);
@ -699,7 +702,8 @@ class builder { @@ -699,7 +702,8 @@ class builder {
if (settings.comp_katex)
text = postprocess_math(text);
text = postprocess_code(text);
if (settings.comp_code)
text = postprocess_code(text);
}
@ -1091,6 +1095,7 @@ std::unordered_map<std::string, std::string> parse_options (int argc, char **arg @@ -1091,6 +1095,7 @@ std::unordered_map<std::string, std::string> parse_options (int argc, char **arg
("y,noninteractive", "Non-interactive mode (confirm everything)")
("t,test", "Start internal server and build in test mode")
("k,nokatex", "Do not compile KaTeX math at run time")
("c,nohlcode", "Do not highlight source code at run time")
("p,port", "Port to use for the internal server", cxxopts::value<std::string>())
("n,name", "Site name, defaults to $source", cxxopts::value<std::string>())
("u,url", "Site url, defaults to file://$destination", cxxopts::value<std::string>())
@ -1126,6 +1131,9 @@ std::unordered_map<std::string, std::string> parse_options (int argc, char **arg @@ -1126,6 +1131,9 @@ std::unordered_map<std::string, std::string> parse_options (int argc, char **arg
if (result.count("nokatex"))
settings.comp_katex = false;
if (result.count("nohlcode"))
settings.comp_code = false;
if (result.count("port")) {
settings.port = result["port"].as<std::string>();
}

2
srchighlight.hpp

@ -195,7 +195,7 @@ class gnu_highlighter { @@ -195,7 +195,7 @@ class gnu_highlighter {
{"zsh", "zsh.lang"},
};
srchilite::SourceHighlight shl {};
srchilite::SourceHighlight shl {"htmlcss.outlang"};
public:

Loading…
Cancel
Save