SynctexOutputParser.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const Path = require('path')
  2. /**
  3. * Parse output from the `synctex view` command
  4. */
  5. function parseViewOutput(output) {
  6. return _parseOutput(output, (record, label, value) => {
  7. switch (label) {
  8. case 'Page':
  9. _setIntProp(record, 'page', value)
  10. break
  11. case 'h':
  12. _setFloatProp(record, 'h', value)
  13. break
  14. case 'v':
  15. _setFloatProp(record, 'v', value)
  16. break
  17. case 'W':
  18. _setFloatProp(record, 'width', value)
  19. break
  20. case 'H':
  21. _setFloatProp(record, 'height', value)
  22. break
  23. }
  24. })
  25. }
  26. /**
  27. * Parse output from the `synctex edit` command
  28. */
  29. function parseEditOutput(output, baseDir) {
  30. return _parseOutput(output, (record, label, value) => {
  31. switch (label) {
  32. case 'Input':
  33. if (Path.isAbsolute(value)) {
  34. record.file = Path.relative(baseDir, value)
  35. } else {
  36. record.file = value
  37. }
  38. break
  39. case 'Line':
  40. _setIntProp(record, 'line', value)
  41. break
  42. case 'Column':
  43. _setIntProp(record, 'column', value)
  44. break
  45. }
  46. })
  47. }
  48. /**
  49. * Generic parser for synctex output
  50. *
  51. * Parses the output into records. Each line is split into a label and a value,
  52. * which are then sent to `processLine` for further processing.
  53. */
  54. function _parseOutput(output, processLine) {
  55. const lines = output.split('\n')
  56. let currentRecord = null
  57. const records = []
  58. for (const line of lines) {
  59. const [label, value] = _splitLine(line)
  60. // A line that starts with 'Output:' indicates a new record
  61. if (label === 'Output') {
  62. // Start new record
  63. currentRecord = {}
  64. records.push(currentRecord)
  65. continue
  66. }
  67. // Ignore the line if we're not in a record yet
  68. if (currentRecord == null) {
  69. continue
  70. }
  71. // Process the line
  72. processLine(currentRecord, label, value)
  73. }
  74. return records
  75. }
  76. /**
  77. * Split a line in label and value components.
  78. *
  79. * The components are separated by a colon. Note that this is slightly
  80. * different from `line.split(':', 2)`. This version puts the entirety of the
  81. * line after the colon in the value component, even if there are more colons
  82. * on the line.
  83. */
  84. function _splitLine(line) {
  85. const splitIndex = line.indexOf(':')
  86. if (splitIndex === -1) {
  87. return ['', line]
  88. }
  89. return [line.slice(0, splitIndex).trim(), line.slice(splitIndex + 1).trim()]
  90. }
  91. function _setIntProp(record, prop, value) {
  92. const intValue = parseInt(value, 10)
  93. if (!isNaN(intValue)) {
  94. record[prop] = intValue
  95. }
  96. }
  97. function _setFloatProp(record, prop, value) {
  98. const floatValue = parseFloat(value)
  99. if (!isNaN(floatValue)) {
  100. record[prop] = floatValue
  101. }
  102. }
  103. module.exports = { parseViewOutput, parseEditOutput }