Skip to content

Commit cd60f25

Browse files
committed
Finish the switch to teh askama templates
1 parent ec6c84a commit cd60f25

File tree

5 files changed

+71
-83
lines changed

5 files changed

+71
-83
lines changed

data/templates/assembly.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ The `context` attribute enables module reuse. Every module ID includes {context}
3333
////
3434
3535
[role="_abstract"]
36-
// <example>
36+
{% if examples -%}
3737
This paragraph is the assembly introduction. It explains what the user will accomplish by working through the modules in the assembly and sets the context for the user story the assembly is based on. The text that immediately follows the `[role="_abstract"]` tag is used for search metadata.
38-
// </example>
38+
{%- endif %}
3939
4040
== Prerequisites
4141
42-
// <example>
42+
{% if examples -%}
4343
* A bulleted list of conditions that must be satisfied before the user starts following this assembly.
4444
* You can also link to other modules or assemblies the user must follow before starting this assembly.
4545
* Delete the section title and bullets if the assembly has no prerequisites.
4646
* X is installed. For information about installing X, see <link>.
4747
* You can log in to X with administrator privileges.
48-
// </example>
48+
{%- endif %}
4949
5050
////
5151
The following include statements pull in the module files that comprise the assembly. Include any combination of concept, procedure, or reference modules required to cover the user story. You can also include other assemblies.
@@ -69,11 +69,11 @@ include::modules/TEMPLATE_PROCEDURE_reference-material.adoc[leveloffset=2]
6969
Optional. Delete if not used.
7070
////
7171
72-
// <example>
72+
{% if examples -%}
7373
* A bulleted list of links to other material closely related to the contents of the assembly, including xref links to other assemblies in your collection.
7474
* For more details on writing assemblies, see the link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
7575
* Use a consistent system for file names, IDs, and titles. For tips, see _Anchor Names and File Names_ in link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
76-
// </example>
76+
{%- endif %}
7777

7878
////
7979
Restore the context to what it was before this assembly.

data/templates/concept.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ In the title of concept modules, include nouns or noun phrases that are used in
1818
////
1919

2020
[role="_abstract"]
21-
// <example>
21+
{% if examples -%}
2222
Write a short introductory paragraph that provides an overview of the module. The text that immediately follows the `[role="_abstract"]` tag is used for search metadata.
2323

2424
The contents of a concept module give the user descriptions and explanations needed to understand and use a product.
2525

2626
* Look at nouns and noun phrases in related procedure modules and assemblies to find the concepts to explain to users.
2727
* Explain only things that are visible to users. Even if a concept is interesting, it probably does not require explanation if it is not visible to users.
2828
* Do not include any instructions to perform an action, such as executing a command. Action items belong in procedure modules.
29-
// </example>
29+
{%- endif %}
3030

3131
[role="_additional-resources"]
3232
.Additional resources
3333
////
3434
Optional. Delete if not used.
3535
////
36-
// <example>
36+
{% if examples -%}
3737
* A bulleted list of links to other material closely related to the contents of the concept module.
3838
* Currently, modules cannot include xrefs, so you cannot include links to other content in your collection. If you need to link to another assembly, add the xref to the assembly that includes this module.
3939
* For more details on writing concept modules, see the link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
4040
* Use a consistent system for file names, IDs, and titles. For tips, see _Anchor Names and File Names_ in link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
41-
// </example>
41+
{%- endif %}
4242

data/templates/procedure.adoc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,53 @@ Start the title of a procedure module with a verb, such as Creating or Create. S
1616
////
1717

1818
[role="_abstract"]
19-
// <example>
19+
{% if examples -%}
2020
Write a short introductory paragraph that provides an overview of the module. Procedure modules should include the steps that users perform and address user motivation.The text that immediately follows the `[role="_abstract"]` tag is used for search metadata.
21-
// </example>
21+
{%- endif %}
2222

2323
.Prerequisites
2424

25-
// <example>
25+
{% if examples -%}
2626
* A bulleted list of conditions that must be satisfied before the user starts following this module.
2727
* You can also link to other modules or assemblies the user must follow before starting this module.
2828
* Delete the section title and bullets if the module has no prerequisites.
29-
// </example>
29+
{%- endif %}
3030

3131
.Procedure
3232

33-
// <example>
33+
{% if examples -%}
3434
. Start each step with an active verb.
3535

3636
. Include one command or action for each step with the exception of simple follow-step, for example:
3737
.. Do this thing and then select *Next*.
3838
.. Do this other thing and then select *Next*.
3939

