From 07dd66ed0716c210138e251a2b542d1a0f7ddaa4 Mon Sep 17 00:00:00 2001 From: kp2pml30 Date: Sun, 8 Feb 2026 01:24:04 +0900 Subject: [PATCH] chore: improve code --- exe/yamd | 6 +++--- lib/yamd/code.rb | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/exe/yamd b/exe/yamd index f43a35a..6b6b94d 100755 --- a/exe/yamd +++ b/exe/yamd @@ -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 diff --git a/lib/yamd/code.rb b/lib/yamd/code.rb index 73e666e..ea7854f 100644 --- a/lib/yamd/code.rb +++ b/lib/yamd/code.rb @@ -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)