Skip to content

Commit 3b05382

Browse files
committed
Escape raw rule doc text for HTML
Some the rule config text is intended for use in both tool output and in the reference, so can't be formatted at the source as the Markdown consumed by the MkDocs website generation system. This raw text may contain incidental Markdown markup, which would result in it being incorrectly rendered in the website, so it must be escaped. Despite much searching at the time the rule documentation website generation system was created, I could not find a good Go package for Markdown escaping of text. I did find an excellent package for Markdown escaping of HTML, and so I used that instead. However, this is not sufficient to completely escape the text. The reason is that Markdown is a partial superset of HTML. The `github.com/JohannesKaufmann/html-to-markdown/escape` package intentionally does not escape HTML. So it is necessary to do HTML escaping of the raw text in addition to Markdown escaping. HTML escaping was not done previously, which resulted in raw text which incidentally had the angle bracket enclosed form of HTML tags not being rendered in the website. For example, the "brief" text of rule PF009: ``` use of compiler.<pattern type>.extra_flags ``` was rendered as: ``` use of compiler..extra_flags ``` The raw text is now escaped both for HTML and for Markdown during the generation of the rule documentation website content.
1 parent f7c0a9a commit 3b05382

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

ruledocsgen/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ func generateRulesDocumentation(ruleConfigurations []ruleconfiguration.Type, out
5555
}
5656

5757
templateFunctions := template.FuncMap{
58-
// Some the rule config text is intended for use in both tool output and in the reference, so can't be formatted at
59-
// the source as Markdown. Incidental markup characters in that text must be escaped.
60-
"escape": escape.MarkdownCharacters,
58+
// Some of the rule config text is intended for use in both tool output and in the reference, so can't be formatted
59+
// at the source as Markdown. Incidental markup characters in that text must be escaped.
60+
"escape": func(rawText string) string {
61+
return escape.MarkdownCharacters(template.HTMLEscapeString(rawText))
62+
},
6163
}
6264

6365
projectRulesIntroTemplate := template.Must(template.New("messageTemplate").Parse(

ruledocsgen/main_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ func TestAll(t *testing.T) {
108108
ErrorModes: []rulemode.Type{rulemode.Default},
109109
RuleFunction: rulefunction.BoardsTxtMissing,
110110
},
111+
{
112+
ProjectType: projecttype.Platform,
113+
SuperprojectType: projecttype.All,
114+
Category: "configuration files",
115+
Subcategory: "boards.txt",
116+
ID: "PF009",
117+
Brief: "use of compiler.<pattern type>.extra_flags & foo 'bar' \"baz\"",
118+
MessageTemplate: "Board ID(s) {{.}} use compiler.<pattern type>.extra_flags properties. These are intended to be left for use by the user.",
119+
Description: "A board definition in the platform's `boards.txt` configuration file is using one of the `compiler.<pattern type>.extra_flags` properties (e.g., `compiler.cpp.extra_flags`). These are intended to be left for use by the user as a standardized interface for customizing the compilation commands. The platform author can define as many arbitrary properties as they like, so there is no need for them to take the user's properties.",
120+
Reference: "",
121+
DisableModes: nil,
122+
EnableModes: []rulemode.Type{rulemode.Default},
123+
InfoModes: nil,
124+
WarningModes: []rulemode.Type{rulemode.Default},
125+
ErrorModes: []rulemode.Type{rulemode.Strict},
126+
RuleFunction: rulefunction.BoardsTxtUserExtraFlagsUsage,
127+
},
111128
{
112129
ProjectType: projecttype.PackageIndex,
113130
SuperprojectType: projecttype.All,

ruledocsgen/testdata/golden/platform.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Arduino Lint provides 1 rules for the [`platform`](https://arduino.github.io/arduino-cli/latest/platform-specification/) project type:
1+
Arduino Lint provides 2 rules for the [`platform`](https://arduino.github.io/arduino-cli/latest/platform-specification/) project type:
22

33
---
44

@@ -20,3 +20,24 @@ Subcategory: boards.txt
2020
| permissive | ERROR |
2121
| specification | ERROR |
2222
| strict | ERROR |
23+
24+
---
25+
26+
<a id="PF009"></a>
27+
28+
## use of compiler.&lt;pattern type&gt;.extra\_flags &amp; foo &#39;bar&#39; &#34;baz&#34; (`PF009`)
29+
30+
A board definition in the platform's `boards.txt` configuration file is using one of the `compiler.<pattern type>.extra_flags` properties (e.g., `compiler.cpp.extra_flags`). These are intended to be left for use by the user as a standardized interface for customizing the compilation commands. The platform author can define as many arbitrary properties as they like, so there is no need for them to take the user's properties.
31+
32+
33+
Enabled for superproject type: all<br />
34+
Category: configuration files<br />
35+
Subcategory: boards.txt
36+
37+
##### Rule levels
38+
39+
| `compliance` | Level |
40+
|---------------|---------|
41+
| permissive | WARNING |
42+
| specification | WARNING |
43+
| strict | ERROR |

0 commit comments

Comments
 (0)