options-reader.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. exports.readOptions = readOptions;
  2. var _ = require("underscore");
  3. var defaultStyleMap = exports._defaultStyleMap = [
  4. "p.Heading1 => h1:fresh",
  5. "p.Heading2 => h2:fresh",
  6. "p.Heading3 => h3:fresh",
  7. "p.Heading4 => h4:fresh",
  8. "p.Heading5 => h5:fresh",
  9. "p.Heading6 => h6:fresh",
  10. "p[style-name='Heading 1'] => h1:fresh",
  11. "p[style-name='Heading 2'] => h2:fresh",
  12. "p[style-name='Heading 3'] => h3:fresh",
  13. "p[style-name='Heading 4'] => h4:fresh",
  14. "p[style-name='Heading 5'] => h5:fresh",
  15. "p[style-name='Heading 6'] => h6:fresh",
  16. "p[style-name='heading 1'] => h1:fresh",
  17. "p[style-name='heading 2'] => h2:fresh",
  18. "p[style-name='heading 3'] => h3:fresh",
  19. "p[style-name='heading 4'] => h4:fresh",
  20. "p[style-name='heading 5'] => h5:fresh",
  21. "p[style-name='heading 6'] => h6:fresh",
  22. "r[style-name='Strong'] => strong",
  23. "p[style-name='footnote text'] => p:fresh",
  24. "r[style-name='footnote reference'] =>",
  25. "p[style-name='endnote text'] => p:fresh",
  26. "r[style-name='endnote reference'] =>",
  27. "p[style-name='annotation text'] => p:fresh",
  28. "r[style-name='annotation reference'] =>",
  29. // LibreOffice
  30. "p[style-name='Footnote'] => p:fresh",
  31. "r[style-name='Footnote anchor'] =>",
  32. "p[style-name='Endnote'] => p:fresh",
  33. "r[style-name='Endnote anchor'] =>",
  34. "p:unordered-list(1) => ul > li:fresh",
  35. "p:unordered-list(2) => ul|ol > li > ul > li:fresh",
  36. "p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh",
  37. "p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
  38. "p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh",
  39. "p:ordered-list(1) => ol > li:fresh",
  40. "p:ordered-list(2) => ul|ol > li > ol > li:fresh",
  41. "p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh",
  42. "p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
  43. "p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh",
  44. "r[style-name='Hyperlink'] =>",
  45. "p[style-name='Normal'] => p:fresh"
  46. ];
  47. var standardOptions = exports._standardOptions = {
  48. transformDocument: identity,
  49. includeDefaultStyleMap: true,
  50. includeEmbeddedStyleMap: true
  51. };
  52. function readOptions(options) {
  53. options = options || {};
  54. return _.extend({}, standardOptions, options, {
  55. customStyleMap: readStyleMap(options.styleMap),
  56. readStyleMap: function() {
  57. var styleMap = this.customStyleMap;
  58. if (this.includeEmbeddedStyleMap) {
  59. styleMap = styleMap.concat(readStyleMap(this.embeddedStyleMap));
  60. }
  61. if (this.includeDefaultStyleMap) {
  62. styleMap = styleMap.concat(defaultStyleMap);
  63. }
  64. return styleMap;
  65. }
  66. });
  67. }
  68. function readStyleMap(styleMap) {
  69. if (!styleMap) {
  70. return [];
  71. } else if (_.isString(styleMap)) {
  72. return styleMap.split("\n")
  73. .map(function(line) {
  74. return line.trim();
  75. })
  76. .filter(function(line) {
  77. return line !== "" && line.charAt(0) !== "#";
  78. });
  79. } else {
  80. return styleMap;
  81. }
  82. }
  83. function identity(value) {
  84. return value;
  85. }