File: textstyleformatter.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 (100 lines) | stat: -rw-r--r-- 3,313 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
/**
 * C++ class: textstyleformatter.cpp
 *
 * Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2005-2008
 * Copyright: See COPYING file that comes with this distribution
 */

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

#include "textstyleformatter.h"
#include "bufferedoutput.h"
#include "preformatter.h"
#include "ctagsformatter.h"
#include "wordtokenizer.h"
#include <sstream>

using namespace std;

namespace srchilite {

TextStyleFormatter::TextStyleFormatter(const TextStyle &style, BufferedOutput *o) :
    textstyle(style), output(o), preFormatter(0), ctagsFormatter(0) {
}

TextStyleFormatter::TextStyleFormatter(const string &repr, BufferedOutput *o) :
    textstyle(TextStyle(repr)), output(o), preFormatter(0), ctagsFormatter(0) {
}

void TextStyleFormatter::format(const string &s, const FormatterParams *params) {
    // first check whether the reference formatter can format the paragraph
    if (!formatReferences(s, params))
        doFormat(s); // otherwise we do it ourselves
}

void TextStyleFormatter::doFormat(const string &s, bool preformat) {
    if (!s.size())
        return;

    if (preFormatter && preformat) {
        output->output(textstyle.output(preFormatter->preformat(s)));
    } else {
        output->output(textstyle.output(s));
    }
}

bool TextStyleFormatter::formatReferences(const string &s,
        const FormatterParams *params) {
    if (ctagsFormatter && params) {
        // for each word in s try to format an anchor or reference
        WordTokenizer::WordTokenizerResults tokens;
        WordTokenizer::tokenize(s, tokens);

        // here we buffer parts that are not references (or spaces)
        ostringstream notReferences;
        CTagsFormatterResults results;

        for (WordTokenizer::WordTokenizerResults::const_iterator token =
                tokens.begin(); token != tokens.end(); ++token) {
            if (token->first.size())  { // a space
                notReferences << token->first;
            } else {
                if (ctagsFormatter->formatCTags(token->second, results, params)) {
                    // first format the previous non reference parts if any
                    doFormat(notReferences.str());
                    notReferences.str("");

                    // then the anchors and references
                    if (results.inlineResult.size()) {
                        // don't preformat: it's already been done
                        doFormat(results.inlineResult, false);
                    } else {
                        // inline result excludes the other ones
                        output->postLineInsertFrom(results.postLineResult);
                        output->postDocInsertFrom(results.postDocResult);

                        // we still need to format this word, since otherwise
                        // it won't appear in the text of the program
                        doFormat(token->second);
                    }

                    // clear results for possible other formatting
                    results.clear();
                } else {
                    notReferences << token->second;
                }
            }
        }

        // format possible buffered tokens
        doFormat(notReferences.str());

        return true;
    }

    return false;
}

}