4040
. Use an unnumbered bullet (*) if the procedure includes only one step.
41-
// </example>
41+
{%- endif %}
4242

4343
.Verification
4444
////
4545
Delete this section if it does not apply to your module. Provide the user with verification methods for the procedure, such as expected output or commands that confirm success or failure.
4646
////
4747

48-
// <example>
48+
{% if examples -%}
4949
. Start each step with an active verb.
5050

5151
. Include one command or action per step.
5252

5353
. Use an unnumbered bullet (*) if the procedure includes only one step.
54-
// </example>
54+
{%- endif %}
5555

5656

5757
[role="_additional-resources"]
5858
.Additional resources
5959
////
6060
Optional. Delete if not used.
6161
////
62-
// <example>
62+
{% if examples -%}
6363
* A bulleted list of links to other material closely related to the contents of the procedure module.
6464
* Currently, modules cannot include xrefs, so you cannot include links to other content in your collection. If you need to link to another assembly, add the xref to the assembly that includes this module.
6565
* For more details on writing procedure modules, see the link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
6666
* Use a consistent system for file names, IDs, and titles. For tips, see _Anchor Names and File Names_ in link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
67-
// </example>
67+
{%- endif %}
6868

data/templates/reference.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ In the title of a reference module, include nouns that are used in the body text
1616
////
1717

1818
[role="_abstract"]
19-
// <example>
19+
{% if examples -%}
2020
Write a short introductory paragraph that provides an overview of the module. The text that immediately follows the `[role="_abstract"]` tag is used for search metadata. A reference module provides data that users might want to look up, but do not need to remember.
2121
It has a very strict structure, often in the form of a list or a table.
2222
A well-organized reference module enables users to scan it quickly to find the details they want.
@@ -38,17 +38,17 @@ Term 2:: Definition
3838
|Row 1, column 1|Row 1, column 2|Row 1, column 3
3939
|Row 2, column 1|Row 2, column 2|Row 2, column 3
4040
|====
41-
// </example>
41+
{%- endif %}
4242

4343
[role="_additional-resources"]
4444
.Additional resources
4545
////
4646
Optional. Delete if not used.
4747
////
48-
// <example>
48+
{% if examples -%}
4949
* A bulleted list of links to other material closely related to the contents of the concept module.
5050
* Currently, modules cannot include xrefs, so you cannot include links to other content in your collection. If you need to link to another assembly, add the xref to the assembly that includes this module.
5151
* For more details on writing reference modules, see the link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
5252
* Use a consistent system for file names, IDs, and titles. For tips, see _Anchor Names and File Names_ in link:https://github.com/redhat-documentation/modular-docs#modular-documentation-reference-guide[Modular Documentation Reference Guide].
53-
// </example>
53+
{%- endif %}
5454

src/templating.rs

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,92 +12,78 @@ struct AssemblyTemplate<'a> { // the name of the struct can be anything
1212
// in your template
1313
module_title: &'a str,
1414
include_statements: &'a str,
15+
examples: bool,
1516
}
1617

1718
#[derive(Template)]
1819
#[template(path = "concept.adoc", escape = "none")]
1920
struct ConceptTemplate<'a> {
2021
module_id: &'a str,
2122
module_title: &'a str,
23+
examples: bool,
2224
}
2325

2426
#[derive(Template)]
2527
#[template(path = "procedure.adoc", escape = "none")]
2628
struct ProcedureTemplate<'a> {
2729
module_id: &'a str,
2830
module_title: &'a str,
31+
examples: bool,
2932
}
3033

3134
#[derive(Template)]
3235
#[template(path = "reference.adoc", escape = "none")]
3336
struct ReferenceTemplate<'a> {
3437
module_id: &'a str,
3538
module_title: &'a str,
39+
examples: bool,
3640
}
3741

3842

