Skip to content

Commit 7fba754

Browse files
authored
Merge pull request #168 from CooperRedhat/procedure-parser
Expose Procedure Parser
2 parents 756a5b4 + 663c391 commit 7fba754

File tree

5 files changed

+136
-130
lines changed

5 files changed

+136
-130
lines changed

packages/dev/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@patternfly/patternfly": "4.183.1",
13-
"@patternfly/quickstarts": "2.2.2",
13+
"@patternfly/quickstarts": "2.2.3",
1414
"@patternfly/transform-adoc": "*",
1515
"@patternfly/react-core": "^4.135.0",
1616
"asciidoctor": "^2.2.1",
Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,3 @@
11
/* eslint-disable */
22

3-
import { QuickStart, QuickStartTask } from '@patternfly/quickstarts';
4-
5-
export const ProcQuickStartParser = (
6-
quickStart: QuickStart & {
7-
spec: {
8-
tasks: undefined | QuickStartTask[] | string[];
9-
};
10-
},
11-
environmentVariables?: { [name: string]: string },
12-
) => {
13-
const replaceEnvironmentVariables = (s: string | undefined) =>
14-
s?.replace(/\${(\w+)}/, (substring, name) => {
15-
return environmentVariables ? ([name] ? environmentVariables[name] : substring) : substring;
16-
});
17-
18-
quickStart.spec.tasks = quickStart.spec.tasks?.map((task: QuickStartTask | string, index) => {
19-
let proc: string;
20-
let answer: QuickStartTask;
21-
if (typeof task === 'string') {
22-
proc = task;
23-
answer = {};
24-
} else {
25-
// @ts-ignore
26-
proc = task.proc;
27-
answer = task;
28-
// @ts-ignore
29-
delete task.proc;
30-
}
31-
32-
let description = '',
33-
procedure,
34-
verification,
35-
title,
36-
summaryFailed,
37-
success,
38-
reviewFailed: string | undefined,
39-
prerequisites;
40-
if (proc) {
41-
const taskDOM = document.createElement('div');
42-
taskDOM.innerHTML = proc;
43-
44-
// remove the screencapture images
45-
taskDOM.querySelectorAll('.imageblock.screencapture').forEach((node) => {
46-
node.parentElement?.removeChild(node);
47-
});
48-
49-
title = taskDOM
50-
.querySelector('h1:first-child,h2:first-child,h3:first-child,h4:first-child,h5:first-child')
51-
?.innerHTML.trim();
52-
let sectionBody = taskDOM.querySelector('.sectionbody');
53-
if (!sectionBody?.hasChildNodes()) {
54-
// possibly in other templates, where we want to look for article
55-
sectionBody = taskDOM.querySelector('article');
56-
}
57-
if (sectionBody) {
58-
for (let i = 0; i < sectionBody.children.length || 0; i++) {
59-
/**
60-
child typically looks like:
61-
62-
<div class="paragraph|olist|ulist|admonitionblock">
63-
<div class="title">Procedure|Prerequisites|Verification|Note|Warning</div>
64-
<ol|ul class="arabic">
65-
<li>
66-
<li>...
67-
</ol|ul>
68-
</div>
69-
70-
And the below code extracts the <ol> or <ul>
71-
Except for when there is no <div class="title|heading"/>, then the description is extracted
72-
in the else if below
73-
*/
74-
const child = sectionBody.children.item(i);
75-
// find the title
76-
const sectionTitle = child?.querySelector('.heading,.title');
77-
// should this section be assigned to a specific section
78-
const sectionTitleText = sectionTitle?.textContent?.trim();
79-
const isKnownSection = ['Procedure', 'Verification', 'Prerequisites'].includes(
80-
sectionTitle?.textContent?.trim(),
81-
);
82-
if (isKnownSection) {
83-
switch (sectionTitleText) {
84-
case 'Procedure':
85-
procedure = child?.querySelector(':not(.heading):not(.title)')?.outerHTML.trim();
86-
break;
87-
case 'Verification':
88-
verification = child?.querySelector(':not(.heading):not(.title)')?.outerHTML.trim();
89-
break;
90-
case 'Prerequisites':
91-
prerequisites = child
92-
?.querySelector(':not(.heading):not(.title)')
93-
?.outerHTML.trim();
94-
break;
95-
}
96-
} else if (!procedure) {
97-
// Otherwise if it comes before a procedure it's part of the description
98-
description = description + child?.outerHTML.trim();
99-
}
100-
}
101-
}
102-
success = taskDOM.querySelector('.qs-summary.success')?.innerHTML.trim();
103-
reviewFailed = taskDOM.querySelector('.qs-review.failed')?.innerHTML.trim();
104-
summaryFailed = taskDOM.querySelector('.qs-summary.failed')?.innerHTML.trim();
105-
}
106-
107-
answer.title = replaceEnvironmentVariables(answer.title || title);
108-
answer.description = replaceEnvironmentVariables(
109-
answer.description || `${description} ${prerequisites || ''} ${procedure}`,
110-
);
111-
answer.review = answer.review || {};
112-
answer.review.instructions = replaceEnvironmentVariables(
113-
answer.review?.instructions || verification || 'Have you completed these steps?',
114-
);
115-
answer.review.failedTaskHelp = replaceEnvironmentVariables(
116-
answer.review.failedTaskHelp ||
117-
reviewFailed ||
118-
'This task isn’t verified yet. Try the task again.',
119-
);
120-
answer.summary = answer.summary || {};
121-
answer.summary.success = replaceEnvironmentVariables(
122-
answer.summary.success || success || 'You have completed this task!',
123-
);
124-
answer.summary.failed = replaceEnvironmentVariables(
125-
answer.summary.failed || summaryFailed || 'Try the steps again.',
126-
);
127-
return answer;
128-
});
129-
return quickStart;
130-
};
3+
export { ProcQuickStartParser } from '@patternfly/quickstarts';

