Skip to content

Commit 76e58e7

Browse files
committed
Add AsciiDoc support
1 parent 51727ff commit 76e58e7

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

autocorrect/grammar/asciidoc.pest

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
para = { ANY* ~ NEWLINE{2} }
2+
expr = _{ codeblock | img | link | td_tag | heading }
3+
text = { (!(expr | newline) ~ ANY)+ }
4+
newline = ${ "\n" | "\r" }
5+
space = @{ (" ")* }
6+
codeblock = ${
7+
codeblock_filename* ~ codeblock_meta* ~
8+
PUSH("----" | "....") ~ codeblock_lang ~ codeblock_code ~ PUSH("----" | "....")
9+
}
10+
codeblock_filename = @{ "." ~ (!newline ~ ANY)+ ~ newline }
11+
codeblock_meta = @{ "[" ~ (!"]" ~ ANY)+ ~ "]" ~ newline }
12+
codeblock_lang = { ASCII_ALPHA* }
13+
codeblock_code = { (!(PEEK) ~ ANY)* }
14+
td_tag = @{ space ~ "|" ~ space }
15+
mark_tag = @{
16+
"**"
17+
| "*"
18+
| "~~"
19+
| "`"
20+
}
21+
heading_tag = @{ ("======" | "=====" | "====" | "===" | "==" | "=") ~ " "* }
22+
23+
img_start = @{ "!" }
24+
img = ${ img_start ~ link }
25+
link = ${ link_string ~ link_url }
26+
link_string = { "[" ~ (!("]" | newline) ~ ANY)* ~ "]" }
27+
link_url = { "(" ~ (!(")" | newline) ~ ANY)* ~ ")" }
28+
mark = ${ PUSH(mark_tag) ~ string ~ PUSH(mark_tag) }
29+
heading = ${ PUSH(heading_tag) ~ string }
30+
string = ${ (!(PEEK | newline) ~ ANY)* }
31+
32+
line = _{ expr | text | newline }
33+
item = _{ SOI ~ line* ~ EOI }

autocorrect/src/code/asciidoc.rs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// autocorrect: false
2+
use super::*;
3+
4+
use autocorrect_derive::GrammarParser;
5+
use pest::Parser as P;
6+
use pest_derive::Parser;
7+
8+
#[derive(GrammarParser, Parser)]
9+
#[grammar = "../grammar/asciidoc.pest"]
10+
struct AsciiDocParser;
11+
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
use pretty_assertions::assert_eq;
16+
17+
#[test]
18+
fn test_format_latex() {
19+
crate::config::setup_test();
20+
21+
let example = r###"
22+
= 如何向AutoCorrect贡献代码
23+
24+
AutoCorrect是在MIT许可证下发布。如果你想贡献一些东西,或者只是想修改代码,这个文档应该可以帮助到你。
25+
26+
== 行为准则
27+
28+
该项目的贡献者需要遵守以下行为准则: link:CODE_OF_CONDUCT.adoc[code of conduct].
29+
如果有特殊情况,请将特殊的情况报告给 [email protected].
30+
31+
== 使用 GitHub Issues
32+
33+
我们使用 GitHub Issues 来对问题进行跟踪和改进。如果您要报告问题,或者有新的建议或意见,请在 https://github.com/huacnlee/autocorrect/issues[Github Issues] 上汇报问题。
34+
35+
[NOTE]
36+
====
37+
AutoCorrect需要提前安装。
38+
====
39+
40+
=== 构建指定项目的文档
41+
42+
==== 执行Lint检查
43+
44+
[indent=0]
45+
----
46+
$ autocorrect --lint
47+
----
48+
49+
=== 执行AutoCorrect格式化
50+
51+
通过AutoCorrect格式化自动纠正文件。
52+
53+
[source]
54+
----
55+
$ autocorrect --fix
56+
----
57+
58+
....
59+
..............................Done.
60+
61+
AutoCorrect spend time: 21ms
62+
...."###;
63+
64+
let expected = r###"
65+
= 如何向 AutoCorrect 贡献代码
66+
67+
AutoCorrect 是在 MIT 许可证下发布。如果你想贡献一些东西,或者只是想修改代码,这个文档应该可以帮助到你。
68+
69+
== 行为准则
70+
71+
该项目的贡献者需要遵守以下行为准则: link:CODE_OF_CONDUCT.adoc[code of conduct].
72+
如果有特殊情况,请将特殊的情况报告给 [email protected].
73+
74+
== 使用 GitHub Issues
75+
76+
我们使用 GitHub Issues 来对问题进行跟踪和改进。如果您要报告问题,或者有新的建议或意见,请在 https://github.com/huacnlee/autocorrect/issues[Github Issues] 上汇报问题。
77+
78+
[NOTE]
79+
====
80+
AutoCorrect 需要提前安装。
81+
====
82+
83+
=== 构建指定项目的文档
84+
85+
==== 执行 Lint 检查
86+
87+
[indent=0]
88+
----
89+
$ autocorrect --lint
90+
----
91+
92+
=== 执行 AutoCorrect 格式化
93+
94+
通过 AutoCorrect 格式化自动纠正文件。
95+
96+
[source]
97+
----
98+
$ autocorrect --fix
99+
----
100+
101+
....
102+
..............................Done.
103+
104+
AutoCorrect spend time: 21ms
105+
...."###;
106+
107+
assert_eq!(expected, format_asciidoc(example).to_string());
108+
}
109+
}

autocorrect/src/code/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod code;
22
mod types;
33

4+
mod asciidoc;
45
mod conf;
56
mod csharp;
67
mod css;
@@ -29,6 +30,7 @@ mod yaml;
2930
pub use code::*;
3031
pub use types::*;
3132

33+
pub use asciidoc::*;
3234
pub use conf::*;
3335
pub use csharp::*;
3436
pub use css::*;
@@ -97,6 +99,7 @@ pub fn lint_for(raw: &str, filename_or_ext: &str) -> LintResult {
9799
"dart" => lint_dart(raw),
98100
"markdown" => lint_markdown(raw),
99101
"latex" => lint_latex(raw),
102+
"asciidoc" => lint_asciidoc(raw),
100103
"gettext" => lint_gettext(raw),
101104
"conf" => lint_conf(raw),
102105
"text" => lint_markdown(raw),
@@ -151,6 +154,7 @@ pub fn format_for(raw: &str, filename_or_ext: &str) -> FormatResult {
151154
"dart" => format_dart(raw),
152155
"markdown" => format_markdown(raw),
153156
"latex" => format_latex(raw),
157+
"asciidoc" => format_asciidoc(raw),
154158
"gettext" => format_gettext(raw),
155159
"conf" => format_conf(raw),
156160
"text" => format_markdown(raw),

autocorrect/src/code/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ lazy_static! {
7575
// LaTeX
7676
"latex" => "latex",
7777
"tex" => "latex",
78+
// AsciiDoc
79+
"asciidoc" => "asciidoc",
80+
"adoc" => "asciidoc",
7881
// gettext
7982
"po" => "gettext",
8083
"pot" => "gettext",

0 commit comments

Comments
 (0)