chore: improve code

This commit is contained in:
kp2pml30 2026-02-08 01:24:04 +09:00
parent 59b1d6c116
commit 07dd66ed07
Signed by: kp2pml30
GPG key ID: CD6528BAC23E3E34
2 changed files with 17 additions and 4 deletions

View file

@ -110,16 +110,16 @@ class HTMLRenderer < YAMD::Renderer
tag('a', href: to, &blk)
end
def code(&blk)
def code(lang: YAMD::Code::Lang::DEFAULT, &blk)
tag('pre', class: "language-any") {
tag('code', class: "language-any") {
@buf << YAMD::Code::highlight(blk.())
@buf << YAMD::Code::highlight(blk.(), lang)
}
}
end
def inline_code(text)
tag('code') {
tag('code', class: "code-inline") {
@buf << YAMD::Code::highlight(text)
}
end

View file

@ -21,7 +21,7 @@ module YAMD::Code
DEFAULT = {
/\b(?:[A-Z][a-zA-Z\-_0-9]*)\b*/ => 'type',
/#(\s|$)[^\n]*/ => 'comment',
/\b(?:open|final|class|fn|let|var|if|else|while|loop|return|namespace|new)\b/ => 'kw',
/\b(?:open|final|class|struct|enum|fn|let|var|if|else|while|loop|return|namespace|new)\b/ => 'kw',
/\b(?:def|import|from)\b/ => 'kw',
/\b(?:null|undefined|true|false|this|self)\b/i => 'const',
/\b(?:__[a-zA-Z0-9_]+__)\b/i => 'const',
@ -30,6 +30,19 @@ module YAMD::Code
/"(?:\\.|[^\\"])*"/ => 'str',
/'(?:\\.|[^\\'])*'/ => 'str',
}
GD_SCRIPT = {
/\b(?:[A-Z_]+[A-Z_0-9]*)\b/ => 'const',
/\b(?:[A-Z][a-zA-Z\-_0-9]*)\b*/ => 'type',
/#(\s|$)[^\n]*/ => 'comment',
/\b(?:class|struct|enum|var|func|const|if|else|while|return)\b/ => 'kw',
/\b(?:class_name|extends)\b/ => 'kw',
/\b(?:null|true|false|self)\b/i => 'const',
/[:,;()\[\]{}<>]/ => 'punct',
/0|(0x[0-9a-fA-F]+)|([1-9][0-9]*(\.[0-9]+)?)/ => 'number',
/"(?:\\.|[^\\"])*"/ => 'str',
/'(?:\\.|[^\\'])*'/ => 'str',
}
end
def self.highlight(txt, highlights=Lang::DEFAULT)