@elisra-devops/docgen-skins 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ import { Run, WIProperty, StyleOptions } from "./wordJsonModels";
2
+ import { defaultJsonRun } from "./default";
3
+ import striphtml from "string-strip-html";
4
+ import logger from "../../services/logger";
5
+
6
+ const replaceBr = ({ tag, deleteFrom, deleteTo, rangesArr }) => {
7
+ if (tag.name === `br`) {
8
+ rangesArr.push(deleteFrom, deleteTo, "\n");
9
+ } else {
10
+ rangesArr.push(deleteFrom, deleteTo, " ");
11
+ }
12
+ };
13
+
14
+ export default class JSONRun {
15
+ runs: Run[];
16
+
17
+ constructor(value: string, styles: StyleOptions, fieldtype?: string) {
18
+ this.runs = this.generateJsonRun(value, styles, fieldtype);
19
+ } //constructor
20
+
21
+ generateJsonRun(value: string, style: StyleOptions, fieldtype?: string): Run[] {
22
+ let runs: Run[] = [];
23
+ let rowArray;
24
+ try {
25
+ rowArray = value.split("\n");
26
+ //try iterating the array
27
+ rowArray.forEach((text, i) => {
28
+ let run: Run = defaultJsonRun;
29
+ if (fieldtype !== "SuiteHeaderParagraphTitle") {
30
+ text = `${striphtml(text.toString(), { cb: replaceBr })}` || "";
31
+ }
32
+ run.text = text;
33
+ run.Bold = style.isBold;
34
+ run.Italic = style.IsItalic;
35
+ run.Underline = style.IsUnderline;
36
+ run.Size = style.Size;
37
+ if (run.Uri === "") {
38
+ run.Uri = null;
39
+ } else {
40
+ run.Uri = style.Uri;
41
+ }
42
+ run.Font = style.Font;
43
+ if (rowArray.length == 1) {
44
+ run.InsertLineBreak = style.InsertLineBreak;
45
+ } else {
46
+ run.InsertLineBreak = false;
47
+ }
48
+ if (rowArray.length - 1 == i) {
49
+ run.InsertLineBreak = style.InsertLineBreak;
50
+ }
51
+ run.InsertSpace = style.InsertSpace;
52
+ runs.push(JSON.parse(JSON.stringify(run)));
53
+ });
54
+ } catch (e) {
55
+ logger.silly(`${value} - cannot be splitted`);
56
+ runs[0] = defaultJsonRun;
57
+ runs[0].text = value
58
+ ? `${striphtml(value.toString(), { cb: replaceBr })}`
59
+ : ``;
60
+ runs[0].Bold = style.isBold;
61
+ runs[0].Italic = style.IsItalic;
62
+ runs[0].Underline = style.IsUnderline;
63
+ runs[0].Size = style.Size;
64
+ if (runs[0].Uri === "") {
65
+ runs[0].Uri = null;
66
+ } else {
67
+ runs[0].Uri = style.Uri;
68
+ }
69
+ runs[0].Font = style.Font;
70
+ runs[0].InsertLineBreak = style.InsertLineBreak;
71
+ runs[0].InsertSpace = style.InsertSpace;
72
+ }
73
+
74
+ return runs;
75
+ } //generateJsonRun
76
+
77
+ getRun(): Run[] {
78
+ return this.runs;
79
+ }
80
+ } //class
@@ -0,0 +1,24 @@
1
+ import { Run, StyleOptions } from "./wordJsonModels";
2
+
3
+ export let defaultJsonRun: Run = {
4
+ text: ``,
5
+ Bold: false,
6
+ Italic: false,
7
+ Underline: false,
8
+ Size: 12,
9
+ Uri: null,
10
+ Font: "Arial",
11
+ InsertLineBreak: false,
12
+ InsertSpace: true
13
+ };
14
+
15
+ export let DescriptionandProcedureStyle: StyleOptions = {
16
+ isBold: true,
17
+ IsItalic: false,
18
+ IsUnderline: true,
19
+ Size: 12,
20
+ Uri: null,
21
+ Font: "Arial",
22
+ InsertLineBreak: false,
23
+ InsertSpace: false,
24
+ };
@@ -0,0 +1,13 @@
1
+ import { JsonHtml } from "../wordJsonModels";
2
+ import JSONRun from "../JSONRun";
3
+
4
+ export default class JSONHtml {
5
+ htmlString: string = "";
6
+ constructor(htmlString: string) {
7
+ this.htmlString = htmlString;
8
+ } //constructor
9
+
10
+ getJSONHtml(): JsonHtml {
11
+ return { type: "html", Html: this.htmlString };
12
+ } //getJSONTable
13
+ } //class
@@ -0,0 +1,76 @@
1
+ import {
2
+ Paragraph,
3
+ Run,
4
+ WIData,
5
+ WIProperty,
6
+ StyleOptions,
7
+ } from "../wordJsonModels";
8
+ import JSONRun from "../JSONRun";
9
+
10
+ export default class JSONParagraph {
11
+ paragraphStyles: StyleOptions;
12
+ paragraphTemplate: Paragraph;
13
+ wiId: number;
14
+ constructor(
15
+ field: WIProperty,
16
+ paragraphStyles: StyleOptions,
17
+ wiId: number,
18
+ headingLevel: number
19
+ ) {
20
+ this.paragraphStyles = paragraphStyles;
21
+ this.wiId = wiId;
22
+ this.paragraphTemplate = {
23
+ type: "paragraph",
24
+ runs: this.generateJsonParagraphRun(field, this.paragraphStyles),
25
+ };
26
+ if (headingLevel && field.name === "Title: ") {
27
+ this.paragraphTemplate.headingLevel = headingLevel;
28
+ } else {
29
+ this.paragraphTemplate.headingLevel = 0;
30
+ }
31
+ } //constructor
32
+
33
+ generateJsonParagraphRun(field: WIProperty, paragraphStyles): Run[] {
34
+ let jsonRun: JSONRun;
35
+ let runs: Run[] = [];
36
+ let titleStyle = JSON.parse(JSON.stringify(paragraphStyles));
37
+
38
+ titleStyle.isBold = false;
39
+ titleStyle.IsUnderline = true;
40
+ titleStyle.InsertSpace = true;
41
+ titleStyle.InsertLineBreak = false;
42
+ //adds ':' to field titles
43
+ if (field.name !== "") {
44
+ field.name += ": ";
45
+ }
46
+ //exclude titles for fields that only show values
47
+ if (
48
+ field.name !== "Title: " &&
49
+ field.name !== "ID: " &&
50
+ field.name !== "Description: " &&
51
+ field.name !== "text: "
52
+ ) {
53
+ paragraphStyles.Uri = field.url;
54
+ jsonRun = new JSONRun(field.name, titleStyle);
55
+ runs = [...runs, ...jsonRun.runs];
56
+
57
+ paragraphStyles.InsertLineBreak = false;
58
+ } //end if
59
+
60
+ if (field.name === "Title: ") {
61
+ paragraphStyles.Uri = field.url;
62
+ paragraphStyles.InsertLineBreak = false;
63
+ jsonRun = new JSONRun(field.value, paragraphStyles);
64
+ runs = [...runs, ...jsonRun.runs];
65
+ } else {
66
+ paragraphStyles.Uri = field.url;
67
+ jsonRun = new JSONRun(field.value, paragraphStyles);
68
+ runs = [...runs, ...jsonRun.runs];
69
+ }
70
+ return runs;
71
+ } //generateJsonParagraph
72
+
73
+ getJSONParagraph(): Paragraph {
74
+ return this.paragraphTemplate;
75
+ } //getJSONTable
76
+ } //class
@@ -0,0 +1,84 @@
1
+ import { Run, WIProperty, StyleOptions } from "../wordJsonModels";
2
+ import JSONHtml from "../html/JSONHtml";
3
+ import JSONParagraph from "./JSONParagraph";
4
+ import JSONTable from "../table/JSONTable";
5
+ import logger from "../../../services/logger";
6
+
7
+ export default class JSONRichTextParagraph {
8
+ paragraphStyles: StyleOptions;
9
+ paragraphTemplate: any;
10
+ wiId: number;
11
+ constructor(
12
+ field: WIProperty,
13
+ paragraphStyles: StyleOptions,
14
+ wiId: number,
15
+ headingLevel: number
16
+ ) {
17
+ this.paragraphStyles = paragraphStyles;
18
+ this.wiId = wiId;
19
+ this.paragraphTemplate = this.generateJsonRichTextParagraphs(
20
+ field,
21
+ this.paragraphStyles
22
+ );
23
+ if (headingLevel && field.name === "Title: ") {
24
+ this.paragraphTemplate.headingLevel = headingLevel;
25
+ } else {
26
+ this.paragraphTemplate.headingLevel = 0;
27
+ }
28
+ } //constructor
29
+
30
+ generateJsonRichTextParagraphs(field: WIProperty, paragraphStyles): Run[] {
31
+ let titleStyle = JSON.parse(JSON.stringify(paragraphStyles));
32
+
33
+ titleStyle.isBold = true;
34
+ titleStyle.IsUnderline = true;
35
+ titleStyle.InsertSpace = true;
36
+ titleStyle.InsertLineBreak = false;
37
+
38
+ let richTextSkin: any[] = [];
39
+ //handle rich text
40
+ try {
41
+ field.richText.forEach((content) => {
42
+ switch (content.type) {
43
+ case "paragraph":
44
+ content.data.fields.forEach((richTextField: WIProperty) => {
45
+ if (/<\/?[a-z][\s\S]*>/.test(richTextField.value)) {
46
+ let html = new JSONHtml(richTextField.value);
47
+ richTextSkin.push(html.getJSONHtml());
48
+ } else {
49
+ //making sure ID is not printed
50
+ let paragraphSkin = new JSONParagraph(
51
+ richTextField,
52
+ paragraphStyles,
53
+ 0,
54
+ 0
55
+ );
56
+ richTextSkin.push(paragraphSkin.getJSONParagraph());
57
+ }
58
+ });
59
+ break;
60
+ case "picture":
61
+ richTextSkin.push({
62
+ type: "picture",
63
+ Path: content.data,
64
+ });
65
+ break;
66
+ case "table":
67
+ if (content.data) {
68
+ let tableSkin = new JSONTable(content.data, paragraphStyles, 0);
69
+ richTextSkin.push(tableSkin.getJSONTable());
70
+ }
71
+ break;
72
+ }
73
+ });
74
+ return richTextSkin;
75
+ } catch (e) {
76
+ logger.warn(e);
77
+ return [];
78
+ }
79
+ } //generateJsonRichTextParagraphs
80
+
81
+ getJSONRichTextParagraph(): any[] {
82
+ return this.paragraphTemplate;
83
+ } //getJSONTable
84
+ } //class
@@ -0,0 +1,94 @@
1
+ import {
2
+ Paragraph,
3
+ Run,
4
+ WIData,
5
+ WIProperty,
6
+ StyleOptions,
7
+ } from "../wordJsonModels";
8
+ import JSONRun from "../JSONRun";
9
+
10
+ export default class JSONTestSuiteHeaderParagraph {
11
+ paragraphStyles: StyleOptions;
12
+ paragraphTemplate: Paragraph;
13
+ wiId: number;
14
+
15
+ constructor(
16
+ fields: WIProperty[],
17
+ paragraphStyles: StyleOptions,
18
+ wiId: number,
19
+ headingLevel: number
20
+ ) {
21
+ this.paragraphStyles = paragraphStyles;
22
+ this.wiId = wiId;
23
+ this.paragraphTemplate = {
24
+ type: "paragraph",
25
+ runs: this.generateJsonParagraphHeaderRuns(fields, this.paragraphStyles),
26
+ };
27
+
28
+ if (headingLevel) {
29
+ this.paragraphTemplate.headingLevel = headingLevel;
30
+ } else {
31
+ this.paragraphTemplate.headingLevel = 0;
32
+ }
33
+ } //constructor
34
+
35
+ generateJsonParagraphHeaderRuns(fields, paragraphStyles) {
36
+ let runs: any = [];
37
+ fields.forEach((field) => {
38
+ runs = [
39
+ ...runs,
40
+ ...this.generateJsonParagraphRun(field, this.paragraphStyles),
41
+ ];
42
+ });
43
+
44
+ return runs;
45
+ }
46
+ generateJsonParagraphRun(field: WIProperty, paragraphStyles): Run[] {
47
+ let jsonRun: JSONRun;
48
+ let runs: Run[] = [];
49
+ let titleStyle = JSON.parse(JSON.stringify(paragraphStyles));
50
+
51
+ titleStyle.isBold = false;
52
+ titleStyle.IsUnderline = true;
53
+ titleStyle.InsertSpace = true;
54
+ titleStyle.InsertLineBreak = false;
55
+ //adds ':' to field titles
56
+ if (field.name !== "") {
57
+ field.name += ": ";
58
+ }
59
+ //exclude titles for fields that only show values
60
+ if (
61
+ field.name !== "Title: " &&
62
+ field.name !== "ID: " &&
63
+ field.name !== "Description: "
64
+ ) {
65
+ paragraphStyles.Uri = field.url;
66
+ jsonRun = new JSONRun(field.name, titleStyle);
67
+ runs = [...runs, ...jsonRun.runs];
68
+
69
+ paragraphStyles.InsertLineBreak = false;
70
+ } //end if
71
+
72
+ if (field.name === "ID: ") {
73
+ field.value = `${field.value}`;
74
+ paragraphStyles.Uri = field.url;
75
+ paragraphStyles.InsertLineBreak = false;
76
+ }
77
+
78
+ if (field.name === "Title: ") {
79
+ let fieldtype = "SuiteHeaderParagraphTitle";
80
+ paragraphStyles.InsertLineBreak = false;
81
+ paragraphStyles.Uri = field.url;
82
+ jsonRun = new JSONRun(field.value, paragraphStyles, fieldtype);
83
+ runs = [...runs, ...jsonRun.runs];
84
+ } else {
85
+ jsonRun = new JSONRun(field.value, paragraphStyles);
86
+ runs = [...runs, ...jsonRun.runs];
87
+ }
88
+ return runs;
89
+ } //generateJsonParagraph
90
+
91
+ getJSONParagraph(): Paragraph {
92
+ return this.paragraphTemplate;
93
+ } //getJSONTable
94
+ } //class
@@ -0,0 +1,75 @@
1
+ import logger from "../../../services/logger";
2
+ import {
3
+ Table,
4
+ TableRow,
5
+ WIQueryResults,
6
+ WIData,
7
+ WIProperty,
8
+ StyleOptions,
9
+ } from "../wordJsonModels";
10
+ import JSONTableRow from "./JSONTableRow";
11
+ export default class JSONTable {
12
+ tableTemplate: Table;
13
+ tableStyles: StyleOptions;
14
+
15
+ constructor(
16
+ data: WIQueryResults,
17
+ tableStyles: StyleOptions,
18
+ headingLvl: number
19
+ ) {
20
+ this.tableStyles = tableStyles;
21
+ this.tableTemplate = {
22
+ type: "table",
23
+ headingLevel: headingLvl,
24
+ Rows: this.generateJsonRows(data, this.tableStyles),
25
+ };
26
+ }
27
+
28
+ generateJsonRows(data: any, tableStyles: StyleOptions): TableRow[] {
29
+ let rows: TableRow[] = [];
30
+ let headersRowData: WIData = this.headersRowAdapter(data[0]);
31
+
32
+ let style2 = {
33
+ isBold: true,
34
+ IsItalic: false,
35
+ IsUnderline: false,
36
+ Size: 12,
37
+ Uri: null,
38
+ Font: "Arial",
39
+ InsertLineBreak: false,
40
+ InsertSpace: false,
41
+ };
42
+ let headersRow = new JSONTableRow(headersRowData, style2);
43
+
44
+ rows.push(headersRow.getRow());
45
+
46
+ data.forEach((rowData: WIData) => {
47
+ let row = new JSONTableRow(rowData, tableStyles);
48
+ rows.push(row.getRow());
49
+ });
50
+
51
+ return rows;
52
+ } //generateJsonRows
53
+
54
+ headersRowAdapter(data: WIData): WIData {
55
+ let headerValuesWi;
56
+ try {
57
+ headerValuesWi = data.fields.map((field: WIProperty) => {
58
+ return { name: "header", value: field.name };
59
+ });
60
+
61
+ } catch (error) {
62
+ logger.error(`no fields to append`);
63
+ }
64
+ return {
65
+ url: "",
66
+ fields: headerValuesWi,
67
+ Source: 999999999999,
68
+ level: data.level,
69
+ };
70
+ } //headersRowAdapter
71
+
72
+ getJSONTable(): Table {
73
+ return this.tableTemplate;
74
+ } //getJSONTable
75
+ } //class
@@ -0,0 +1,117 @@
1
+ import { extname } from "path";
2
+ import {
3
+ TableCell,
4
+ Run,
5
+ Attachment,
6
+ WIProperty,
7
+ MultipeValuesWIProperty,
8
+ StyleOptions,
9
+ JsonHtml,
10
+ } from "../wordJsonModels";
11
+ import JSONRun from "../JSONRun";
12
+ import logger from "../../../services/logger";
13
+
14
+ export default class JSONTableCell {
15
+ cell: TableCell;
16
+ constructor(data: WIProperty, tableStyles: StyleOptions) {
17
+ this.cell = this.generateJsonCell(data, tableStyles);
18
+ }
19
+
20
+ generateJsonCell(
21
+ data: WIProperty | MultipeValuesWIProperty,
22
+ styles
23
+ ): TableCell {
24
+ let runs: Run[] = [];
25
+ let attachments: Attachment[] = [];
26
+ let HtmlData: string = ""
27
+
28
+ //multiple values
29
+ if (
30
+ typeof data.value !== "string" &&
31
+ typeof data.value !== "number" &&
32
+ data.value
33
+ ) {
34
+ data.value.forEach((runData) => {
35
+ //check if photo type
36
+ if (this.isPictureUri(runData.tableCellAttachmentLink)) {
37
+ let attachment = {
38
+ type: "Picture",
39
+ path: runData.tableCellAttachmentLink,
40
+ };
41
+ attachments.push(attachment);
42
+ }
43
+ else if(/<[^>]*>/.test(runData.value)) {
44
+ HtmlData = runData.value || "";
45
+ }
46
+ else {
47
+ styles.Uri = runData.relativeUrl
48
+ ? runData.relativeUrl
49
+ : runData.Uri
50
+ ? runData.Uri
51
+ : null;
52
+ styles.InsertLineBreak = false;
53
+ let jsonRun = new JSONRun(runData.value, styles);
54
+ runs = [...runs, ...jsonRun.getRun()];
55
+ }
56
+ });
57
+ }
58
+ else if(/<[^>]*>/.test(data.value)) {
59
+ HtmlData = data.value || "";
60
+ }
61
+ else {
62
+ styles.Uri = data.relativeUrl
63
+ ? data.relativeUrl
64
+ : data.url
65
+ ? data.url
66
+ : null;
67
+ let text = data.value || "";
68
+ let jsonRun = new JSONRun(`${text}`, styles);
69
+ runs = [...runs, ...jsonRun.getRun()];
70
+ }
71
+ if (HtmlData!= "")
72
+ {
73
+ let Html:JsonHtml = {
74
+ type:"html",
75
+ Html:HtmlData
76
+ }
77
+ return {
78
+ attachments: attachments,
79
+ Paragraphs: [
80
+ {
81
+ Runs: runs,
82
+ },
83
+ ],
84
+ Html
85
+ };
86
+ }
87
+ else{
88
+ return {
89
+ attachments: attachments,
90
+ Paragraphs: [
91
+ {
92
+ Runs: runs,
93
+ },
94
+ ],
95
+ };
96
+ }
97
+ } //generateJsonCells
98
+
99
+ isPictureUri(uri: string) {
100
+ try {
101
+ let fileExt = extname(uri);
102
+ logger.debug(`attachment extension: ${fileExt}`);
103
+ if (fileExt == ".PNG" || fileExt == ".jpg") {
104
+ return true;
105
+ } else {
106
+ return false;
107
+ }
108
+ } catch (e) {
109
+ logger.error(`error parsing file extension for : ${uri}`);
110
+ return false;
111
+ }
112
+ }
113
+
114
+ getJsonCell(): TableCell {
115
+ return this.cell;
116
+ }
117
+ } //class
@@ -0,0 +1,34 @@
1
+ import {
2
+ TableRow,
3
+ TableCell,
4
+ WIData,
5
+ WIProperty,
6
+ StyleOptions
7
+ } from "../wordJsonModels";
8
+ import JSONTableCell from "./JSONTableCell";
9
+
10
+ export default class JSONTableRow {
11
+ row: TableRow;
12
+
13
+ constructor(data: WIData, tableStyles: StyleOptions) {
14
+ this.row = this.generateJsonRow(data, tableStyles);
15
+ } //constructor
16
+
17
+ generateJsonRow(data: WIData, styles: StyleOptions): TableRow {
18
+ let tableCells: TableCell[] = [];
19
+
20
+ data.fields.forEach((wiProperty: WIProperty) => {
21
+ if (wiProperty.name === "id") {
22
+ styles.Uri = data.url;
23
+ }
24
+ let jsonTableCell = new JSONTableCell(wiProperty, styles);
25
+ tableCells.push(JSON.parse(JSON.stringify(jsonTableCell.getJsonCell())));
26
+ });
27
+
28
+ return { Cells: tableCells };
29
+ } //generateJsonRow
30
+
31
+ getRow(): TableRow {
32
+ return this.row;
33
+ } //getRow
34
+ } //class
@@ -0,0 +1,97 @@
1
+ export interface StyleOptions {
2
+ isBold: boolean;
3
+ IsItalic: boolean;
4
+ IsUnderline: boolean;
5
+ Size: number;
6
+ Uri: string;
7
+ Font: string;
8
+ InsertLineBreak: boolean;
9
+ InsertSpace: boolean;
10
+ }
11
+ export interface DocumentSkin {
12
+ templatePath: string;
13
+ contentControls: ContetControlSkin[];
14
+ }
15
+ export interface ContetControlSkin {
16
+ title: string;
17
+ wordObjects: any[]; //can be table or paragraph or file
18
+ }
19
+ export interface JsonHtml {
20
+ type: string;
21
+ Html: string;
22
+ }
23
+ export interface TableRow {
24
+ Cells: TableCell[];
25
+ }
26
+ export interface TableCell {
27
+ attachments?: Attachment[];
28
+ Paragraphs: [
29
+ {
30
+ Runs: Run[];
31
+ }
32
+ ];
33
+ Html?:JsonHtml
34
+ }
35
+ export interface Paragraph {
36
+ type: string;
37
+ headingLevel?: number;
38
+ runs: Run[];
39
+ }
40
+ export interface Attachment {
41
+ type: string;
42
+ path: string;
43
+ }
44
+ export interface Run {
45
+ text: string;
46
+ Bold: boolean;
47
+ Italic: boolean;
48
+ Underline: boolean;
49
+ Size: number;
50
+ Uri: string;
51
+ Font: string;
52
+ InsertLineBreak: boolean;
53
+ InsertSpace: boolean;
54
+ }
55
+ export interface WorkItemData {}
56
+
57
+ export interface Table {
58
+ type: string;
59
+ headingLevel: number;
60
+ Rows: TableRow[];
61
+ }
62
+ export interface WIQueryResults {
63
+ columns: WIColumns[];
64
+ workItems: WIData[];
65
+ asOf: string;
66
+ queryResultType: number;
67
+ queryType: number;
68
+ }
69
+ export interface WIData {
70
+ url: string;
71
+ fields: WIProperty[];
72
+ Source: number;
73
+ level: number;
74
+ }
75
+ export interface WIProperty {
76
+ name: string;
77
+ value: any;
78
+ url?: string;
79
+ relativeUrl?: string;
80
+ attachmentLink?: string;
81
+ relativeAttachmentLink?: string;
82
+ richText?: any[];
83
+ }
84
+ export interface MultipeValuesWIProperty {
85
+ name: string;
86
+ value: any[];
87
+ url?: string;
88
+ relativeUrl?: string;
89
+ attachmentLink?: string;
90
+ relativeAttachmentLink?: string;
91
+ richText?: any[];
92
+ }
93
+ export interface WIColumns {
94
+ name: string;
95
+ referenceName: string;
96
+ url: string;
97
+ }