Convert LaTeX to plaintext using a long-running Pandoc process.

main.cpp 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <iostream>
  2. #include <string>
  3. #include <pandoc.h>
  4. const int BUFFER_SIZE = 1024;
  5. struct data
  6. {
  7. std::string src; // String to convert
  8. size_t src_cur; // Reader position in the string
  9. std::string dst; // Converted string
  10. };
  11. int reader(char *buf, void *user_data)
  12. {
  13. struct data *d = (struct data *) user_data;
  14. // See how much is left to read
  15. size_t to_write = d->src.length() - d->src_cur;
  16. // If finished, tell that to pandoc
  17. if (to_write == 0) {
  18. return 0;
  19. }
  20. // Make sure not to overflow the buffer
  21. if (to_write > BUFFER_SIZE) {
  22. to_write = BUFFER_SIZE;
  23. }
  24. // Copy this chunk of data to the buffer
  25. std::strcpy(buf, (d->src.substr(d->src_cur, to_write)).c_str());
  26. d->src_cur += to_write;
  27. return to_write;
  28. }
  29. void writer(const char *buf, int len, void *user_data)
  30. {
  31. struct data *d = (struct data *) user_data;
  32. // Append what pandoc has produced
  33. std::string buf_str = std::string(buf).substr(0, len);
  34. d->dst.append(buf_str);
  35. }
  36. int main() {
  37. pandoc_init();
  38. // Read from stdin
  39. for (std::string line; std::getline(std::cin, line);) {
  40. struct data d = {
  41. .src = line
  42. // Zero-initialize everything else
  43. };
  44. // Do the conversion
  45. char *error = pandoc(BUFFER_SIZE,
  46. "markdown",
  47. "plain",
  48. NULL,
  49. reader,
  50. writer,
  51. &d);
  52. if (error) {
  53. std::cerr << "ERROR: " << error << std::endl;
  54. } else {
  55. std::cout << d.dst << std::endl;
  56. }
  57. free(error);
  58. }
  59. pandoc_exit();
  60. }