manoutputter.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  2. // vi:set ts=4 sts=4 sw=4 noet :
  3. //
  4. // Copyright 2010, 2011 wkhtmltopdf authors
  5. //
  6. // This file is part of wkhtmltopdf.
  7. //
  8. // wkhtmltopdf is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Lesser General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // wkhtmltopdf is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Lesser General Public License
  19. // along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.
  20. #include "outputter.h"
  21. #include <QStringList>
  22. #define S(x) ((x).toUtf8().constData())
  23. class ManOutputter: public Outputter {
  24. private:
  25. FILE * fd;
  26. int order;
  27. public:
  28. ManOutputter(FILE * _): fd(_) {
  29. fprintf(fd,".TH WKHTMLTOPDF 1 \"2009 February 23\"\n\n");
  30. }
  31. void beginSection(const QString & name) {
  32. fprintf(fd, ".SH %s\n", S(name));
  33. }
  34. void endSection() {
  35. fprintf(fd, "\n");
  36. }
  37. void beginParagraph() {
  38. }
  39. void endParagraph() {
  40. fprintf(fd, "\n\n");
  41. }
  42. void text(const QString & t) {
  43. QString str = QString(t).replace("-", "\\-");
  44. fprintf(fd, "%s", S(str));
  45. }
  46. void sectionLink(const QString & t) {
  47. text(t);
  48. }
  49. void bold(const QString & t) {
  50. fprintf(fd, "\\fB%s\\fP", S(t));
  51. }
  52. void italic(const QString & t) {
  53. fprintf(fd, "\\fB%s\\fP", S(t));
  54. }
  55. void link(const QString & t) {
  56. fprintf(fd, "<%s>", S(t));
  57. }
  58. void verbatim(const QString & t) {
  59. QString str = QString(t).replace("-", "\\-");
  60. QStringList l = str.split('\n');
  61. while ( l.back() == "") l.pop_back();
  62. foreach (const QString & line, l)
  63. fprintf(fd, " %s\n", S(line));
  64. fprintf(fd, "\n");
  65. }
  66. void beginSwitch() {
  67. fprintf(fd, ".PD 0\n");
  68. }
  69. void beginList(bool ordered) {
  70. order=(ordered?1:-1);
  71. }
  72. void endList() {
  73. fprintf(fd, "\n");
  74. }
  75. void listItem(const QString & s) {
  76. if (order < 0) fprintf(fd, " * ");
  77. else fprintf(fd, "%3d ", order++);
  78. fprintf(fd,"%s\n",S(s));
  79. }
  80. void cswitch(const ArgHandler * h) {
  81. fprintf(fd, ".TP\n");
  82. fprintf(fd, "\\fB");
  83. if (h->shortSwitch != 0)
  84. fprintf(fd, "\\-%c, ", h->shortSwitch);
  85. else
  86. fprintf(fd, " ");
  87. fprintf(fd,"\\-\\-%s\\fR", S(h->longName));
  88. for (QVector<QString>::const_iterator i = h->argn.constBegin(); i != h->argn.constEnd(); ++i)
  89. fprintf(fd," \\fI<%s>\\fR", S(*i));
  90. fprintf(fd, "\n%s\n", S(QString(h->desc).replace("-", "\\-")));
  91. }
  92. void endSwitch() {
  93. fprintf(fd, ".PD\n");
  94. fprintf(fd, "\n");
  95. }
  96. };
  97. /*!
  98. Create a man page outputter
  99. \param fd A file description to output to
  100. */
  101. Outputter * Outputter::man(FILE * fd) {
  102. return new ManOutputter(fd);
  103. }