packages/module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@patternfly/quickstarts",
3-
"version": "2.2.2",
3+
"version": "2.2.3",
44
"description": "PatternFly quick starts",
55
"files": [
66
"dist"

packages/module/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export * from './utils/help-topic-context';
1212
export * from './utils/help-topic-types';
1313
export * from './utils/quick-start-utils';
1414
export * from './utils/useLocalStorage';
15+
export * from './utils/asciidoc-procedure-parser';
1516
export { default as QuickStartPanelContent } from './QuickStartPanelContent';
1617
export { default as QuickStartCloseModal } from './QuickStartCloseModal';
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/* eslint-disable */
2+
// Brought in from dev to publish this with QS module
3+
// Dev now imports from here
4+
5+
import { QuickStart, QuickStartTask } from './quick-start-types';
6+
7+
export const ProcQuickStartParser = (
8+
quickStart: QuickStart & {
9+
spec: {
10+
tasks: undefined | QuickStartTask[] | string[];
11+
};
12+
},
13+
environmentVariables?: { [name: string]: string },
14+
) => {
15+
const replaceEnvironmentVariables = (s: string | undefined) =>
16+
s?.replace(/\${(\w+)}/, (substring, name) => {
17+
return environmentVariables ? ([name] ? environmentVariables[name] : substring) : substring;
18+
});
19+
20+
quickStart.spec.tasks = quickStart.spec.tasks?.map((task: QuickStartTask | string, index) => {
21+
let proc: string;
22+
let answer: QuickStartTask;
23+
if (typeof task === 'string') {
24+
proc = task;
25+
answer = {};
26+
} else {
27+
// @ts-ignore
28+
proc = task.proc;
29+
answer = task;
30+
// @ts-ignore
31+
delete task.proc;
32+
}
33+
34+
let description = '',
35+
procedure,
36+
verification,
37+
title,
38+
summaryFailed,
39+
success,
40+
reviewFailed: string | undefined,
41+
prerequisites;
42+
if (proc) {
43+
const taskDOM = document.createElement('div');
44+
taskDOM.innerHTML = proc;
45+
46+
// remove the screencapture images
47+
taskDOM.querySelectorAll('.imageblock.screencapture').forEach((node) => {
48+
node.parentElement?.removeChild(node);
49+
});
50+
51+
title = taskDOM
52+
.querySelector('h1:first-child,h2:first-child,h3:first-child,h4:first-child,h5:first-child')
53+
?.innerHTML.trim();
54+
let sectionBody = taskDOM.querySelector('.sectionbody');
55+
if (!sectionBody?.hasChildNodes()) {
56+
// possibly in other templates, where we want to look for article
57+
sectionBody = taskDOM.querySelector('article');
58+
}
59+
if (sectionBody) {
60+
for (let i = 0; i < sectionBody.children.length || 0; i++) {
61+
/**
62+
child typically looks like:
63+
64+
<div class="paragraph|olist|ulist|admonitionblock">
65+
<div class="title">Procedure|Prerequisites|Verification|Note|Warning</div>
66+
<ol|ul class="arabic">
67+
<li>
68+
<li>...
69+
</ol|ul>
70+
</div>
71+
72+
And the below code extracts the <ol> or <ul>
73+
Except for when there is no <div class="title|heading"/>, then the description is extracted
74+
in the else if below
75+
*/
76+
const child = sectionBody.children.item(i);
77+
// find the title
78+
const sectionTitle = child?.querySelector('.heading,.title');
79+
// should this section be assigned to a specific section
80+
const sectionTitleText = sectionTitle?.textContent?.trim();
81+
const isKnownSection = ['Procedure', 'Verification', 'Prerequisites'].includes(
82+
sectionTitle?.textContent?.trim(),
83+
);
84+
if (isKnownSection) {
85+
switch (sectionTitleText) {
86+
case 'Procedure':
87+
procedure = child?.querySelector(':not(.heading):not(.title)')?.outerHTML.trim();
88+
break;
89+
case 'Verification':
90+
verification = child?.querySelector(':not(.heading):not(.title)')?.outerHTML.trim();
91+
break;
92+
case 'Prerequisites':
93+
prerequisites = child
94+
?.querySelector(':not(.heading):not(.title)')
95+
?.outerHTML.trim();
96+
break;
97+
}
98+
} else if (!procedure) {
99+
// Otherwise if it comes before a procedure it's part of the description
100+
description = description + child?.outerHTML.trim();
101+
}
102+
}
103+
}
104+
success = taskDOM.querySelector('.qs-summary.success')?.innerHTML.trim();
105+
reviewFailed = taskDOM.querySelector('.qs-review.failed')?.innerHTML.trim();
106+
summaryFailed = taskDOM.querySelector('.qs-summary.failed')?.innerHTML.trim();
107+
}
108+
109+
answer.title = replaceEnvironmentVariables(answer.title || title);
110+
answer.description = replaceEnvironmentVariables(
111+
answer.description || `${description} ${prerequisites || ''} ${procedure}`,
112+
);
113+
answer.review = answer.review || {};
114+
answer.review.instructions = replaceEnvironmentVariables(
115+
answer.review?.instructions || verification || 'Have you completed these steps?',
116+
);
117+
answer.review.failedTaskHelp = replaceEnvironmentVariables(
118+
answer.review.failedTaskHelp ||
119+
reviewFailed ||
120+
'This task isn’t verified yet. Try the task again.',
121+
);
122+
answer.summary = answer.summary || {};
123+
answer.summary.success = replaceEnvironmentVariables(
124+
answer.summary.success || success || 'You have completed this task!',
125+
);
126+
answer.summary.failed = replaceEnvironmentVariables(
127+
answer.summary.failed || summaryFailed || 'Try the steps again.',
128+
);
129+
return answer;
130+
});
131+
return quickStart;
132+
};

0 commit comments

Comments
 (0)