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GAAG,EAAM,IAAI;IACb,KAAK,EAAI,IAAI;IACb,IAAI,EAAK,IAAI;IACb,IAAI,EAAK,IAAI;IACb,KAAK,EAAI,IAAI;IACb,MAAM,EAAG,IAAI;CAChB,CAAC;AAKK,0BAAQ,GAAG;IACd,GAAG,EAAO,IAAI;IACd,IAAI,EAAM,IAAI;IACd,QAAQ,EAAE,IAAI;IACd,MAAM,EAAI,IAAI;IACd,KAAK,EAAK,IAAI;CACjB,CAAC;AA9BO,iBAAiB;IAD7B,sBAAS,CAAC,EAAC,IAAI,EAAE,cAAc,EAAC,CAAC;GACrB,iBAAiB,CAkI7B;AAlIY,8CAAiB","sourcesContent":["import { Component, RendererComponent } from '../components';\nimport { PageEvent } from '../events';\n\n/**\n * List of states the parser of [[PrettyPrintPlugin]] can be in.\n */\nenum PrettyPrintState {\n /**\n * Default state of the parser. Empty lines will be removed and indention will be adjusted.\n */\n Default,\n\n /**\n * Comment state, the parser waits for a comment closing tag.\n */\n Comment,\n\n /**\n * Pre state, the parser waits for the closing tag of the current pre block.\n */\n Pre\n}\n\n/**\n * A plugin that pretty prints the generated html.\n *\n * This not only aids in making the generated html source code more readable, by removing\n * blank lines and unnecessary whitespaces the size of the documentation is reduced without\n * visual impact.\n *\n * At the point writing this the docs of TypeDoc took 97.8 MB without and 66.4 MB with this\n * plugin enabled, so it reduced the size to 68% of the original output.\n */\n@Component({name: 'pretty-print'})\nexport class PrettyPrintPlugin extends RendererComponent {\n /**\n * Map of all tags that will be ignored.\n */\n static IGNORED_TAGS = {\n area: true,\n base: true,\n br: true,\n wbr: true,\n col: true,\n command: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n link: true,\n meta: true,\n param: true,\n source: true\n };\n\n /**\n * Map of all tags that prevent this plugin form modifying the following code.\n */\n static PRE_TAGS = {\n pre: true,\n code: true,\n textarea: true,\n script: true,\n style: true\n };\n\n /**\n * Create a new PrettyPrintPlugin instance.\n */\n initialize() {\n this.listenTo(this.owner, PageEvent.END, this.onRendererEndPage, -1024);\n }\n\n /**\n * Triggered after a document has been rendered, just before it is written to disc.\n *\n * @param event\n */\n onRendererEndPage(event: PageEvent) {\n let match: RegExpMatchArray | null;\n let line: string;\n let lineState: PrettyPrintState;\n let lineDepth: number;\n let tagName: string;\n let preName: string | undefined;\n\n let tagExp = /<\\s*(\\w+)[^>]*>|<\\/\\s*(\\w+)[^>]*>|<!--|-->/g;\n let emptyLineExp = /^[\\s]*$/;\n let minLineDepth = 1;\n let state = PrettyPrintState.Default;\n const stack: string[] = [];\n\n const lines = (event.contents || '').split(/\\r\\n?|\\n/);\n let index = 0;\n let count = lines.length;\n\n while (index < count) {\n line = lines[index];\n if (emptyLineExp.test(line)) {\n if (state === PrettyPrintState.Default) {\n lines.splice(index, 1);\n count -= 1;\n continue;\n }\n } else {\n lineState = state;\n lineDepth = stack.length;\n\n while (match = tagExp.exec(line)) {\n if (state === PrettyPrintState.Comment) {\n if (match[0] === '-->') {\n state = PrettyPrintState.Default;\n }\n } else if (state === PrettyPrintState.Pre) {\n if (match[2] && match[2].toLowerCase() === preName) {\n state = PrettyPrintState.Default;\n }\n } else {\n if (match[0] === '<!--') {\n state = PrettyPrintState.Comment;\n } else if (match[1]) {\n tagName = match[1].toLowerCase();\n if (tagName in PrettyPrintPlugin.IGNORED_TAGS) {\n continue;\n }\n if (tagName in PrettyPrintPlugin.PRE_TAGS) {\n state = PrettyPrintState.Pre;\n preName = tagName;\n } else {\n if (tagName === 'body') {\n minLineDepth = 2;\n }\n stack.push(tagName);\n }\n } else if (match[2]) {\n tagName = match[2].toLowerCase();\n if (tagName in PrettyPrintPlugin.IGNORED_TAGS) {\n continue;\n }\n\n const n = stack.lastIndexOf(tagName);\n if (n !== -1) {\n stack.length = n;\n }\n }\n }\n }\n\n if (lineState === PrettyPrintState.Default) {\n lineDepth = Math.min(lineDepth, stack.length);\n line = line.replace(/^\\s+/, '').replace(/\\s+$/, '');\n if (lineDepth > minLineDepth) {\n line = Array(lineDepth - minLineDepth + 1).join('\\t') + line;\n }\n\n lines[index] = line;\n }\n }\n\n index++;\n }\n\n event.contents = lines.join('\\n');\n }\n}\n"]}
\No newline at end of file