File: asciidoc.C

package info (click to toggle)
witty 3.3.3%2Bdfsg-4.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,228 kB
  • ctags: 26,694
  • sloc: cpp: 147,809; ansic: 77,999; xml: 16,331; sh: 1,303; makefile: 198; java: 86; sql: 14
file content (94 lines) | stat: -rw-r--r-- 2,070 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "asciidoc.h"

#include <fstream>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <boost/scoped_array.hpp>

#include "Wt/WString"

using namespace Wt;

namespace {

std::string tempFileName() 
{
#ifndef WT_WIN32
  char spool[20];
  strcpy(spool, "/tmp/wtXXXXXX");

  int i = mkstemp(spool);
  close(i);
#else
  char spool[2 * L_tmpnam];
  tmpnam(spool);
#endif
  return std::string(spool);
}

std::string readFileToString(const std::string& fileName) 
{
  std::fstream file (fileName.c_str(), std::ios::in | std::ios::binary);
  file.seekg(0, std::ios::end);
  int length = file.tellg();
  file.seekg(0, std::ios::beg);

  boost::scoped_array<char> buf(new char[length]);
  file.read(buf.get(), length);
  file.close();

  return std::string(buf.get(), length);
}

}

WString asciidoc(const Wt::WString& src)
{
  std::string srcFileName = tempFileName();
  std::string htmlFileName = tempFileName();

  {
    std::ofstream srcFile(srcFileName.c_str(), std::ios::out);
    std::string ssrc = src.toUTF8();
    srcFile.write(ssrc.c_str(), (std::streamsize)ssrc.length());
    srcFile.close();
  }

#if defined(ASCIIDOC_EXECUTABLE)
#define xstr(s) str(s)
#define str(s) #s
  std::string cmd = xstr(ASCIIDOC_EXECUTABLE);
#else
  std::string cmd = "asciidoc";
#endif
  std::string command = cmd + " -o " + htmlFileName + " -s " + srcFileName;

#ifndef WT_WIN32
  /*
   * So, asciidoc apparently sends a SIGINT which is caught by its parent
   * process.. So we have to temporarily ignore it.
   */
  struct sigaction newAction, oldAction;
  newAction.sa_handler = SIG_IGN;
  newAction.sa_flags = 0;
  sigemptyset(&newAction.sa_mask);
  sigaction(SIGINT, &newAction, &oldAction);
#endif
  bool ok = system(command.c_str()) == 0;
#ifndef WT_WIN32
  sigaction(SIGINT, &oldAction, 0);
#endif

  WString result;

  if (ok) {
    result = WString::fromUTF8(readFileToString(htmlFileName));
  } else
    result = WString::fromUTF8("<i>Could not execute asciidoc</i>");

  unlink(srcFileName.c_str());
  unlink(htmlFileName.c_str());

  return result;
}