3943
impl Input {
40-
/// Perform string replacements in the modular template that matches the `ModuleType`.
41-
/// Return the template text with all replacements.
42-
pub fn text(&self) -> String {
43-
// TODO: Add a comment in the generated file with a pre-filled include statement
44-
45-
let mut template = match self.mod_type {
46-
ModuleType::Assembly => AssemblyTemplate {
47-
module_id: &self.id(),
48-
module_title: &self.title,
49-
include_statements: if let Some(include_statements) = &self.includes {
50-
/* include_statements.join("\n\n") */
51-
"Include this.\n\nInclude that."
52-
} else {
53-
"Include modules here."
54-
}
55-
}.render(),
56-
ModuleType::Concept => ConceptTemplate { module_id: &self.id(), module_title: &self.title }.render(),
57-
ModuleType::Procedure => ProcedureTemplate { module_id: &self.id(), module_title: &self.title }.render(),
58-
ModuleType::Reference => ReferenceTemplate { module_id: &self.id(), module_title: &self.title }.render(),
59-
}.expect("Failed to cintruct the template");
60-
61-
/*
44+
/// Render the include statements that appear inside an assembly
45+
/// into the final format. If the assembly includes nothing, use
46+
/// a placeholder, or an empty string if examples are disabled.
47+
fn includes_block(&self) -> String {
6248
if let Some(include_statements) = &self.includes {
6349
// The includes should never be empty thanks to the required group in clap
6450
assert!(!include_statements.is_empty());
6551
// Join the includes into a block of text, with blank lines in between to prevent
6652
// the AsciiDoc syntax to blend between modules
67-
let includes_text = include_statements.join("\n\n");
68-
69-
template_with_replacements =
70-
template_with_replacements.replace("${include_statements}", &includes_text);
53+
include_statements.join("\n\n")
7154
} else if self.options.examples {
72-
template_with_replacements = template_with_replacements
73-
.replace("${include_statements}", "Include modules here.");
55+
"Include modules here.".to_string()
7456
} else {
75-
template_with_replacements =
76-
template_with_replacements.replace("${include_statements}\n", "");
57+
String::new()
7758
}
78-
*/
59+
}
7960

80-
// If the `--no-examples` option is active, remove all lines between the <example> tags.
81-
if !self.options.examples {
82-
let examples: Regex = RegexBuilder::new(r"^// <example>\n[\s\S]*\n^// </example>\n")
83-
.multi_line(true)
84-
.swap_greed(true)
85-
.build()
86-
.unwrap();
87-
template = examples
88-
.replace_all(&template, "")
89-
.to_string();
90-
// If the `--no-examples` option isn't active, remove just the <example> tags.
91-
} else {
92-
let example_tags: Regex = RegexBuilder::new(r"^// </?example>\n")
93-
.multi_line(true)
94-
.swap_greed(true)
95-
.build()
96-
.unwrap();
97-
template = example_tags
98-
.replace_all(&template, "")
99-
.to_string();
100-
}
61+
/// Perform string replacements in the modular template that matches the `ModuleType`.
62+
/// Return the template text with all replacements.
63+
pub fn text(&self) -> String {
64+
let mut document = match self.mod_type {
65+
ModuleType::Assembly => AssemblyTemplate {
66+
module_id: &self.id(),
67+
module_title: &self.title,
68+
include_statements: &self.includes_block(),
69+
examples: self.options.examples,
70+
}.render(),
71+
ModuleType::Concept => ConceptTemplate {
72+
module_id: &self.id(),
73+
module_title: &self.title,
74+
examples: self.options.examples,
75+
}.render(),
76+
ModuleType::Procedure => ProcedureTemplate {
77+
module_id: &self.id(),
78+
module_title: &self.title,
79+
examples: self.options.examples,
80+
}.render(),
81+
ModuleType::Reference => ReferenceTemplate {
82+
module_id: &self.id(),
83+
module_title: &self.title,
84+
examples: self.options.examples,
85+
}.render(),
86+
}.expect("Failed to construct the document from the template");
10187

10288
// If comments are disabled via an option, delete comment lines from the content
10389
if !self.options.comments {
@@ -107,8 +93,8 @@ impl Input {
10793
.swap_greed(true)
10894
.build()
10995
.unwrap();
110-
template = multi_comments
111-
.replace_all(&template, "")
96+
document = multi_comments
97+
.replace_all(&document, "")
11298
.to_string();
11399

114100
// Delete single-line comments
@@ -117,20 +103,22 @@ impl Input {
117103
.swap_greed(true)
118104
.build()
119105
.unwrap();
120-
template = single_comments
121-
.replace_all(&template, "")
106+
document = single_comments
107+
.replace_all(&document, "")
122108
.to_string();
123109

124110
// Delete leading white space left over by the deleted comments
125111
let leading_whitespace: Regex = RegexBuilder::new(r"^[\s\n]*")
126112
.multi_line(true)
127113
.build()
128114
.unwrap();
129-
template = leading_whitespace
130-
.replace(&template, "")
115+
document = leading_whitespace
116+
.replace(&document, "")
131117
.to_string();
132118
}
133119

134-
template
120+
// Add newlines at the end of the document to prevent potential issues
121+
// when including two AsciiDoc files right next to each other.
122+
document + "\n\n"
135123
}
136124
}

0 commit comments

Comments
 (0)