@hia/theme-core 0.7.0 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/export/editor-themes.d.ts +62 -0
- package/dist/export/editor-themes.js +201 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lang zh-CN 编辑器配色主题生成器(`W-tb-P42`,ADR-015):把 HIA 主题映射为
|
|
3
|
+
* TextMate(`.tmTheme` XML plist)/ VSCode(`.color-theme.json`)/ Vim(`.vim`)/ Emacs(`.el`)
|
|
4
|
+
* 四种格式。**scope→角色档位映射**是 HIA 的设计判断(登记于 ADR-015),逐 scope 独立取色——
|
|
5
|
+
* 导出产物是精确的(与预览的"着色示意"分离)。明暗两套由 appearance 参数派生。
|
|
6
|
+
*
|
|
7
|
+
* @lang en Editor colour theme generators (`W-tb-P42`, ADR-015): map an HIA theme to
|
|
8
|
+
* TextMate (`.tmTheme` XML plist) / VSCode (`.color-theme.json`) / Vim (`.vim`) / Emacs (`.el`).
|
|
9
|
+
* The scope→role mapping is an HIA design judgement (registered in ADR-015); each scope resolves
|
|
10
|
+
* independently, so exports are exact (the preview remains an approximation, by design).
|
|
11
|
+
*/
|
|
12
|
+
import type { Theme } from '../types.js';
|
|
13
|
+
/** @lang zh-CN 外观档位:light/dark 各派生一套编辑器主题。 @lang en Appearance: light/dark each derive one editor theme. */
|
|
14
|
+
export type EditorAppearance = 'light' | 'dark';
|
|
15
|
+
/**
|
|
16
|
+
* @lang zh-CN 生成 TextMate 主题(`.tmTheme`,XML plist 格式)。
|
|
17
|
+
* scope 按"父 scope"粒度输出(TextMate 匹配前缀,子 scope 继承)。
|
|
18
|
+
* @lang en TextMate theme (`.tmTheme`, XML plist). Scopes emit at parent granularity (TextMate matches by
|
|
19
|
+
* prefix, children inherit).
|
|
20
|
+
*/
|
|
21
|
+
export declare function textmateTheme(t: Theme, appearance?: EditorAppearance): string;
|
|
22
|
+
/**
|
|
23
|
+
* @lang zh-CN 生成 VSCode 主题(`.color-theme.json`):tokenColors(scope 数组)+ colors
|
|
24
|
+
* (workbench chrome:editorBackground/lineNumber/selection 等)。
|
|
25
|
+
* @lang en VSCode theme (`.color-theme.json`): tokenColors (scope array) + workbench colors
|
|
26
|
+
* (editorBackground, lineNumber, selection…).
|
|
27
|
+
*/
|
|
28
|
+
export declare function vscodeTheme(t: Theme, appearance?: EditorAppearance): string;
|
|
29
|
+
/**
|
|
30
|
+
* @lang zh-CN 生成 Vim 主题(`.vim` 脚本):highlight 组映射——
|
|
31
|
+
* Comment/String/Number/Keyword/Function/Type/Normal/Visual/LineNr。
|
|
32
|
+
* @lang en Vim theme (`.vim` script): highlight groups Comment/String/Number/Keyword/Function/Type/Normal/
|
|
33
|
+
* Visual/LineNr.
|
|
34
|
+
*/
|
|
35
|
+
export declare function vimTheme(t: Theme, appearance?: EditorAppearance): string;
|
|
36
|
+
/**
|
|
37
|
+
* @lang zh-CN 生成 Emacs 主题(`.el` face 定义):defface + deftheme,组与 Vim 对齐。
|
|
38
|
+
* @lang en Emacs theme (`.el` face definitions): deftheme + defface, groups aligned with Vim.
|
|
39
|
+
*/
|
|
40
|
+
export declare function emacsTheme(t: Theme, appearance?: EditorAppearance): string;
|
|
41
|
+
/**
|
|
42
|
+
* @lang zh-CN 生成器注册表:`builtin:` templateRef 名 → 生成函数与产物扩展名。
|
|
43
|
+
* TDON 文档经 `exportManifest.files[]` 以 `templateRef` 引用(与 `builtin:hia-tokens-css` 同构)。
|
|
44
|
+
* appearance 由调用方(导出链路)传入——TDON 文档可用同一路径声明两份(light/dark)。
|
|
45
|
+
* @lang en Generator registry: `builtin:` templateRef name → generator + file extension. TDON documents
|
|
46
|
+
* reference them from `exportManifest.files[]` (isomorphic with `builtin:hia-tokens-css`); appearance is
|
|
47
|
+
* passed by the export pipeline, so a document can declare both light/dark files.
|
|
48
|
+
*/
|
|
49
|
+
export declare const EDITOR_THEME_GENERATORS: {
|
|
50
|
+
ref: string;
|
|
51
|
+
ext: string;
|
|
52
|
+
label: string;
|
|
53
|
+
generate: (t: Theme, appearance: EditorAppearance) => string;
|
|
54
|
+
}[];
|
|
55
|
+
/** @lang zh-CN 按 ref 取生成器定义;未命中返回 undefined。 @lang en Looks up a generator by ref; undefined when missing. */
|
|
56
|
+
export declare function findEditorThemeGenerator(ref: string): {
|
|
57
|
+
ref: string;
|
|
58
|
+
ext: string;
|
|
59
|
+
label: string;
|
|
60
|
+
generate: (t: Theme, appearance: EditorAppearance) => string;
|
|
61
|
+
} | undefined;
|
|
62
|
+
//# sourceMappingURL=editor-themes.d.ts.map
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// 角色档位取值(恒 HEX;缺失档位回退基色)。
|
|
2
|
+
// <lang en>Role/step lookup (always HEX; missing steps fall back to the base colour).</lang>
|
|
3
|
+
function step(t, role, shade) {
|
|
4
|
+
const r = t[role];
|
|
5
|
+
// shade 是运行时拼出的档位键('50'…'900'),ColorScale 索引签名按字符串键放行。
|
|
6
|
+
// <lang en>shade is a runtime-built step key ('50'…'900'); index ColorScale by string key.</lang>
|
|
7
|
+
return r.scale[shade] ?? r.value;
|
|
8
|
+
}
|
|
9
|
+
function thirdOr(t, fallback) { return t.thirdColor?.value ?? fallback; }
|
|
10
|
+
function auxOr(t, i, fallback) { return t.auxiliary[i]?.value ?? fallback; }
|
|
11
|
+
/**
|
|
12
|
+
* @lang zh-CN scope 映射表(ADR-015 登记的 HIA 设计判断)。
|
|
13
|
+
* 设计原则:注释退后(secondary.600 低刺激)、字符串/常量用语义间隔色(success/warning 系
|
|
14
|
+
* 若语义色存在则优先——编辑器主题不自带语义组,故此处用 primary 系间隔档)、
|
|
15
|
+
* 关键字用主色、函数名用主色深阶、变量用中性深墨、类型用第三色/辅助色。
|
|
16
|
+
* @lang en The scope mapping (HIA design judgement, ADR-015): comments recede (secondary.600),
|
|
17
|
+
* strings/constants get semantic-interval shades, keywords use the primary, function names its darker
|
|
18
|
+
* step, variables neutral ink, types the third/aux colour.
|
|
19
|
+
*/
|
|
20
|
+
function scopeMappings(t) {
|
|
21
|
+
const hasSemantic = t.semantic;
|
|
22
|
+
const stringHex = hasSemantic ? t.semantic.success.scale['600'] ?? t.semantic.success.value : step(t, 'secondary', '600');
|
|
23
|
+
const numberHex = hasSemantic ? t.semantic.warning.scale['600'] ?? t.semantic.warning.value : step(t, 'secondary', '700');
|
|
24
|
+
const typeHex = t.thirdColor?.value ?? auxOr(t, 0, step(t, 'primary', '400'));
|
|
25
|
+
return [
|
|
26
|
+
{ scope: 'comment', fg: () => step(t, 'secondary', '600') },
|
|
27
|
+
{ scope: 'string', fg: () => stringHex },
|
|
28
|
+
{ scope: 'constant.numeric', fg: () => numberHex },
|
|
29
|
+
{ scope: 'constant.language', fg: () => step(t, 'primary', '400') },
|
|
30
|
+
{ scope: 'keyword', fg: () => step(t, 'primary', '500') },
|
|
31
|
+
{ scope: 'keyword.operator', fg: () => step(t, 'secondary', '700') },
|
|
32
|
+
{ scope: 'entity.name.function', fg: () => step(t, 'primary', '600') },
|
|
33
|
+
{ scope: 'entity.name.type', fg: () => typeHex },
|
|
34
|
+
{ scope: 'entity.name.tag', fg: () => step(t, 'primary', '600') },
|
|
35
|
+
{ scope: 'variable', fg: () => step(t, 'secondary', '900') },
|
|
36
|
+
{ scope: 'variable.parameter', fg: () => step(t, 'secondary', '800') },
|
|
37
|
+
{ scope: 'support.function', fg: () => step(t, 'primary', '600') },
|
|
38
|
+
{ scope: 'meta.tag', fg: () => thirdOr(t, step(t, 'primary', '600')) },
|
|
39
|
+
{ scope: 'markup.heading', fg: () => step(t, 'primary', '700') }
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
/** @lang zh-CN 编辑器 chrome:背景/前景/行号/选区/当前行——明暗两套各自取色。 @lang en Editor chrome: background, foreground, line numbers, selection, current line — per appearance. */
|
|
43
|
+
function chrome(t, appearance) {
|
|
44
|
+
// light:主色 50 阶底 + 深墨字;dark:主色 900 阶底 + 主色 50 字。
|
|
45
|
+
// <lang en>light: primary-50 bg + ink text; dark: primary-900 bg + primary-50 text.</lang>
|
|
46
|
+
if (appearance === 'dark') {
|
|
47
|
+
return { bg: t.primary.scale['900'] ?? '#101A2B', fg: t.primary.scale['50'] ?? '#E8EEF7', line: t.primary.scale['800'] ?? '#182540', selection: t.primary.scale['700'] ?? '#243B5E', current: t.primary.scale['800'] ?? '#182540', comment: step(t, 'secondary', '400') };
|
|
48
|
+
}
|
|
49
|
+
return { bg: t.primary.scale['50'] ?? '#F4F8FD', fg: t.primary.scale['900'] ?? '#0F1E33', line: t.primary.scale['100'] ?? '#DCE8F4', selection: t.primary.scale['100'] ?? '#DCE8F4', current: t.primary.scale['100'] ?? '#DCE8F4', comment: step(t, 'secondary', '600') };
|
|
50
|
+
}
|
|
51
|
+
/** @lang zh-CN 主题名规范化(编辑器主题名不含空格/特殊字符)。 @lang en Normalises the theme name for editor theme titles. */
|
|
52
|
+
function editorName(t, appearance) {
|
|
53
|
+
const base = t.name.replace(/[^\w\u4e00-\u9fa5-]+/g, ' ').trim();
|
|
54
|
+
return `${base} (${appearance})`;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* @lang zh-CN 生成 TextMate 主题(`.tmTheme`,XML plist 格式)。
|
|
58
|
+
* scope 按"父 scope"粒度输出(TextMate 匹配前缀,子 scope 继承)。
|
|
59
|
+
* @lang en TextMate theme (`.tmTheme`, XML plist). Scopes emit at parent granularity (TextMate matches by
|
|
60
|
+
* prefix, children inherit).
|
|
61
|
+
*/
|
|
62
|
+
export function textmateTheme(t, appearance = 'light') {
|
|
63
|
+
const c = chrome(t, appearance);
|
|
64
|
+
const items = scopeMappings(t).map(m => ` <dict>
|
|
65
|
+
<key>name</key><string>${m.scope}</string>
|
|
66
|
+
<key>scope</key><string>${m.scope}</string>
|
|
67
|
+
<key>settings</key><dict>
|
|
68
|
+
<key>foreground</key><string>${m.fg(t)}</string>${m.bg ? `\n <key>background</key><string>${m.bg(t)}` : ''}
|
|
69
|
+
</dict>
|
|
70
|
+
</dict>`).join('\n');
|
|
71
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
72
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
73
|
+
<plist version="1.0">
|
|
74
|
+
<dict>
|
|
75
|
+
<key>name</key><string>${editorName(t, appearance)}</string>
|
|
76
|
+
<key>semanticClass</key><string>theme.${appearance}.${editorName(t, appearance)}</string>
|
|
77
|
+
<key>settings</key><array>
|
|
78
|
+
<dict>
|
|
79
|
+
<key>settings</key><dict>
|
|
80
|
+
<key>background</key><string>${c.bg}</string>
|
|
81
|
+
<key>foreground</key><string>${c.fg}</string>
|
|
82
|
+
<key>lineHighlight</key><string>${c.current}</string>
|
|
83
|
+
<key>selection</key><string>${c.selection}</string>
|
|
84
|
+
</dict>
|
|
85
|
+
</dict>
|
|
86
|
+
${items}
|
|
87
|
+
</array>
|
|
88
|
+
</dict>
|
|
89
|
+
</plist>
|
|
90
|
+
`;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @lang zh-CN 生成 VSCode 主题(`.color-theme.json`):tokenColors(scope 数组)+ colors
|
|
94
|
+
* (workbench chrome:editorBackground/lineNumber/selection 等)。
|
|
95
|
+
* @lang en VSCode theme (`.color-theme.json`): tokenColors (scope array) + workbench colors
|
|
96
|
+
* (editorBackground, lineNumber, selection…).
|
|
97
|
+
*/
|
|
98
|
+
export function vscodeTheme(t, appearance = 'light') {
|
|
99
|
+
const c = chrome(t, appearance);
|
|
100
|
+
const tokenColors = scopeMappings(t).map(m => ({
|
|
101
|
+
scope: m.scope,
|
|
102
|
+
settings: { foreground: m.fg(t), ...(m.bg ? { background: m.bg(t) } : {}) }
|
|
103
|
+
}));
|
|
104
|
+
const doc = {
|
|
105
|
+
name: editorName(t, appearance),
|
|
106
|
+
type: appearance === 'dark' ? 'dark' : 'light',
|
|
107
|
+
semanticHighlighting: true,
|
|
108
|
+
colors: {
|
|
109
|
+
'editor.background': c.bg,
|
|
110
|
+
'editor.foreground': c.fg,
|
|
111
|
+
'editorLineNumber.foreground': c.line,
|
|
112
|
+
'editor.selectionBackground': c.selection,
|
|
113
|
+
'editor.lineHighlightBackground': c.current
|
|
114
|
+
},
|
|
115
|
+
tokenColors
|
|
116
|
+
};
|
|
117
|
+
return JSON.stringify(doc, null, 2) + '\n';
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @lang zh-CN 生成 Vim 主题(`.vim` 脚本):highlight 组映射——
|
|
121
|
+
* Comment/String/Number/Keyword/Function/Type/Normal/Visual/LineNr。
|
|
122
|
+
* @lang en Vim theme (`.vim` script): highlight groups Comment/String/Number/Keyword/Function/Type/Normal/
|
|
123
|
+
* Visual/LineNr.
|
|
124
|
+
*/
|
|
125
|
+
export function vimTheme(t, appearance = 'light') {
|
|
126
|
+
const c = chrome(t, appearance);
|
|
127
|
+
const m = scopeMappings(t);
|
|
128
|
+
const fg = (scope) => m.find(x => x.scope === scope)?.fg(t) ?? c.fg;
|
|
129
|
+
const name = editorName(t, appearance).replace(/[^\w\u4e00-\u9fa5-]+/g, '_');
|
|
130
|
+
return `" ${editorName(t, appearance)} — generated by HIA Theme Builder (W-tb-P42 / ADR-015)
|
|
131
|
+
" Usage: :colorscheme ${name}
|
|
132
|
+
if exists('g:syntax_on')
|
|
133
|
+
syntax reset
|
|
134
|
+
endif
|
|
135
|
+
let g:colors_name = '${name}'
|
|
136
|
+
|
|
137
|
+
set background=${appearance === 'dark' ? 'dark' : 'light'}
|
|
138
|
+
highlight clear
|
|
139
|
+
|
|
140
|
+
highlight Normal guifg=${c.fg} guibg=${c.bg}
|
|
141
|
+
highlight Comment guifg=${fg('comment')}
|
|
142
|
+
highlight String guifg=${fg('string')}
|
|
143
|
+
highlight Number guifg=${fg('constant.numeric')}
|
|
144
|
+
highlight Boolean guifg=${fg('constant.language')}
|
|
145
|
+
highlight Keyword guifg=${fg('keyword')}
|
|
146
|
+
highlight Operator guifg=${fg('keyword.operator')}
|
|
147
|
+
highlight Function guifg=${fg('entity.name.function')}
|
|
148
|
+
highlight Type guifg=${fg('entity.name.type')}
|
|
149
|
+
highlight Tag guifg=${fg('entity.name.tag')}
|
|
150
|
+
highlight Variable guifg=${fg('variable')}
|
|
151
|
+
highlight Visual guibg=${c.selection}
|
|
152
|
+
highlight LineNr guifg=${c.line}
|
|
153
|
+
highlight CursorLine guibg=${c.current}
|
|
154
|
+
`;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @lang zh-CN 生成 Emacs 主题(`.el` face 定义):defface + deftheme,组与 Vim 对齐。
|
|
158
|
+
* @lang en Emacs theme (`.el` face definitions): deftheme + defface, groups aligned with Vim.
|
|
159
|
+
*/
|
|
160
|
+
export function emacsTheme(t, appearance = 'light') {
|
|
161
|
+
const c = chrome(t, appearance);
|
|
162
|
+
const m = scopeMappings(t);
|
|
163
|
+
const fg = (scope) => m.find(x => x.scope === scope)?.fg(t) ?? c.fg;
|
|
164
|
+
const name = editorName(t, appearance).replace(/[^\w\u4e00-\u9fa5-]+/g, '-').toLowerCase();
|
|
165
|
+
return `;;; ${editorName(t, appearance)} — generated by HIA Theme Builder (W-tb-P42 / ADR-015)
|
|
166
|
+
;;; Usage: (load-theme '${name})
|
|
167
|
+
|
|
168
|
+
(deftheme ${name}
|
|
169
|
+
"HIA Theme Builder theme (${appearance}).")
|
|
170
|
+
|
|
171
|
+
(custom-theme-set-faces
|
|
172
|
+
'${name}
|
|
173
|
+
'(default ((t (:background "${c.bg}" :foreground "${c.fg}"))))
|
|
174
|
+
'(font-lock-comment-face ((t (:foreground "${fg('comment')}"))))
|
|
175
|
+
'(font-lock-string-face ((t (:foreground "${fg('string')}"))))
|
|
176
|
+
'(font-lock-constant-face ((t (:foreground "${fg('constant.numeric')}"))))
|
|
177
|
+
'(font-lock-keyword-face ((t (:foreground "${fg('keyword')}"))))
|
|
178
|
+
'(font-lock-function-name-face ((t (:foreground "${fg('entity.name.function')}"))))
|
|
179
|
+
'(font-lock-type-face ((t (:foreground "${fg('entity.name.type')}"))))
|
|
180
|
+
'(font-lock-variable-name-face ((t (:foreground "${fg('variable')}"))))
|
|
181
|
+
'(region ((t (:background "${c.selection}"))))
|
|
182
|
+
'(line-number ((t (:foreground "${c.line}")))))
|
|
183
|
+
`;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @lang zh-CN 生成器注册表:`builtin:` templateRef 名 → 生成函数与产物扩展名。
|
|
187
|
+
* TDON 文档经 `exportManifest.files[]` 以 `templateRef` 引用(与 `builtin:hia-tokens-css` 同构)。
|
|
188
|
+
* appearance 由调用方(导出链路)传入——TDON 文档可用同一路径声明两份(light/dark)。
|
|
189
|
+
* @lang en Generator registry: `builtin:` templateRef name → generator + file extension. TDON documents
|
|
190
|
+
* reference them from `exportManifest.files[]` (isomorphic with `builtin:hia-tokens-css`); appearance is
|
|
191
|
+
* passed by the export pipeline, so a document can declare both light/dark files.
|
|
192
|
+
*/
|
|
193
|
+
export const EDITOR_THEME_GENERATORS = [
|
|
194
|
+
{ ref: 'builtin:textmate-theme', ext: '.tmTheme', label: 'TextMate', generate: textmateTheme },
|
|
195
|
+
{ ref: 'builtin:vscode-theme', ext: '.color-theme.json', label: 'VSCode', generate: vscodeTheme },
|
|
196
|
+
{ ref: 'builtin:vim-theme', ext: '.vim', label: 'Vim', generate: vimTheme },
|
|
197
|
+
{ ref: 'builtin:emacs-theme', ext: '.el', label: 'Emacs', generate: emacsTheme }
|
|
198
|
+
];
|
|
199
|
+
/** @lang zh-CN 按 ref 取生成器定义;未命中返回 undefined。 @lang en Looks up a generator by ref; undefined when missing. */
|
|
200
|
+
export function findEditorThemeGenerator(ref) { return EDITOR_THEME_GENERATORS.find(g => g.ref === ref); }
|
|
201
|
+
//# sourceMappingURL=editor-themes.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -60,6 +60,8 @@ export { wordpressChildTarget } from './export/wordpress.js';
|
|
|
60
60
|
export { tailwindConfigTarget } from './export/tailwind.js';
|
|
61
61
|
export { tailwindV4Target } from './export/tailwind4.js';
|
|
62
62
|
export { dtcgExportTarget, exportDtcgTokens, parseDtcgTokens } from './export/dtcg.js';
|
|
63
|
+
export { textmateTheme, vscodeTheme, vimTheme, emacsTheme, EDITOR_THEME_GENERATORS, findEditorThemeGenerator } from './export/editor-themes.js';
|
|
64
|
+
export type { EditorAppearance } from './export/editor-themes.js';
|
|
63
65
|
export type { DtcgIssue, DtcgIssueCode, DtcgParseResult } from './export/dtcg.js';
|
|
64
66
|
export { injectTemplateTarget } from './export/inject-template.js';
|
|
65
67
|
export { analyzeTheme } from './analyze/analyze.js';
|
package/dist/index.js
CHANGED
|
@@ -94,6 +94,7 @@ export { wordpressChildTarget } from './export/wordpress.js';
|
|
|
94
94
|
export { tailwindConfigTarget } from './export/tailwind.js';
|
|
95
95
|
export { tailwindV4Target } from './export/tailwind4.js';
|
|
96
96
|
export { dtcgExportTarget, exportDtcgTokens, parseDtcgTokens } from './export/dtcg.js';
|
|
97
|
+
export { textmateTheme, vscodeTheme, vimTheme, emacsTheme, EDITOR_THEME_GENERATORS, findEditorThemeGenerator } from './export/editor-themes.js';
|
|
97
98
|
export { injectTemplateTarget } from './export/inject-template.js';
|
|
98
99
|
export { analyzeTheme } from './analyze/analyze.js';
|
|
99
100
|
export { toV2Signals } from './analyze/v1-compat.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hia/theme-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
4
|
"description": "HIA cross-platform theme domain core: models, OKLCH algorithms, theme studio state, gradient rendering, token export and website-color analysis.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "HIA Theme Builder contributors",
|