/
kopython
/
_hyperscript
Обзор
Документация
Войти
/
kopython
/
_hyperscript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
www/_build/widgets.js
55 строк
2 KB
Deniz Akşimşek
fix {% syntaxes %}
13 окт 2022, 13:30
13 окт 2022, 13:30
d3074f5
Код
Авторство
О чём код?
module.exports = function (config) { config.addPairedShortcode('example', (content, caption) => { let rv = "<figure class='box'>\n\n" if (caption) rv += `<figcaption class="allcaps" >Example: ${caption}</figcaption>\n\n` else rv += `<figcaption class="allcaps">Example</figcaption>\n\n` rv += " ~~~ html" rv += content rv += "~~~\n\n" rv += content rv += "</figure>" return rv }) function syntax(syntax) { syntax = syntax .replace(/\[\[([\*\+\?])\]\]/g, "<sup>$1</sup>" ) .replace(/\[\[([a-zA-Z0-9 ]+)\]\]/g, '<b class="chip syntaxvar"><var>$1</var></b>' ) return `<code class="syntax">${syntax}</code>` } config.addShortcode("syntax", syntax) function syntaxify(line) { return line .replace(/`([^`]+)`/g, (match, p1) => syntax(p1)) } config.addPairedShortcode("syntaxes", content => { const buf = [] const lines = content.split('\n'); let dt = false; for (const line of lines) { if (isJustWhitespace(line)) { buf.push(line); } else if (!indented(line)) { dt = true; buf.push('\n<dt>' + syntaxify(line)); } else { if (dt) buf.push('<dd>\n'); dt = false; buf.push(dedent(line)); } } return `<div class="box"><dl class="syntaxes">${buf.join('\n')}</dl></div>`; }) } function isJustWhitespace(str) { return /^\s*$/.test(str) } function dedent(str) { return str.replace(/^(\t| )/, '') } function indented(str) { return str.startsWith(' ') || str.startsWith('\t') }