aboutsummaryrefslogtreecommitdiff
path: root/js/lib/xml.js
blob: e867fdcccd8a113be2629276f69ba9350f59b65d (plain)
  1. "use strict";
  2. var escapeXml = require('./common').escapeXml;
  3. // Helper function to produce an XML tag.
  4. var tag = function(name, attrs, selfclosing) {
  5. var result = '<' + name;
  6. if (attrs && attrs.length > 0) {
  7. var i = 0;
  8. var attrib;
  9. while ((attrib = attrs[i]) !== undefined) {
  10. result += ' ' + attrib[0] + '="' + attrib[1] + '"';
  11. i++;
  12. }
  13. }
  14. if (selfclosing) {
  15. result += ' /';
  16. }
  17. result += '>';
  18. return result;
  19. };
  20. var reXMLTag = /\<[^>]*\>/;
  21. var toTagName = function(s) {
  22. return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
  23. };
  24. var renderNodes = function(block) {
  25. var attrs;
  26. var tagname;
  27. var walker = block.walker();
  28. var event, node, entering;
  29. var buffer = "";
  30. var lastOut = "\n";
  31. var disableTags = 0;
  32. var indentLevel = 0;
  33. var indent = ' ';
  34. var unescapedContents;
  35. var container;
  36. var selfClosing;
  37. var nodetype;
  38. var out = function(s) {
  39. if (disableTags > 0) {
  40. buffer += s.replace(reXMLTag, '');
  41. } else {
  42. buffer += s;
  43. }
  44. lastOut = s;
  45. };
  46. var esc = this.escape;
  47. var cr = function() {
  48. if (lastOut !== '\n') {
  49. buffer += '\n';
  50. lastOut = '\n';
  51. for (var i = indentLevel; i--;) {
  52. buffer += indent;
  53. }
  54. }
  55. };
  56. var options = this.options;
  57. if (options.time) { console.time("rendering"); }
  58. buffer += '<?xml version="1.0" encoding="UTF-8"?>\n';
  59. buffer += '<!DOCTYPE CommonMark SYSTEM "CommonMark.dtd">\n';
  60. while ((event = walker.next())) {
  61. entering = event.entering;
  62. node = event.node;
  63. nodetype = node.t;
  64. if (nodetype === 'ReferenceDef') {
  65. continue;
  66. }
  67. container = node.isContainer();
  68. selfClosing = nodetype === 'HorizontalRule' || nodetype === 'Hardbreak' ||
  69. nodetype === 'Softbreak' || nodetype === 'Image';
  70. unescapedContents = nodetype === 'Html' || nodetype === 'HtmlInline';
  71. tagname = toTagName(nodetype);
  72. if (entering) {
  73. attrs = [];
  74. switch (nodetype) {
  75. case 'List':
  76. var data = node.list_data;
  77. if (data.type !== undefined) {
  78. attrs.push(['type', data.type.toLowerCase()]);
  79. }
  80. if (data.start !== undefined) {
  81. attrs.push(['start', String(data.start)]);
  82. }
  83. if (data.tight !== undefined) {
  84. attrs.push(['tight', (data.tight ? 'true' : 'false')]);
  85. }
  86. if (data.delimiter !== undefined) {
  87. var delimword = '';
  88. if (data.delimiter === '.') {
  89. delimword = 'period';
  90. } else {
  91. delimword = 'paren';
  92. }
  93. attrs.push(['delimiter', delimword]);
  94. }
  95. break;
  96. case 'CodeBlock':
  97. if (node.info) {
  98. attrs.push(['info', node.info]);
  99. }
  100. break;
  101. case 'Header':
  102. attrs.push(['level', String(node.level)]);
  103. break;
  104. case 'Link':
  105. case 'Image':
  106. attrs.push(['destination', node.destination]);
  107. attrs.push(['title', node.title]);
  108. break;
  109. default:
  110. break;
  111. }
  112. if (options.sourcepos) {
  113. var pos = node.sourcepos;
  114. if (pos !== undefined) {
  115. attrs.push(['data-sourcepos', String(pos[0][0]) + ':' +
  116. String(pos[0][1]) + '-' + String(pos[1][0]) + ':' +
  117. String(pos[1][1])]);
  118. }
  119. }
  120. cr();
  121. out(tag(tagname, attrs, selfClosing));
  122. if (container) {
  123. indentLevel += 1;
  124. } else if (!container && !selfClosing) {
  125. if (node.literal) {
  126. out(unescapedContents ? node.literal : esc(node.literal));
  127. }
  128. out(tag('/' + tagname));
  129. }
  130. } else {
  131. indentLevel -= 1;
  132. cr();
  133. out(tag('/' + tagname));
  134. }
  135. }
  136. if (options.time) { console.timeEnd("rendering"); }
  137. buffer += '\n';
  138. return buffer;
  139. };
  140. // The XmlRenderer object.
  141. function XmlRenderer(options){
  142. return {
  143. // default options:
  144. softbreak: '\n', // by default, soft breaks are rendered as newlines in HTML
  145. // set to "<br />" to make them hard breaks
  146. // set to " " if you want to ignore line wrapping in source
  147. escape: escapeXml,
  148. options: options || {},
  149. render: renderNodes
  150. };
  151. }
  152. module.exports = XmlRenderer;