"var Converter = require(\"./converter.js\").Converter;\nvar Node = require(\"../node.js\").Node;\n\nfunction ConverterHTML(orgDocument, exportOptions) {\n this.initialize(orgDocument, exportOptions);\n this.result = this.convert();\n}\n\nConverterHTML.prototype = {\n __proto__: Converter.prototype,\n\n convert: function () {\n var title = this.orgDocument.title ? this.convertNode(this.orgDocument.title) : this.untitled;\n var titleHTML = this.tag(\"h\" + Math.max(Number(this.headerOffset), 1), title);\n var contentHTML = this.convertNodes(this.orgDocument.nodes, true /* record headers */);\n var toc = this.computeToc(this.documentOptions[\"toc\"]);\n var tocHTML = this.tocToHTML(toc);\n\n return {\n title: title,\n titleHTML: titleHTML,\n contentHTML: contentHTML,\n tocHTML: tocHTML,\n toc: toc,\n toString: function () {\n return titleHTML + tocHTML + \"\\n\" + contentHTML;\n }\n };\n },\n\n tocToHTML: function (toc) {\n function tocToHTMLFunction(tocList) {\n var html = \"\";\n for (var i = 0; i < tocList.length; ++i) {\n var tocItem = tocList[i];\n var sectionNumberText = tocItem.headerNode.sectionNumberText;\n var sectionNumber = this.documentOptions.num ?\n this.inlineTag(\"span\", sectionNumberText, {\n\"class\": \"section-number\"\n }) : \"\";\n var header = this.getNodeTextContent(tocItem.headerNode);\n var headerLink = this.inlineTag(\"a\", sectionNumber + header, {\n href: \"#header-\" + sectionNumberText.replace(/\\./g, \"-\")\n });\n var subList = tocItem.childTocs.length ? tocToHTMLFunction.call(this, tocItem.childTocs) : \"\";\n html += this.tag(\"li\", headerLink + subList);\n }\n return this.tag(\"ul\", html);\n }\n\n return tocToHTMLFunction.call(this, toc);\n },\n\n computeAuxDataForNode: function (node) {\n while (node.parent &&\n node.parent.type === Node.types.inlineContainer) {\n node = node.parent;\n }\n var attributesNode = node.previousSibling;\n var attributesText = \"\";\n while (attributesNode &&\n attributesNode.type === Node.types.directive &&\n attributesNode.directiveName === \"attr_html:\") {\n attributesText += attributesNode.directiveRawValue + \"\";\n attributesNode = attributesNode.previousSibling;\n }\n return attributesText;\n },\n\n // Method to construct org-js generated class\n orgClassName: function (className) {\n return this.exportOptions.htmlClassPrefix ?\n this.exportOptions.htmlClassPrefix + className\n : className;\n },\n\n // Method to construct org-js generated id\n orgId: function (id) {\n return this.exportOptions.htmlIdPrefix ?\n this.exportOptions.htmlIdPrefix + id\n : id;\n },\n\n // ----------------------------------------------------\n // Node conversion\n // ----------------------------------------------------\n\n convertHeader: function (node, childText, auxData,\n taskStatus, sectionNumberText) {\n var headerAttributes = {};\n\n if (taskStatus) {\n childText = this.inlineTag(\"span\", childText.substring(0, 4), {\n\"class\": \"task-status \" + taskStatus\n }) + childText.substring(5);\n }\n\n if (sectionNumberText) {\n childText = this.inlineTag(\"span\", sectionNumberText, {\n\"class\": \"section-number\"\n }) + childText;\n headerAttributes[\"id\"] = \"header-\" + sectionNumberText.replace(/\\./g, \"-\");\n }\n\n if (taskStatus)\n headerAttributes[\"class\"] = \"task-status \" + taskStatus;\n\n return this.tag(\"h\" + (this.headerOffset + node.level),\n childText, headerAttributes, auxData);\n },\n\n convertOrderedList: function (node, childText, auxData) {\n return this.tag(\"ol\", childText, null, auxData);\n },\n\n convertUnorderedList: function (node, childText, auxData) {\n return this.tag(\"ul\", childText, null, auxData);\n },\n\n convertDefinitionList: function (node, childText, auxData) {\n return this.tag(\"dl\", childText, null, auxData);\n },\n\n convertDefinitionItem: function (node, childText, auxData,\n term, definition) {\n return this.tag(\"dt\", term) + this.tag(\"dd\", definition);\n },\n\n convertListItem: function (node, childText, auxData) {\n if (this.exportOptions.suppressCheckboxHandling) {\n return this.tag(\"li\", childText, null, auxData);\n } else {\n var listItemAttributes = {};\n var listItemText = childText;\n // Embed checkbox\n if (/^\\s*\\[(X| |-)\\]([\\s\\S]*)/.exec(listItemText)) {\n listItemText = RegExp.$2 ;\n var checkboxIndicator = RegExp.$1;\n\n var checkboxAttributes = { type: \"checkbox\" };\n switch (checkboxIndicator) {\n case \"X\":\n checkboxAttributes[\"checked\"] = \"true\";\n listItemAttributes[\"data-checkbox-status\"] = \"done\";\n break;\n case \"-\":\n listItemAttributes[\"data-checkbox-status\"] = \"intermediate\";\n break;\n default:\n listItemAttributes[\"data-checkbox-status\"] = \"undone\";\n break;\n }\n\n listItemText = this.inlineTag(\"input\", null, checkboxAttributes) + listItemText;\n }\n\n return this.tag(\"li\", listItemText, listItemAttributes, auxData);\n }\n },\n\n convertParagraph: function (node, childText, auxData) {\n return this.tag(\"p\", childText, null, auxData);\n },\n\n convertPreformatted: function (node, childText, auxData) {\n return this.tag(\"pre\", childText, null, auxData);\n },\n\n convertTable: function (node, childText, auxData) {\n return this.tag(\"table\", this.tag(\"tbody\", childText), null, auxData);\n },\n\n convertTableRow: function (node, childText, auxData) {\n return this.tag(\"tr\", childText);\n },\n\n convertTableHeader: function (node, childText, auxData) {\n return this.tag(\"th\", childText);\n },\n\n convertTableCell: function (node, childText, auxData) {\n return this.tag(\"td\", childText);\n },\n\n convertHorizontalRule: function (node, childText, auxData) {\n return this.tag(\"hr\", null, null, auxData);\n },\n\n convertInlineContainer: function (node, childText, auxData) {\n return childText;\n },\n\n convertBold: function (node, childText, auxData) {\n return this.inlineTag(\"b\", childText);\n },\n\n convertItalic: function (node, childText, auxData) {\n return this.inlineTag(\"i\", childText);\n },\n\n convertUnderline: function (node, childText, auxData) {\n return this.inlineTag(\"span\", childText, {\n style: \"text-decoration:underline;\"\n });\n },\n\n convertCode: function (node, childText, auxData) {\n return this.inlineTag(\"code\", childText);\n },\n\n convertDashed: function (node, childText, auxData) {\n return this.inlineTag(\"del\", childText);\n },\n\n convertLink: function (node, childText, auxData) {\n var srcParameterStripped = this.stripParametersFromURL(node.src);\n if (this.imageExtensionPattern.exec(srcParameterStripped)) {\n var imgText = this.getNodeTextContent(node);\n return this.inlineTag(\"img\", null, {\n src: node.src,\n alt: imgText,\n title: imgText\n }, auxData);\n } else {\n return this.inlineTag(\"a\", childText, { href: node.src });\n }\n },\n\n convertQuote: function (node, childText, auxData) {\n return this.tag(\"blockquote\", childText, null, auxData);\n },\n\n convertExample: function (node, childText, auxData) {\n return this.tag(\"pre\", childText, null, auxData);\n },\n\n convertSrc: function (node, childText, auxData) {\n var codeLanguage = node.directiveArguments.length\n ? node.directiveArguments[0]\n : \"unknown\";\n childText = this.tag(\"code\", childText, {\n\"class\": \"language-\" + codeLanguage\n }, auxData);\n return this.tag(\"pre\", childText, {\n\"class\": \"prettyprint\"\n });\n },\n\n // @override\n convertHTML: function (node, childText, auxData) {\n if (node.directiveName === \"html:\") {\n return node.directiveRawValue;\n } else if (node.directiveName === \"html\") {\n return node.children.map(function (textNode) {\n return textNode.value;\n }).join(\"\\n\");\n } else {\n return childText;\n }\n },\n\n // @implement\n convertHeaderBlock: function (headerBlock, level, index) {\n level = level || 0;\n index = index || 0;\n\n var contents = [];\n\n var headerNode = headerBlock.header;\n if (headerNode) {\n contents.push(this.convertNode(headerNode));\n }\n\n var blockContent = this.convertNodes(headerBlock.childNodes);\n contents.push(blockContent);\n\n var childBlockContent = headerBlock.childBlocks\n .map(function (block, idx) {\n return this.convertHeaderBlock(block, level + 1, idx);\n }, this)\n .join(\"\\n\");\n contents.push(childBlockContent);\n\n var contentsText = contents.join(\"\\n\");\n\n if (headerNode) {\n return this.tag(\"section\", \"\\n\" + contents.join(\"\\n\"), {\n\"class\": \"block block-level-\" + level\n });\n } else {\n return contentsText;\n }\n },\n\n // ----------------------------------------------------\n // Supplemental methods\n // ----------------------------------------------------\n\n replaceMap: {\n // [replacing pattern, predicate]\n\"&\": [\"&\", null],\n\"<\": [\"<\", null],\n\">\": [\">\", null],\n '\"': [\""\", null],\n\"'\": [\"'\", null],\n\"->\": [\"➔\", function (text, insideCodeElement) {\n return this.exportOptions.translateSymbolArrow && !insideCodeElement;\n }]\n },\n\n replaceRegexp: null,\n\n // @implement @override\n escapeSpecialChars: function (text, insideCodeElement) {\n if (!this.replaceRegexp) {\n this.replaceRegexp = new RegExp(Object.keys(this.replaceMap).join(\"|\"), \"g\");\n }\n\n var replaceMap = this.replaceMap;\n var self = this;\n return text.replace(this.replaceRegexp, function (matched) {\n if (!replaceMap[matched]) {\n throw Error(\"escapeSpecialChars: Invalid match\");\n }\n\n var predicate = replaceMap[matched][1];\n if (typeof predicate === \"function\" &&\n !predicate.call(self, text, insideCodeElement)) {\n // Not fullfill the predicate\n return matched;\n }\n\n return replaceMap[matched][0];\n });\n },\n\n // @implement\n postProcess: function (node, currentText, insideCodeElement) {\n if (this.exportOptions.exportFromLineNumber &&\n typeof node.fromLineNumber === \"number\") {\n // Wrap with line number information\n currentText = this.inlineTag(\"div\", currentText, {\n\"data-line-number\": node.fromLineNumber\n });\n }\n return currentText;\n },\n\n // @implement\n makeLink: function (url) {\n return \"<a href=\\\"\" + url + \"\\\">\" + decodeURIComponent(url) + \"</a>\";\n },\n\n // @implement\n makeSubscript: function (match, body, subscript) {\n return \"<span class=\\\"org-subscript-parent\\\">\" +\n body +\n\"</span><span class=\\\"org-subscript-child\\\">\" +\n subscript +\n\"</span>\";\n },\n\n // ----------------------------------------------------\n // Specific methods\n // ----------------------------------------------------\n\n attributesObjectToString: function (attributesObject) {\n var attributesString = \"\";\n for (var attributeName in attributesObject) {\n if (attributesObject.hasOwnProperty(attributeName)) {\n var attributeValue = attributesObject[attributeName];\n // To avoid id/class name conflicts with other frameworks,\n // users can add arbitrary prefix to org-js generated\n // ids/classes via exportOptions.\n if (attributeName === \"class\") {\n attributeValue = this.orgClassName(attributeValue);\n } else if (attributeName === \"id\") {\n attributeValue = this.orgId(attributeValue);\n }\n attributesString += \"\" + attributeName + \"=\\\"\" + attributeValue + \"\\\"\";\n }\n }\n return attributesString;\n },\n\n inlineTag: function (name, innerText, attributesObject, auxAttributesText) {\n attributesObject = attributesObject || {};\n\n var htmlString = \"<\" + name;\n // TODO: check duplicated attributes\n if (auxAttributesText)\n htmlString += \"\" + auxAttributesText;\n htmlString += this.attributesObjectToString(attributesObject);\n\n if (innerText === null)\n return htmlString + \"/>\";\n\n htmlString += \">\" + innerText + \"</\" + name + \">\";\n\n return htmlString;\n },\n\n tag: function (name, innerText, attributesObject, auxAttributesText) {\n return this.inlineTag(name, innerText, attributesObject, auxAttributesText) + \"\\n\";\n }\n};\n\nif (typeof exports !== \"undefined\")\n exports.ConverterHTML = ConverterHTML;\n",
"var Node = require(\"../node.js\").Node;\n\nfunction Converter() {\n}\n\nConverter.prototype = {\n exportOptions: {\n headerOffset: 1,\n exportFromLineNumber: false,\n suppressSubScriptHandling: false,\n suppressAutoLink: false,\n // HTML\n translateSymbolArrow: false,\n suppressCheckboxHandling: false,\n // { \"directive:\": function (node, childText, auxData) {} }\n customDirectiveHandler: null,\n // e.g., \"org-js-\"\n htmlClassPrefix: null,\n htmlIdPrefix: null\n },\n\n untitled: \"Untitled\",\n result: null,\n\n // TODO: Manage TODO lists\n\n initialize: function (orgDocument, exportOptions) {\n this.orgDocument = orgDocument;\n this.documentOptions = orgDocument.options || {};\n this.exportOptions = exportOptions || {};\n\n this.headers = [];\n this.headerOffset =\n typeof this.exportOptions.headerOffset === \"number\" ? this.exportOptions.headerOffset : 1;\n this.sectionNumbers = [0];\n },\n\n createTocItem: function (headerNode, parentTocs) {\n var childTocs = [];\n childTocs.parent = parentTocs;\n var tocItem = { headerNode: headerNode, childTocs: childTocs };\n return tocItem;\n },\n\n computeToc: function (exportTocLevel) {\n if (typeof exportTocLevel !== \"number\")\n exportTocLevel = Infinity;\n\n var toc = [];\n toc.parent = null;\n\n var previousLevel = 1;\n var currentTocs = toc; // first\n\n for (var i = 0; i < this.headers.length; ++i) {\n var headerNode = this.headers[i];\n\n if (headerNode.level > exportTocLevel)\n continue;\n\n var levelDiff = headerNode.level - previousLevel;\n if (levelDiff > 0) {\n for (var j = 0; j < levelDiff; ++j) {\n if (currentTocs.length === 0) {\n // Create a dummy tocItem\n var dummyHeader = Node.createHeader([], {\n level: previousLevel + j\n });\n dummyHeader.sectionNumberText = \"\";\n currentTocs.push(this.createTocItem(dummyHeader, currentTocs));\n }\n currentTocs = currentTocs[currentTocs.length - 1].childTocs;\n }\n } else if (levelDiff < 0) {\n levelDiff = -levelDiff;\n for (var k = 0; k < levelDiff; ++k) {\n currentTocs = currentTocs.parent;\n }\n }\n\n currentTocs.push(this.createTocItem(headerNode, currentTocs));\n\n previousLevel = headerNode.level;\n }\n\n return toc;\n },\n\n convertNode: function (node, recordHeader, insideCodeElement) {\n if (!insideCodeElement) {\n if (node.type === Node.types.directive) {\n if (node.directiveName === \"example\" ||\n node.directiveName === \"src\") {\n insideCodeElement = true;\n }\n } else if (node.type === Node.types.preformatted) {\n insideCodeElement = true;\n }\n }\n\n if (typeof node === \"string\") {\n node = Node.createText(null, { value: node });\n }\n\n var childText = node.children ? this.convertNodesInternal(node.children, recordHeader, insideCodeElement) : \"\";\n var text;\n\n var auxData = this.computeAuxDataForNode(node);\n\n switch (node.type) {\n case Node.types.header:\n // Parse task status\n var taskStatus = null;\n if (childText.indexOf(\"TODO \") === 0)\n taskStatus = \"todo\";\n else if (childText.indexOf(\"DONE \") === 0)\n taskStatus = \"done\";\n\n // Compute section number\n var sectionNumberText = null;\n if (recordHeader) {\n var thisHeaderLevel = node.level;\n var previousHeaderLevel = this.sectionNumbers.length;\n if (thisHeaderLevel > previousHeaderLevel) {\n // Fill missing section number\n var levelDiff = thisHeaderLevel - previousHeaderLevel;\n for (var j = 0; j < levelDiff; ++j) {\n this.sectionNumbers[thisHeaderLevel - 1 - j] = 0; // Extend\n }\n } else if (thisHeaderLevel < previousHeaderLevel) {\n this.sectionNumbers.length = thisHeaderLevel; // Collapse\n }\n this.sectionNumbers[thisHeaderLevel - 1]++;\n sectionNumberText = this.sectionNumbers.join(\".\");\n node.sectionNumberText = sectionNumberText; // Can be used in ToC\n }\n\n text = this.convertHeader(node, childText, auxData,\n taskStatus, sectionNumberText);\n\n if (recordHeader)\n this.headers.push(node);\n break;\n case Node.types.orderedList:\n text = this.convertOrderedList(node, childText, auxData);\n break;\n case Node.types.unorderedList:\n text = this.convertUnorderedList(node, childText, auxData);\n break;\n case Node.types.definitionList:\n text = this.convertDefinitionList(node, childText, auxData);\n break;\n case Node.types.listElement:\n if (node.isDefinitionList) {\n var termText = this.convertNodesInternal(node.term, recordHeader, insideCodeElement);\n text = this.convertDefinitionItem(node, childText, auxData,\n termText, childText);\n } else {\n text = this.convertListItem(node, childText, auxData);\n }\n break;\n case Node.types.paragraph:\n text = this.convertParagraph(node, childText, auxData);\n break;\n case Node.types.preformatted:\n text = this.convertPreformatted(node, childText, auxData);\n break;\n case Node.types.table:\n text = this.convertTable(node, childText, auxData);\n break;\n case Node.types.tableRow:\n text = this.convertTableRow(node, childText, auxData);\n break;\n case Node.types.tableCell:\n if (node.isHeader)\n text = this.convertTableHeader(node, childText, auxData);\n else\n text = this.convertTableCell(node, childText, auxData);\n break;\n case Node.types.horizontalRule:\n text = this.convertHorizontalRule(node, childText, auxData);\n break;\n // ============================================================ //\n // Inline\n // ============================================================ //\n case Node.types.inlineContainer:\n text = this.convertInlineContainer(node, childText, auxData);\n break;\n case Node.types.bold:\n text = this.convertBold(node, childText, auxData);\n break;\n case Node.types.italic:\n text = this.convertItalic(node, childText, auxData);\n break;\n case Node.types.underline:\n text = this.convertUnderline(node, childText, auxData);\n break;\n case Node.types.code:\n text = this.convertCode(node, childText, auxData);\n break;\n case Node.types.dashed:\n text = this.convertDashed(node, childText, auxData);\n break;\n case Node.types.link:\n text = this.convertLink(node, childText, auxData);\n break;\n case Node.types.directive:\n switch (node.directiveName) {\n case \"quote\":\n text = this.convertQuote(node, childText, auxData);\n break;\n case \"example\":\n text = this.convertExample(node, childText, auxData);\n break;\n case \"src\":\n text = this.convertSrc(node, childText, auxData);\n break;\n case \"html\":\n case \"html:\":\n text = this.convertHTML(node, childText, auxData);\n break;\n default:\n if (this.exportOptions.customDirectiveHandler &&\n this.exportOptions.customDirectiveHandler[node.directiveName]) {\n text = this.exportOptions.customDirectiveHandler[node.directiveName](\n node, childText, auxData\n );\n } else {\n text = childText;\n }\n }\n break;\n case Node.types.text:\n text = this.convertText(node.value, insideCodeElement);\n break;\n default:\n throw Error(\"Unknown node type: \" + node.type);\n }\n\n if (typeof this.postProcess === \"function\") {\n text = this.postProcess(node, text, insideCodeElement);\n }\n\n return text;\n },\n\n convertText: function (text, insideCodeElement) {\n var escapedText = this.escapeSpecialChars(text, insideCodeElement);\n\n if (!this.exportOptions.suppressSubScriptHandling && !insideCodeElement) {\n escapedText = this.makeSubscripts(escapedText, insideCodeElement);\n }\n if (!this.exportOptions.suppressAutoLink) {\n escapedText = this.linkURL(escapedText);\n }\n\n return escapedText;\n },\n\n // By default, ignore html\n convertHTML: function (node, childText, auxData) {\n return childText;\n },\n\n convertNodesInternal: function (nodes, recordHeader, insideCodeElement) {\n var nodesTexts = [];\n for (var i = 0; i < nodes.length; ++i) {\n var node = nodes[i];\n var nodeText = this.convertNode(node, recordHeader, insideCodeElement);\n nodesTexts.push(nodeText);\n }\n return this.combineNodesTexts(nodesTexts);\n },\n\n convertHeaderBlock: function (headerBlock, recordHeader) {\n throw Error(\"convertHeaderBlock is not implemented\");\n },\n\n convertHeaderTree: function (headerTree, recordHeader) {\n return this.convertHeaderBlock(headerTree, recordHeader);\n },\n\n convertNodesToHeaderTree: function (nodes, nextBlockBegin, blockHeader) {\n var childBlocks = [];\n var childNodes = [];\n\n if (typeof nextBlockBegin === \"undefined\") {\n nextBlockBegin = 0;\n }\n if (typeof blockHeader === \"undefined\") {\n blockHeader = null;\n }\n\n for (var i = nextBlockBegin; i < nodes.length;) {\n var node = nodes[i];\n\n var isHeader = node.type === Node.types.header;\n\n if (!isHeader) {\n childNodes.push(node);\n i = i + 1;\n continue;\n }\n\n // Header\n if (blockHeader && node.level <= blockHeader.level) {\n // Finish Block\n break;\n } else {\n // blockHeader.level < node.level\n // Begin child block\n var childBlock = this.convertNodesToHeaderTree(nodes, i + 1, node);\n childBlocks.push(childBlock);\n i = childBlock.nextIndex;\n }\n }\n\n // Finish block\n return {\n header: blockHeader,\n childNodes: childNodes,\n nextIndex: i,\n childBlocks: childBlocks\n };\n },\n\n convertNodes: function (nodes, recordHeader, insideCodeElement) {\n return this.convertNodesInternal(nodes, recordHeader, insideCodeElement);\n },\n\n combineNodesTexts: function (nodesTexts) {\n return nodesTexts.join(\"\");\n },\n\n getNodeTextContent: function (node) {\n if (node.type === Node.types.text)\n return this.escapeSpecialChars(node.value);\n else\n return node.children ? node.children.map(this.getNodeTextContent, this).join(\"\") : \"\";\n },\n\n // @Override\n escapeSpecialChars: function (text) {\n throw Error(\"Implement escapeSpecialChars\");\n },\n\n // http://daringfireball.net/2010/07/improved_regex_for_matching_urls\n urlPattern: /\\b(?:https?:\\/\\/|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}\\/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?\u00ab\u00bb\u201c\u201d\u2018\u2019])/ig,\n\n // @Override\n linkURL: function (text) {\n var self = this;\n return text.replace(this.urlPattern, function (matched) {\n if (matched.indexOf(\"://\") < 0)\n matched = \"http://\" + matched;\n return self.makeLink(matched);\n });\n },\n\n makeLink: function (url) {\n throw Error(\"Implement makeLink\");\n },\n\n makeSubscripts: function (text) {\n if (this.documentOptions[\"^\"] === \"{}\")\n return text.replace(/\\b([^_ \\t]*)_{([^}]*)}/g,\n this.makeSubscript);\n else if (this.documentOptions[\"^\"])\n return text.replace(/\\b([^_ \\t]*)_([^_]*)\\b/g,\n this.makeSubscript);\n else\n return text;\n },\n\n makeSubscript: function (match, body, subscript) {\n throw Error(\"Implement makeSubscript\");\n },\n\n stripParametersFromURL: function (url) {\n return url.replace(/\\?.*$/, \"\");\n },\n\n imageExtensionPattern: new RegExp(\"(\" + [\n\"bmp\", \"png\", \"jpeg\", \"jpg\", \"gif\", \"tiff\",\n\"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\", \"ppm\", \"svg\"\n ].join(\"|\") + \")$\", \"i\")\n};\n\nif (typeof exports !== \"undefined\")\n exports.Converter = Converter;\n",
"/**\n * Copyright (C) 2018 The Software Heritage developers\n * See the AUTHORS file at the top-level directory of this distribution\n * License: GNU Affero General Public License version 3, or any later version\n * See top-level LICENSE file for more information\n */\n\n// org-js chunk that will be lazily loaded\n\nimport './org.css';\nexport * from 'org';\n",
"function PrototypeNode(type, children) {\n this.type = type;\n this.children = [];\n\n if (children) {\n for (var i = 0, len = children.length; i < len; ++i) {\n this.appendChild(children[i]);\n }\n }\n}\nPrototypeNode.prototype = {\n previousSibling: null,\n parent: null,\n get firstChild() {\n return this.children.length < 1 ?\n null : this.children[0];\n },\n get lastChild() {\n return this.children.length < 1 ?\n null : this.children[this.children.length - 1];\n },\n appendChild: function (newChild) {\n var previousSibling = this.children.length < 1 ?\n null : this.lastChild;\n this.children.push(newChild);\n newChild.previousSibling = previousSibling;\n newChild.parent = this;\n },\n toString: function () {\n var string = \"<\" + this.type + \">\";\n\n if (typeof this.value !== \"undefined\") {\n string += \"\" + this.value;\n } else if (this.children) {\n string += \"\\n\" + this.children.map(function (child, idx) {\n return \"#\" + idx + \"\" + child.toString();\n }).join(\"\\n\").split(\"\\n\").map(function (line) {\n return \"\" + line;\n }).join(\"\\n\");\n }\n\n return string;\n }\n};\n\nvar Node = {\n types: {},\n\n define: function (name, postProcess) {\n this.types[name] = name;\n\n var methodName = \"create\" + name.substring(0, 1).toUpperCase() + name.substring(1);\n var postProcessGiven = typeof postProcess === \"function\";\n\n this[methodName] = function (children, options) {\n var node = new PrototypeNode(name, children);\n\n if (postProcessGiven)\n postProcess(node, options || {});\n\n return node;\n };\n }\n};\n\nNode.define(\"text\", function (node, options) {\n node.value = options.value;\n});\nNode.define(\"header\", function (node, options) {\n node.level = options.level;\n});\nNode.define(\"orderedList\");\nNode.define(\"unorderedList\");\nNode.define(\"definitionList\");\nNode.define(\"listElement\");\nNode.define(\"paragraph\");\nNode.define(\"preformatted\");\nNode.define(\"table\");\nNode.define(\"tableRow\");\nNode.define(\"tableCell\");\nNode.define(\"horizontalRule\");\nNode.define(\"directive\");\n\n// Inline\nNode.define(\"inlineContainer\");\n\nNode.define(\"bold\");\nNode.define(\"italic\");\nNode.define(\"underline\");\nNode.define(\"code\");\nNode.define(\"verbatim\");\nNode.define(\"dashed\");\nNode.define(\"link\", function (node, options) {\n node.src = options.src;\n});\n\nif (typeof exports !== \"undefined\")\n exports.Node = Node;\n",
"if (typeof exports !== \"undefined\") {\n function exportModule(module) {\n for (var exportedName in module) {\n if (module.hasOwnProperty(exportedName)) {\n exports[exportedName] = module[exportedName];\n }\n }\n }\n\n exportModule(require(\"./org/parser.js\"));\n exportModule(require(\"./org/lexer.js\"));\n exportModule(require(\"./org/node.js\"));\n exportModule(require(\"./org/parser.js\"));\n exportModule(require(\"./org/stream.js\"));\n exportModule(require(\"./org/converter/html.js\"));\n}\n",
"var Stream = require(\"./stream.js\").Stream;\nvar Lexer = require(\"./lexer.js\").Lexer;\nvar Node = require(\"./node.js\").Node;\n\nfunction Parser() {\n this.inlineParser = new InlineParser();\n}\n\nParser.parseStream = function (stream, options) {\n var parser = new Parser();\n parser.initStatus(stream, options);\n parser.parseNodes();\n return parser.nodes;\n};\n\nParser.prototype = {\n initStatus: function (stream, options) {\n if (typeof stream === \"string\")\n stream = new Stream(stream);\n this.lexer = new Lexer(stream);\n this.nodes = [];\n this.options = {\n toc: true,\n num: true,\n\"^\": \"{}\",\n multilineCell: false\n };\n // Override option values\n if (options && typeof options === \"object\") {\n for (var key in options) {\n this.options[key] = options[key];\n }\n }\n this.document = {\n options: this.options,\n directiveValues: {},\n convert: function (ConverterClass, exportOptions) {\n var converter = new ConverterClass(this, exportOptions);\n return converter.result;\n }\n };\n },\n\n parse: function (stream, options) {\n this.initStatus(stream, options);\n this.parseDocument();\n this.document.nodes = this.nodes;\n return this.document;\n },\n\n createErrorReport: function (message) {\n return new Error(message + \" at line \" + this.lexer.getLineNumber());\n },\n\n skipBlank: function () {\n var blankToken = null;\n while (this.lexer.peekNextToken().type === Lexer.tokens.blank)\n blankToken = this.lexer.getNextToken();\n return blankToken;\n },\n\n setNodeOriginFromToken: function (node, token) {\n node.fromLineNumber = token.fromLineNumber;\n return node;\n },\n\n appendNode: function (newNode) {\n var previousSibling = this.nodes.length > 0 ? this.nodes[this.nodes.length - 1] : null;\n this.nodes.push(newNode);\n newNode.previousSibling = previousSibling;\n },\n\n // ------------------------------------------------------------\n // <Document> ::= <Element>*\n // ------------------------------------------------------------\n\n parseDocument: function () {\n this.parseTitle();\n this.parseNodes();\n },\n\n parseNodes: function () {\n while (this.lexer.hasNext()) {\n var element = this.parseElement();\n if (element) this.appendNode(element);\n }\n },\n\n parseTitle: function () {\n this.skipBlank();\n\n if (this.lexer.hasNext() &&\n this.lexer.peekNextToken().type === Lexer.tokens.line)\n this.document.title = this.createTextNode(this.lexer.getNextToken().content);\n else\n this.document.title = null;\n\n this.lexer.pushDummyTokenByType(Lexer.tokens.blank);\n },\n\n // ------------------------------------------------------------\n // <Element> ::= (<Header> | <List>\n // | <Preformatted> | <Paragraph>\n // | <Table>)*\n // ------------------------------------------------------------\n\n parseElement: function () {\n var element = null;\n\n switch (this.lexer.peekNextToken().type) {\n case Lexer.tokens.header:\n element = this.parseHeader();\n break;\n case Lexer.tokens.preformatted:\n element = this.parsePreformatted();\n break;\n case Lexer.tokens.orderedListElement:\n case Lexer.tokens.unorderedListElement:\n element = this.parseList();\n break;\n case Lexer.tokens.line:\n element = this.parseText();\n break;\n case Lexer.tokens.tableRow:\n case Lexer.tokens.tableSeparator:\n element = this.parseTable();\n break;\n case Lexer.tokens.blank:\n this.skipBlank();\n if (this.lexer.hasNext()) {\n if (this.lexer.peekNextToken().type === Lexer.tokens.line)\n element = this.parseParagraph();\n else\n element = this.parseElement();\n }\n break;\n case Lexer.tokens.horizontalRule:\n this.lexer.getNextToken();\n element = Node.createHorizontalRule();\n break;\n case Lexer.tokens.directive:\n element = this.parseDirective();\n break;\n case Lexer.tokens.comment:\n // Skip\n this.lexer.getNextToken();\n break;\n default:\n throw this.createErrorReport(\"Unhandled token: \" + this.lexer.peekNextToken().type);\n }\n\n return element;\n },\n\n parseElementBesidesDirectiveEnd: function () {\n try {\n // Temporary, override the definition of `parseElement`\n this.parseElement = this.parseElementBesidesDirectiveEndBody;\n return this.parseElement();\n } finally {\n this.parseElement = this.originalParseElement;\n }\n },\n\n parseElementBesidesDirectiveEndBody: function () {\n if (this.lexer.peekNextToken().type === Lexer.tokens.directive &&\n this.lexer.peekNextToken().endDirective) {\n return null;\n }\n\n return this.originalParseElement();\n },\n\n // ------------------------------------------------------------\n // <Header>\n //\n // : preformatted\n // : block\n // ------------------------------------------------------------\n\n parseHeader: function () {\n var headerToken = this.lexer.getNextToken();\n var header = Node.createHeader([\n this.createTextNode(headerToken.content) // TODO: Parse inline markups\n ], { level: headerToken.level });\n this.setNodeOriginFromToken(header, headerToken);\n\n return header;\n },\n\n // ------------------------------------------------------------\n // <Preformatted>\n //\n // : preformatted\n // : block\n // ------------------------------------------------------------\n\n parsePreformatted: function () {\n var preformattedFirstToken = this.lexer.peekNextToken();\n var preformatted = Node.createPreformatted([]);\n this.setNodeOriginFromToken(preformatted, preformattedFirstToken);\n\n var textContents = [];\n\n while (this.lexer.hasNext()) {\n var token = this.lexer.peekNextToken();\n if (token.type !== Lexer.tokens.preformatted ||\n token.indentation < preformattedFirstToken.indentation)\n break;\n this.lexer.getNextToken();\n textContents.push(token.content);\n }\n\n preformatted.appendChild(this.createTextNode(textContents.join(\"\\n\"), true /* no emphasis */));\n\n return preformatted;\n },\n\n // ------------------------------------------------------------\n // <List>\n //\n // - foo\n // 1. bar\n // 2. baz\n // ------------------------------------------------------------\n\n // XXX: not consider codes (e.g., =Foo::Bar=)\n definitionPattern: /^(.*?) :: *(.*)$/,\n\n parseList: function () {\n var rootToken = this.lexer.peekNextToken();\n var list;\n var isDefinitionList = false;\n\n if (this.definitionPattern.test(rootToken.content)) {\n list = Node.createDefinitionList([]);\n isDefinitionList = true;\n } else {\n list = rootToken.type === Lexer.tokens.unorderedListElement ?\n Node.createUnorderedList([]) : Node.createOrderedList([]);\n }\n this.setNodeOriginFromToken(list, rootToken);\n\n while (this.lexer.hasNext()) {\n var nextToken = this.lexer.peekNextToken();\n if (!nextToken.isListElement() || nextToken.indentation !== rootToken.indentation)\n break;\n list.appendChild(this.parseListElement(rootToken.indentation, isDefinitionList));\n }\n\n return list;\n },\n\n unknownDefinitionTerm: \"???\",\n\n parseListElement: function (rootIndentation, isDefinitionList) {\n var listElementToken = this.lexer.getNextToken();\n var listElement = Node.createListElement([]);\n this.setNodeOriginFromToken(listElement, listElementToken);\n\n listElement.isDefinitionList = isDefinitionList;\n\n if (isDefinitionList) {\n var match = this.definitionPattern.exec(listElementToken.content);\n listElement.term = [\n this.createTextNode(match && match[1] ? match[1] : this.unknownDefinitionTerm)\n ];\n listElement.appendChild(this.createTextNode(match ? match[2] : listElementToken.content));\n } else {\n listElement.appendChild(this.createTextNode(listElementToken.content));\n }\n\n while (this.lexer.hasNext()) {\n var blankToken = this.skipBlank();\n if (!this.lexer.hasNext())\n break;\n\n var notBlankNextToken = this.lexer.peekNextToken();\n if (blankToken && !notBlankNextToken.isListElement())\n this.lexer.pushToken(blankToken); // Recover blank token only when next line is not listElement.\n if (notBlankNextToken.indentation <= rootIndentation)\n break; // end of the list\n\n var element = this.parseElement(); // recursive\n if (element)\n listElement.appendChild(element);\n }\n\n return listElement;\n },\n\n // ------------------------------------------------------------\n // <Table> ::= <TableRow>+\n // ------------------------------------------------------------\n\n parseTable: function () {\n var nextToken = this.lexer.peekNextToken();\n var table = Node.createTable([]);\n this.setNodeOriginFromToken(table, nextToken);\n var sawSeparator = false;\n\n var allowMultilineCell = nextToken.type === Lexer.tokens.tableSeparator && this.options.multilineCell;\n\n while (this.lexer.hasNext() &&\n (nextToken = this.lexer.peekNextToken()).isTableElement()) {\n if (nextToken.type === Lexer.tokens.tableRow) {\n var tableRow = this.parseTableRow(allowMultilineCell);\n table.appendChild(tableRow);\n } else {\n // Lexer.tokens.tableSeparator\n sawSeparator = true;\n this.lexer.getNextToken();\n }\n }\n\n if (sawSeparator && table.children.length) {\n table.children[0].children.forEach(function (cell) {\n cell.isHeader = true;\n });\n }\n\n return table;\n },\n\n // ------------------------------------------------------------\n // <TableRow> ::= <TableCell>+\n // ------------------------------------------------------------\n\n parseTableRow: function (allowMultilineCell) {\n var tableRowTokens = [];\n\n while (this.lexer.peekNextToken().type === Lexer.tokens.tableRow) {\n tableRowTokens.push(this.lexer.getNextToken());\n if (!allowMultilineCell) {\n break;\n }\n }\n\n if (!tableRowTokens.length) {\n throw this.createErrorReport(\"Expected table row\");\n }\n\n var firstTableRowToken = tableRowTokens.shift();\n var tableCellTexts = firstTableRowToken.content.split(\"|\");\n\n tableRowTokens.forEach(function (rowToken) {\n rowToken.content.split(\"|\").forEach(function (cellText, cellIdx) {\n tableCellTexts[cellIdx] = (tableCellTexts[cellIdx] || \"\") + \"\\n\" + cellText;\n });\n });\n\n // TODO: Prepare two pathes: (1)\n var tableCells = tableCellTexts.map(\n // TODO: consider '|' escape?\n function (text) {\n return Node.createTableCell(Parser.parseStream(text));\n }, this);\n\n return this.setNodeOriginFromToken(Node.createTableRow(tableCells), firstTableRowToken);\n },\n\n // ------------------------------------------------------------\n // <Directive> ::= \"#+.*\"\n // ------------------------------------------------------------\n\n parseDirective: function () {\n var directiveToken = this.lexer.getNextToken();\n var directiveNode = this.createDirectiveNodeFromToken(directiveToken);\n\n if (directiveToken.endDirective)\n throw this.createErrorReport(\"Unmatched 'end' directive for \" + directiveNode.directiveName);\n\n if (directiveToken.oneshotDirective) {\n this.interpretDirective(directiveNode);\n return directiveNode;\n }\n\n if (!directiveToken.beginDirective)\n throw this.createErrorReport(\"Invalid directive \" + directiveNode.directiveName);\n\n // Parse begin ~ end\n directiveNode.children = [];\n if (this.isVerbatimDirective(directiveNode))\n return this.parseDirectiveBlockVerbatim(directiveNode);\n else\n return this.parseDirectiveBlock(directiveNode);\n },\n\n createDirectiveNodeFromToken: function (directiveToken) {\n var matched = /^[ ]*([^ ]*)[ ]*(.*)[ ]*$/.exec(directiveToken.content);\n\n var directiveNode = Node.createDirective(null);\n this.setNodeOriginFromToken(directiveNode, directiveToken);\n directiveNode.directiveName = matched[1].toLowerCase();\n directiveNode.directiveArguments = this.parseDirectiveArguments(matched[2]);\n directiveNode.directiveOptions = this.parseDirectiveOptions(matched[2]);\n directiveNode.directiveRawValue = matched[2];\n\n return directiveNode;\n },\n\n isVerbatimDirective: function (directiveNode) {\n var directiveName = directiveNode.directiveName;\n return directiveName === \"src\" || directiveName === \"example\" || directiveName === \"html\";\n },\n\n parseDirectiveBlock: function (directiveNode, verbatim) {\n this.lexer.pushDummyTokenByType(Lexer.tokens.blank);\n\n while (this.lexer.hasNext()) {\n var nextToken = this.lexer.peekNextToken();\n if (nextToken.type === Lexer.tokens.directive &&\n nextToken.endDirective &&\n this.createDirectiveNodeFromToken(nextToken).directiveName === directiveNode.directiveName) {\n // Close directive\n this.lexer.getNextToken();\n return directiveNode;\n }\n var element = this.parseElementBesidesDirectiveEnd();\n if (element)\n directiveNode.appendChild(element);\n }\n\n throw this.createErrorReport(\"Unclosed directive \" + directiveNode.directiveName);\n },\n\n parseDirectiveBlockVerbatim: function (directiveNode) {\n var textContent = [];\n\n while (this.lexer.hasNext()) {\n var nextToken = this.lexer.peekNextToken();\n if (nextToken.type === Lexer.tokens.directive &&\n nextToken.endDirective &&\n this.createDirectiveNodeFromToken(nextToken).directiveName === directiveNode.directiveName) {\n this.lexer.getNextToken();\n directiveNode.appendChild(this.createTextNode(textContent.join(\"\\n\"), true));\n return directiveNode;\n }\n textContent.push(this.lexer.stream.getNextLine());\n }\n\n throw this.createErrorReport(\"Unclosed directive \" + directiveNode.directiveName);\n },\n\n parseDirectiveArguments: function (parameters) {\n return parameters.split(/[ ]+/).filter(function (param) {\n return param.length && param[0] !== \"-\";\n });\n },\n\n parseDirectiveOptions: function (parameters) {\n return parameters.split(/[ ]+/).filter(function (param) {\n return param.length && param[0] === \"-\";\n });\n },\n\n interpretDirective: function (directiveNode) {\n // http://orgmode.org/manual/Export-options.html\n switch (directiveNode.directiveName) {\n case \"options:\":\n this.interpretOptionDirective(directiveNode);\n break;\n case \"title:\":\n this.document.title = directiveNode.directiveRawValue;\n break;\n case \"author:\":\n this.document.author = directiveNode.directiveRawValue;\n break;\n case \"email:\":\n this.document.email = directiveNode.directiveRawValue;\n break;\n default:\n this.document.directiveValues[directiveNode.directiveName] = directiveNode.directiveRawValue;\n break;\n }\n },\n\n interpretOptionDirective: function (optionDirectiveNode) {\n optionDirectiveNode.directiveArguments.forEach(function (pairString) {\n var pair = pairString.split(\":\");\n this.options[pair[0]] = this.convertLispyValue(pair[1]);\n }, this);\n },\n\n convertLispyValue: function (lispyValue) {\n switch (lispyValue) {\n case \"t\":\n return true;\n case \"nil\":\n return false;\n default:\n if (/^[0-9]+$/.test(lispyValue))\n return parseInt(lispyValue);\n return lispyValue;\n }\n },\n\n // ------------------------------------------------------------\n // <Paragraph> ::= <Blank> <Line>*\n // ------------------------------------------------------------\n\n parseParagraph: function () {\n var paragraphFisrtToken = this.lexer.peekNextToken();\n var paragraph = Node.createParagraph([]);\n this.setNodeOriginFromToken(paragraph, paragraphFisrtToken);\n\n var textContents = [];\n\n while (this.lexer.hasNext()) {\n var nextToken = this.lexer.peekNextToken();\n if (nextToken.type !== Lexer.tokens.line\n || nextToken.indentation < paragraphFisrtToken.indentation)\n break;\n this.lexer.getNextToken();\n textContents.push(nextToken.content);\n }\n\n paragraph.appendChild(this.createTextNode(textContents.join(\"\\n\")));\n\n return paragraph;\n },\n\n parseText: function (noEmphasis) {\n var lineToken = this.lexer.getNextToken();\n return this.createTextNode(lineToken.content, noEmphasis);\n },\n\n // ------------------------------------------------------------\n // <Text> (DOM Like)\n // ------------------------------------------------------------\n\n createTextNode: function (text, noEmphasis) {\n return noEmphasis ? Node.createText(null, { value: text })\n : this.inlineParser.parseEmphasis(text);\n }\n};\nParser.prototype.originalParseElement = Parser.prototype.parseElement;\n\n// ------------------------------------------------------------\n// Parser for Inline Elements\n//\n// @refs org-emphasis-regexp-components\n// ------------------------------------------------------------\n\nfunction InlineParser() {\n this.preEmphasis = \"\\t\\\\('\\\"\";\n this.postEmphasis = \"- \\t.,:!?;'\\\"\\\\)\";\n this.borderForbidden = \"\\t\\r\\n,\\\"'\";\n this.bodyRegexp = \"[\\\\s\\\\S]*?\";\n this.markers = \"*/_=~+\";\n\n this.emphasisPattern = this.buildEmphasisPattern();\n this.linkPattern = /\\[\\[([^\\]]*)\\](?:\\[([^\\]]*)\\])?\\]/g; // \\1 => link, \\2 => text\n}\n\nInlineParser.prototype = {\n parseEmphasis: function (text) {\n var emphasisPattern = this.emphasisPattern;\n emphasisPattern.lastIndex = 0;\n\n var result = [],\n match,\n previousLast = 0,\n savedLastIndex;\n\n while ((match = emphasisPattern.exec(text))) {\n var whole = match[0];\n var pre = match[1];\n var marker = match[2];\n var body = match[3];\n var post = match[4];\n\n {\n // parse links\n var matchBegin = emphasisPattern.lastIndex - whole.length;\n var beforeContent = text.substring(previousLast, matchBegin + pre.length);\n savedLastIndex = emphasisPattern.lastIndex;\n result.push(this.parseLink(beforeContent));\n emphasisPattern.lastIndex = savedLastIndex;\n }\n\n var bodyNode = [Node.createText(null, { value: body })];\n var bodyContainer = this.emphasizeElementByMarker(bodyNode, marker);\n result.push(bodyContainer);\n\n previousLast = emphasisPattern.lastIndex - post.length;\n }\n\n if (emphasisPattern.lastIndex === 0 ||\n emphasisPattern.lastIndex !== text.length - 1)\n result.push(this.parseLink(text.substring(previousLast)));\n\n if (result.length === 1) {\n // Avoid duplicated inline container wrapping\n return result[0];\n } else {\n return Node.createInlineContainer(result);\n }\n },\n\n depth: 0,\n parseLink: function (text) {\n var linkPattern = this.linkPattern;\n linkPattern.lastIndex = 0;\n\n var match,\n result = [],\n previousLast = 0,\n savedLastIndex;\n\n while ((match = linkPattern.exec(text))) {\n var whole = match[0];\n var src = match[1];\n var title = match[2];\n\n // parse before content\n var matchBegin = linkPattern.lastIndex - whole.length;\n var beforeContent = text.substring(previousLast, matchBegin);\n result.push(Node.createText(null, { value: beforeContent }));\n\n // parse link\n var link = Node.createLink([]);\n link.src = src;\n if (title) {\n savedLastIndex = linkPattern.lastIndex;\n link.appendChild(this.parseEmphasis(title));\n linkPattern.lastIndex = savedLastIndex;\n } else {\n link.appendChild(Node.createText(null, { value: src }));\n }\n result.push(link);\n\n previousLast = linkPattern.lastIndex;\n }\n\n if (linkPattern.lastIndex === 0 ||\n linkPattern.lastIndex !== text.length - 1)\n result.push(Node.createText(null, { value: text.substring(previousLast) }));\n\n return Node.createInlineContainer(result);\n },\n\n emphasizeElementByMarker: function (element, marker) {\n switch (marker) {\n case \"*\":\n return Node.createBold(element);\n case \"/\":\n return Node.createItalic(element);\n case \"_\":\n return Node.createUnderline(element);\n case \"=\":\n case \"~\":\n return Node.createCode(element);\n case \"+\":\n return Node.createDashed(element);\n }\n },\n\n buildEmphasisPattern: function () {\n return new RegExp(\n\"([\" + this.preEmphasis + \"]|^|\\r?\\n)\" + // \\1 => pre\n\"([\" + this.markers + \"])\" + // \\2 => marker\n\"([^\" + this.borderForbidden + \"]|\" + // \\3 => body\n\"[^\" + this.borderForbidden + \"]\" +\n this.bodyRegexp +\n\"[^\" + this.borderForbidden + \"])\" +\n\"\\\\2\" +\n\"([\" + this.postEmphasis +\"]|$|\\r?\\n)\", // \\4 => post\n // flags\n\"g\"\n );\n }\n};\n\nif (typeof exports !== \"undefined\") {\n exports.Parser = Parser;\n exports.InlineParser = InlineParser;\n}\n",