File: sourcehighlight.cpp

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (401 lines) | stat: -rw-r--r-- 12,584 bytes parent folder | download | duplicates (6)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2004-2008
//
// Copyright: See COPYING file that comes with this distribution
//

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <fstream>

#include "sourcehighlight.h"
#include "fileutil.h"
#include "formattermanager.h"
#include "textstyles.h"
#include "preformatter.h"
#include "parsestyles.h"
#include "textstyleformatter.h"
#include "textstyleformatterfactory.h"
#include "outlangdefparserfun.h"
#include "langdefmanager.h"
#include "regexrulefactory.h"
#include "highlightstate.h"
#include "sourcehighlighter.h"
#include "bufferedoutput.h"
#include "sourcefilehighlighter.h"
#include "linenumgenerator.h"
#include "ioexception.h"
#include "docgenerator.h"
#include "srcuntabifier.h"
#include "langmap.h"
#include "parserexception.h"
#include "ctagsmanager.h"
#include "ctagsformatter.h"
#include "highlightstateprinter.h"
#include "langelemsprinter.hpp"
#include "langelems.h"
#include "verbosity.h"
#include "settings.h"

using namespace std;

namespace srchilite {

SourceHighlight::SourceHighlight(const std::string &_outputLang) :
    outputLang(_outputLang), dataDir(Settings::retrieveDataDir()), styleFile(
            "default.style"), formatterManager(0), preFormatter(0),
            langDefManager(new LangDefManager(new RegexRuleFactory)),
            lineNumGenerator(0), docGenerator(0), noDocGenerator(0),
            highlightEventListener(0), ctagsManager(0), ctagsFormatter(0),
            lineRanges(0), regexRanges(0), optimize(true), generateLineNumbers(
                    false), generateLineNumberRefs(false), lineNumberPad('0'),
            lineNumberDigits(0), generateEntireDoc(false),
            generateVersion(true), canUseStdOut(true), binaryOutput(false),
            tabSpaces(0) {
}

SourceHighlight::~SourceHighlight() {
    if (formatterManager)
        delete formatterManager;

    if (preFormatter)
        delete preFormatter;

    delete langDefManager->getRuleFactory();
    delete langDefManager;

    if (lineNumGenerator)
        delete lineNumGenerator;

    if (docGenerator)
        delete docGenerator;

    if (noDocGenerator)
        delete noDocGenerator;

    if (ctagsFormatter)
        delete ctagsFormatter;
}

void SourceHighlight::initialize() {
    if (formatterManager)
        return; // already initialized

    TextStylesPtr textStyles = parse_outlang_def(dataDir.c_str(),
            outputLang.c_str());

    FormatterPtr defaultFormatter(new TextStyleFormatter("$text"));
    formatterManager = new FormatterManager(defaultFormatter);

    if (tabSpaces) {
        preFormatter = new Untabifier(tabSpaces);
        preFormatter->setPreFormatter(PreFormatterPtr(new PreFormatter(
                textStyles->charTranslator)));
    } else {
        preFormatter = new PreFormatter(textStyles->charTranslator);
    }

    linePrefix = textStyles->line_prefix;

    if (ctagsManager) {
        ctagsFormatter = ctagsManager->createCTagsFormatter(
                textStyles->refstyle);
        ctagsFormatter->setPreFormatter(preFormatter);
    }

    TextStyleFormatterFactory formatterFactory(textStyles, preFormatter,
            ctagsFormatter, formatterManager);

    if (styleCssFile.size())
        parseCssStyles(dataDir, styleCssFile, &formatterFactory,
                backgroundColor);
    else
        parseStyles(dataDir, styleFile, &formatterFactory, backgroundColor);

    // keep the background color empty if none is specified
    if (backgroundColor != "")
        backgroundColor = formatterFactory.preprocessColor(backgroundColor);

    formatterFactory.addDefaultFormatter();

    // use the style default file to build missing formatters
    if (styleDefaultFile.size()) {
        LangMap defaultStyles(dataDir, styleDefaultFile);
        defaultStyles.open();
        for (LangMap::const_iterator it = defaultStyles.begin(); it
                != defaultStyles.end(); ++it) {
            formatterFactory.createMissingFormatter(it->first, it->second);
        }
    }

    formatterCollection = formatterFactory.getFormatterCollection();

    // initialize the line number generator
    TextStyleFormatter *lineNumFormatter =
            dynamic_cast<TextStyleFormatter *> (formatterManager->getFormatter(
                    "linenum").get());
    lineNumGenerator = new LineNumGenerator(lineNumFormatter->toString(), 5,
            lineNumberPad);
    lineNumGenerator->setAnchorPrefix(lineNumberAnchorPrefix);
    if (generateLineNumberRefs)
        lineNumGenerator->setAnchorStyle(textStyles->refstyle.anchor);

    docGenerator = new DocGenerator(textStyles->docTemplate.toStringBegin(),
            textStyles->docTemplate.toStringEnd());
    noDocGenerator = new DocGenerator(
            textStyles->noDocTemplate.toStringBegin(),
            textStyles->noDocTemplate.toStringEnd());

    docGenerator->set_gen_version(generateVersion);
    noDocGenerator->set_gen_version(generateVersion);

    docGenerator->setBackgroundColor(backgroundColor);
    noDocGenerator->setBackgroundColor(backgroundColor);

    docGenerator->setCss(css);
    noDocGenerator->setCss(css);

    // open possible header and footer files
    if (headerFileName.size()) {
        const string &header = readFile(headerFileName);
        docGenerator->setHeader(header);
        noDocGenerator->setHeader(header);
    }

    if (footerFileName.size()) {
        const string &footer = readFile(footerFileName);
        docGenerator->setFooter(footer);
        noDocGenerator->setFooter(footer);
    }

    // set the preformatter in all the formatters
    for (TextStyleFormatterCollection::const_iterator it =
            formatterCollection.begin(); it != formatterCollection.end(); ++it) {
        (*it)->setPreFormatter(preFormatter);
    }

    outputFileExtension = textStyles->file_extension;
}

void SourceHighlight::highlight(const std::string &input,
        const std::string &_output, const std::string &inputLang) {

    initialize();

    // the output file name could be empty, and we might generate it, so make a copy
    string output = _output;

    HighlightStatePtr highlightState = langDefManager->getHighlightState(
            dataDir, inputLang);

    // compute line number digits (if a file name is specified)
    if (generateLineNumbers && input.size()) {
        ifstream is(input.c_str());

        if (!is) {
            throw IOException("cannot open input file", input);
        }

        unsigned int lines = get_line_count(is);

        int line_num_digit = 0;
        while (lines) {
            ++line_num_digit;
            lines /= 10;
        }

        lineNumGenerator->setDigitNum(line_num_digit);
    }

    ifstream is;
    ofstream os;

    bool use_stdin = !input.size();

    // in case of multiple files we must disable generation to stdout.
    bool use_stdout = (!output.size() && canUseStdOut) || output == "STDOUT";

    if (!use_stdin) {
        is.open(input.c_str());
        if (!is) {
            throw IOException("cannot open input file", input);
        }
    }

    if (!use_stdout) {
        // in case the output file is not specified
        if (!output.size()) {
            // in this case we need the extension defined in the outlang
            if (!outputFileExtension.size()) {
                // we can't continue
                ParserException e("missing file extension in " + outputLang,
                        "source-highlight");
                e.additional
                        = "this is needed when the output file is not specified";
                throw e;
            }

            // generate the output file name starting from known info
            output = createOutputFileName(input);
        }

        if (binaryOutput) {
            os.open(output.c_str(), std::ios::out | std::ios::binary);
        } else {
            os.open(output.c_str());
        }

        if (!os) {
            throw IOException("cannot open output file", output);
        }
    }

    docGenerator->setInputFileName(input);
    noDocGenerator->setInputFileName(input);

    if (!title.size()) {
        // if the title was not set, then use the input file name
        docGenerator->setTitle(input);
        noDocGenerator->setTitle(input);
    }

    docGenerator->setInputLang(inputLang);
    noDocGenerator->setInputLang(inputLang);      

    if (ctagsFormatter) {
        // if we need to generate references, then set the file info
        ctagsFormatter->setFileInfo(input, output);
    }

    highlight((use_stdin ? cin : is), (use_stdout ? cout : os), inputLang,
            input);

    if (is.is_open())
        is.close();
    if (os.is_open())
        os.close();
}

const string SourceHighlight::createOutputFileName(const std::string &inputFile) {
    return srchilite::createOutputFileName(inputFile, outputFileDir,
            outputFileExtension);
}

void SourceHighlight::highlight(std::istream &input, std::ostream &output,
        const std::string &inputLang, const std::string &inputFileName) {

    initialize();

    HighlightStatePtr highlightState = langDefManager->getHighlightState(
            dataDir, inputLang);

    SourceHighlighter highlighter(highlightState);
    highlighter.setFormatterManager(formatterManager);
    highlighter.setOptimize(optimize);
    if (highlightEventListener)
        highlighter.addListener(highlightEventListener);

    BufferedOutput bufferedOutput(output);

    // if no optimization, then always flush the output
    if (!optimize)
        bufferedOutput.setAlwaysFlush(true);

    updateBufferedOutput(&bufferedOutput);

    SourceFileHighlighter fileHighlighter(inputFileName, &highlighter,
            &bufferedOutput);

    fileHighlighter.setLineRanges(lineRanges);
    fileHighlighter.setRegexRanges(regexRanges);

    if (generateLineNumbers) {
        fileHighlighter.setLineNumGenerator(lineNumGenerator);
        if (lineNumberDigits != 0) {
            lineNumGenerator->setDigitNum(lineNumberDigits);
        }
    }

    // set the prefix for all lines
    fileHighlighter.setLinePrefix(linePrefix);

    fileHighlighter.setPreformatter(preFormatter);

    // set the range separator only after the preformatter!
    // since the separator itself might have to be preformatted
    if (rangeSeparator.size()) {
        fileHighlighter.setRangeSeparator(rangeSeparator);
    }

    // the formatter for possible context lines
    fileHighlighter.setContextFormatter(formatterManager->getFormatter(
            "context").get());

    DocGenerator *documentGenerator = (generateEntireDoc ? docGenerator
            : noDocGenerator);

    if (title.size()) {
        // for each output set the title
        documentGenerator->setTitle(title);
    }

    documentGenerator->setInputLang(inputLang);

    // first generate the start of the output file
    documentGenerator->generate_start_doc(&output);

    fileHighlighter.highlight(input);

    // finally generate the end of the output file
    documentGenerator->generate_end_doc(&output);

    // since the highlighter can be re-used, we need to remove the
    // listener for other runs
    if (highlightEventListener)
        highlighter.removeListener(highlightEventListener);
}

void SourceHighlight::checkLangDef(const std::string &langFile) {
    // make sure to build the highlight state each time
    langDefManager->buildHighlightState(dataDir, langFile);

    // if we get here, no problems were found, otherwise this method
    // exits with an exception
}

void SourceHighlight::checkOutLangDef(const std::string &outlangFile) {
    // make sure to build the highlight state each time
    parse_outlang_def(dataDir.c_str(), outlangFile.c_str());

    // if we get here, no problems were found, otherwise this method
    // exits with an exception
}

void SourceHighlight::printHighlightState(const std::string &langFile,
        std::ostream &os) {
    HighlightStatePrinter printer(os);

    printer.printHighlightState(langDefManager->buildHighlightState(dataDir,
            langFile).get());
}

void SourceHighlight::printLangElems(const std::string &langFile,
        std::ostream &os) {
    LangElemsPrinter printer;
    LangElems *elems = langDefManager->getLangElems(dataDir, langFile);

    printer.print(elems, os);

    delete elems;
}

void SourceHighlight::updateBufferedOutput(BufferedOutput *output) {
    for (TextStyleFormatterCollection::const_iterator it =
            formatterCollection.begin(); it != formatterCollection.end(); ++it) {
        (*it)->setBufferedOutput(output);
    }
}

}