Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (115 loc) · 2.98 KB
/
Copy pathmain.cpp
File metadata and controls
138 lines (115 loc) · 2.98 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright (c) 2009 by Chad Nelson
Released under the MIT License.
See the provided LICENSE.TXT file for details.
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <boost/optional.hpp>
//#include <boost/regex.hpp> // For the 'test' option
#include "markdown.h"
using std::cerr;
using std::cout;
using std::endl;
namespace {
class Options {
public:
Options(): mDebug(false), mTest(false) { }
bool readOptions(int argc, char *argv[]);
void showHelp();
bool debug() const { return mDebug; }
bool test() const { return mTest; }
std::string inputFile() const { return mInputFile; }
private:
std::string mInputFile;
bool mDebug, mTest;
};
bool Options::readOptions(int argc, char *argv[]) {
bool help=false, err=false;
for (int x=1; x<argc && !help && !err; ++x) {
if (argv[x][0]=='-' && argv[x][1]=='-' && argv[x][2]!=0) {
// It's a full-word option.
std::string opt(argv[x]+2);
if (opt=="debug") {
mDebug=true;
} else if (opt=="test") {
mTest=true;
} else if (opt=="help") {
help=true;
} else {
err=true;
cerr << "Unknown option " << argv[x] << ", use -? for help."
<< endl;
}
} else if (argv[x][0]=='-') {
// It's one or more option flags.
const char *i=argv[x]+1;
while (*i!=0 && !help && !err) {
switch (*i) {
case '?': help=true; break;
case 'd': mDebug=true; break;
default:
err=true;
cerr << "Uknown option flag '" << *i << "', use -? for "
"help." << endl;
}
++i;
}
} else {
// It's a parameter.
if (mInputFile.empty()) {
mInputFile=argv[x];
} else {
err=true;
cerr << "Too many parameters. Already had '" << mInputFile
<< "', found '" << argv[x] << "' too. Use -? for help."
<< endl;
}
}
}
if (help) {
showHelp();
return false;
} else if (err) {
return false;
} else return true;
}
void Options::showHelp() {
const char *cHelpScreen=
"This program converts input (from an input file or stdin) from Markdown syntax\n"
"to HTML using the cpp-markdown library.\n"
"\n"
"Usage:\n"
" cpp-markdown [<option>...] [input-file]\n"
"\n"
"Available options are:\n"
" -?, --help Show this screen.\n"
" -d, --debug Show tokens instead of HTML output.\n";
cerr << endl << cHelpScreen << endl;
}
} // namespace
int main(int argc, char *argv[]) {
Options cfg;
if (!cfg.readOptions(argc, argv)) return 1;
// if (cfg.test()) {
// return 1;
// }
std::ifstream ifile;
std::istream *in=&std::cin;
if (!cfg.inputFile().empty()) {
cerr << "Reading file '" << cfg.inputFile() << "'..." << endl;
ifile.open(cfg.inputFile().c_str());
if (!ifile) {
cerr << "Error: Can't open file." << endl;
return 1;
} else in=&ifile;
} else cerr << "Reading standard input..." << endl;
markdown::Document doc;
doc.read(*in);
if (cfg.debug()) doc.writeTokens(cout);
else doc.write(cout);
return 0;
}