Skip to content

Commit e7cde4a

Browse files
committed
更新代码块用法
1 parent 4345fb2 commit e7cde4a

37 files changed

+176
-174
lines changed

blog/2023-02-01-decoding-libcss-source-code.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ LibCSS 是一个 CSS 解析器和选择引擎,由 C 语言编写,是 [NetSur
1111

1212
笔者之所以选择研究 LibCSS 源码,是因为在改造 CSS 库 lcui/css 时遇到了瓶颈。虽然参考 MDN 上的 [CSS 值定义语法文档](https://developer.mozilla.org/en-US/docs/Web/CSS/Value_definition_syntax)设计并实现了 CSS 值定义语法的解析器和匹配器,但在解决取值的问题时一直找不到最优的方案,其难点在于 CSS 属性的值的数量和类型不是固定的,且由于 `background` 这类简写属性的存在,还得考虑如何复用取值逻辑。因此 LibCSS 作为一个 C 语言编写的且已经历过浏览器考验的项目,很适合作为笔者的研究对象。
1313

14+
<!-- truncate -->
15+
1416
## 用法示例
1517

1618
LibCSS 的示例程序源码在 `examples/exmaple1.c` 中,它展示了如何用 LibCSS 实现 CSS 样式的加载、选择和读取,我们先从它入手,了解 LibCSS 大致的用法和概念,以便于后续的深入研究。

docs/guide/2-installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
假设应用程序项目目录中有个 vendor 目录专用于存放第三方库的源码,那么我们可以手动下载 LCUI 的源码包然后解压到 vendor 目录中,再将 LCUI 的 xmake.lua 包含进来即可:
1111

12-
```lua title=xmake.lua
12+
```lua title="xmake.lua"
1313
includes("vendor/LCUI/xmake.lua")
1414

1515
target("app")
@@ -35,7 +35,7 @@ git submodule update --init --recursive
3535

3636
下载 LCUI 的已编译的文件包,解压到特定的目录内,例如:vendor 目录,然后添加编译和链接配置:
3737

38-
```lua title=xmake.lua
38+
```lua title="xmake.lua"
3939
target("myapp")
4040
add_includedirs("vendor/include")
4141
add_linkdirs("vendor/lib")

docs/guide/3-quick-start.mdx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
首先,我们需要包含 LCUI 库的头文件,并初始化 LCUI 应用程序。
2121

22-
```c title=main.c
22+
```c title="main.c"
2323
#include <LCUI.h>
2424
#include <LCUI/main.h>
2525

@@ -38,7 +38,7 @@ LCUI.h 是 LCUI 库的头文件,而 LCUI/main.h 是则是程序主入口的头
3838
3939
LCUI 应用程序的用户界面是由**组件**组成的。组件拥有自己的逻辑和外观,可以小到一个按钮,也可以大到整个页面。
4040
41-
```c title=main.c
41+
```c title="main.c"
4242
#include <LCUI.h>
4343
#include <LCUI/main.h>
4444
@@ -66,7 +66,7 @@ import Window from "@site/src/components/QuickStart/Window";
6666

6767
LCUI 的组件基于原型来实现组件的抽象和继承,组件原型记录了组件的创建、销毁、绘制、估算尺寸等方法,通过创建组件原型然后修改这些方法即可创建你的自定义组件。
6868

69-
```c title=main.c
69+
```c title="main.c"
7070
#include <LCUI.h>
7171
#include <LCUI/main.h>
7272

@@ -115,7 +115,7 @@ LCUI 包含 CSS 解析和选择引擎,你可以使用一些简单的 CSS 规
115115
116116
首先创建一个 css 文件,填入以下内容:
117117
118-
```css title=main.css
118+
```css title="main.css"
119119
root {
120120
padding: 24px;
121121
}
@@ -129,7 +129,7 @@ root {
129129

130130
然后在 `main()` 函数中调用 `ui_load_css_file()` 函数从文件加载 CSS 规则,再调用 `ui_widget_add_class()` 函数为 text 组件添加 title 类名,以应用 CSS 文件中定义的 `.title` 的样式。
131131

132-
```c title=main.c
132+
```c title="main.c"
133133
int main(int argc, char *argv[])
134134
{
135135
lcui_init();
@@ -200,7 +200,7 @@ LCUI 支持流式布局(Flow Layout)和弹性盒子布局(Flexible Box)
200200

201201
以常见的垂直水平居中布局为例,我们可以使用弹性盒子布局来实现:
202202

203-
```css title=main.css
203+
```css title="main.css"
204204
root {
205205
padding: 24px;
206206
// highlight-start
@@ -235,7 +235,7 @@ root {
235235

236236
XML 写法如下:
237237

238-
```xml title=main.xml
238+
```xml title="main.xml"
239239
<?xml version="1.0" encoding="UTF-8" ?>
240240
<lcui-app>
241241
<resource type="text/css" src="main.css"/>
@@ -252,7 +252,7 @@ resource 标签用于引入包括 CSS、字体在内的资源文件。ui 标签
252252

253253
`main()` 函数中调用 `ui_load_xml_file()` 函数加载这个 XML 文件:
254254

255-
```c title=main.c
255+
```c title="main.c"
256256
int main(int argc, char *argv[])
257257
{
258258
lcui_init();
@@ -287,7 +287,7 @@ LCUI 的组件是通过事件来实现与用户交互的,接下来我们让的
287287
288288
首先,修改 XML 文件,为需要操作的组件添加 id 属性,以便于在代码中访问它们。
289289
290-
```xml title=main.xml
290+
```xml title="main.xml"
291291
<?xml version="1.0" encoding="UTF-8" ?>
292292
<lcui-app>
293293
<resource type="text/css" src="main.css"/>
@@ -304,7 +304,7 @@ LCUI 的组件是通过事件来实现与用户交互的,接下来我们让的
304304

305305
然后,添加点击(Click)事件处理函数和事件绑定。
306306

307-
```c title=main.c
307+
```c title="main.c"
308308
// highlight-start
309309
void handle_my_button_click(ui_widget_t *w, ui_event_t *e, void *arg)
310310
{
@@ -349,15 +349,15 @@ import EventHandlerExample from '@site/src/components/QuickStart/EventHandlerExa
349349
350350
首先,你需要定义组件状态的数据结构:
351351
352-
```c title=main.c
352+
```c title="main.c"
353353
typedef struct {
354354
int count;
355355
} my_button_t;
356356
```
357357

358358
在初始化函数中初始化状态:
359359

360-
```c title=main.c
360+
```c title="main.c"
361361
void my_button_init(ui_widget_t *w)
362362
{
363363
// highlight-start
@@ -371,7 +371,7 @@ void my_button_init(ui_widget_t *w)
371371
372372
然后,定义一个更新函数,用于将状态转变为用户界面上实际要展示内容:
373373
374-
```c title=main.c
374+
```c title="main.c"
375375
void my_button_update(ui_widget_t *w)
376376
{
377377
char str[64];
@@ -385,7 +385,7 @@ void my_button_update(ui_widget_t *w)
385385

386386
初始化函数中也需要调用它来更新初始内容:
387387

388-
```c title=main.c
388+
```c title="main.c"
389389
void my_button_init(ui_widget_t *w)
390390
{
391391
my_button_t *data;
@@ -399,7 +399,7 @@ void my_button_init(ui_widget_t *w)
399399
400400
之后,添加点击事件的处理函数,在里面更改状态并进行更新:
401401
402-
```c title=main.c
402+
```c title="main.c"
403403
// highlight-start
404404
void my_button_on_click(ui_widget_t *w, ui_event_t *e, void *arg)
405405
{
@@ -425,7 +425,7 @@ void my_button_init(ui_widget_t *w)
425425

426426
最后,删掉 `main()` 中的事件绑定代码,像这样:
427427

428-
```c title=main.c
428+
```c title="main.c"
429429
int main(int argc, char *argv[])
430430
{
431431
lcui_init();
@@ -444,7 +444,7 @@ int main(int argc, char *argv[])
444444
445445
你可以在 XML 文件中添加多个 my-button 组件,然后尝试点击不同的按钮:
446446
447-
```xml title=main.xml
447+
```xml title="main.xml"
448448
<?xml version="1.0" encoding="UTF-8" ?>
449449
<lcui-app>
450450
<resource type="text/css" src="main.css"/>

docs/guide/4-typescript.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ CSS 的作用域是全局的,如果你担心样式冲突,则可以采用 CSS
312312
313313
首先,添加 `.module.css` 后缀的文件,写入 CSS 代码:
314314
315-
```css title=MyComponent.module.css
315+
```css title="MyComponent.module.css"
316316
.card {
317317
border: 1px solid #eee;
318318
border-radius: 4px;
@@ -321,7 +321,7 @@ CSS 的作用域是全局的,如果你担心样式冲突,则可以采用 CSS
321321
322322
然后在 `.tsx` 文件中使用 `import` 语句导入它:
323323
324-
```tsx title=MyComponent.tsx
324+
```tsx title="MyComponent.tsx"
325325
import styles from './MyComponent.module.css'
326326
```
327327

docs/rfc/lcui-cli/0001-json-compiler.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
示例 JSON 文件内容:
1515

16-
```json title=home.json
16+
```json title="home.json"
1717
{
1818
"name": "lcui-app",
1919
"children": [
@@ -63,7 +63,7 @@
6363

6464
示例 CSS 文件内容:
6565

66-
```css title=home.css
66+
```css title="home.css"
6767
root {
6868
background-color: #f6f8fa;
6969
}
@@ -77,7 +77,7 @@ root {
7777

7878
执行 `lcui compile home.json` 命令后,生成 `home.json.h` 文件:
7979

80-
```c title=home.json.h
80+
```c title="home.json.h"
8181
/** This file is generated from home.json */
8282

8383
#include <ui.h>

docs/rfc/lcui-cli/0002-xml-compiler.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
示例 XML 文件内容:
1515

16-
```xml title=home.xml
16+
```xml title="home.xml"
1717
<?xml version="1.0" encoding="UTF-8" ?>
1818
<lcui-app>
1919
<resource type="text/css" src="home.css"/>
@@ -29,7 +29,7 @@
2929

3030
示例 CSS 文件内容:
3131

32-
```css title=home.css
32+
```css title="home.css"
3333
root {
3434
background-color: #f6f8fa;
3535
}
@@ -43,7 +43,7 @@ root {
4343

4444
执行 `lcui compile home.xml` 命令后,生成 `home.xml.h` 文件:
4545

46-
```c title=home.xml.h
46+
```c title="home.xml.h"
4747
/** This file is generated from home.xml */
4848

4949
#include <ui.h>

docs/rfc/lcui-cli/0003-yaml-compiler.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
示例 XML 文件内容:
1515

16-
```yml title=home.yml
16+
```yml title="home.yml"
1717
name: lcui-app
1818
children:
1919
- name: resource
@@ -51,7 +51,7 @@ children:
5151
5252
示例 CSS 文件内容:
5353
54-
```css title=home.css
54+
```css title="home.css"
5555
root {
5656
background-color: #f6f8fa;
5757
}
@@ -65,7 +65,7 @@ root {
6565

6666
执行 `lcui compile home.yml` 命令后,生成 `home.yml.h` 文件:
6767

68-
```c title=home.yml.h
68+
```c title="home.yml.h"
6969
/** This file is generated from home.yml */
7070

7171
#include <ui.h>

docs/rfc/lcui-cli/0004-css-compiler.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
示例 CSS 文件内容:
1515

16-
```css title=home.css
16+
```css title="home.css"
1717
root {
1818
background-color: #f6f8fa;
1919
}
@@ -27,7 +27,7 @@ root {
2727

2828
执行 `lcui compile home.css` 命令后,生成 `home.css.h` 文件:
2929

30-
```c title=home.yml.h
30+
```c title="home.yml.h"
3131
static const char *css_str_0 = "\
3232
root {\
3333
background-color: #f6f8fa;\

docs/rfc/lcui-cli/0005-sass-compiler.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
示例 SCSS 文件内容:
1515

16-
```scss title=home.scss
16+
```scss title="home.scss"
1717
$bg: #f6f8fa;
1818
$color: #28a745;
1919

@@ -30,7 +30,7 @@ root {
3030

3131
执行 `lcui compile home.scss` 命令后,生成 `home.scss.h` 文件:
3232

33-
```c title=home.yml.h
33+
```c title="home.yml.h"
3434
static const char *css_str_0 = "\
3535
root {\
3636
background-color: #f6f8fa;\

docs/rfc/lcui-cli/0007-react-tsx-compiler.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
示例 tsx 文件内容:
1515

16-
```tsx title=home.tsx
16+
```tsx title="home.tsx"
1717
import React from "react";
1818
import styles from "./home.module.css";
1919

@@ -29,7 +29,7 @@ export default function Home() {
2929

3030
示例 CSS 文件内容:
3131

32-
```css title=home.module.css
32+
```css title="home.module.css"
3333
:global(.home) {
3434
padding: 20px;
3535
}
@@ -49,7 +49,7 @@ export default function Home() {
4949

5050
执行 `lcui compile home.tsx` 命令后,生成 `home.tsx.h` 文件:
5151

52-
```c title=home.tsx.h
52+
```c title="home.tsx.h"
5353
/** This file is generated from home.tsx */
5454

5555
#include <ui.h>

0 commit comments

Comments
 (0)