"Fossies" - the Fresh Open Source Software Archive 
Member "cb2bib-2.0.1/src/c2b/texParser.cpp" (12 Feb 2021, 3900 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "texParser.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.0.0_vs_2.0.1.
1 /***************************************************************************
2 * Copyright (C) 2004-2021 by Pere Constans
3 * constans@molspaces.com
4 * cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
5 * See the LICENSE file that comes with this distribution.
6 ***************************************************************************/
7 #include "texParser.h"
8
9 #include "cb2bib_utilities.h"
10
11
12 void texParser::parse(QString tex)
13 {
14 _stream.setString(&tex, QIODevice::ReadOnly);
15 _paragraph.clear();
16 _stop_parsing = false;
17 while (!_stream.atEnd() && !_stop_parsing)
18 {
19 readLine(true);
20 if (_line.isEmpty())
21 {
22 flush();
23 parseEmptyLine();
24 }
25 else if (_line.startsWith('%'))
26 {
27 flush();
28 parseComment(_line);
29 }
30 else if (_line.startsWith('\\'))
31 doElement();
32 else
33 _paragraph += ' ' + _line;
34 }
35 flush();
36 }
37
38 void texParser::doElement()
39 {
40 if (_line.startsWith("\\begin{"))
41 gotoEnd("begin");
42 else if (_line.startsWith("\\section{"))
43 gotoEndBraces("section");
44 else if (_line.startsWith("\\subsection{"))
45 gotoEndBraces("subsection");
46 else if (_line.startsWith("\\subsubsection{"))
47 gotoEndBraces("subsubsection");
48 else if (_line.startsWith("\\title{"))
49 gotoEndBraces("title");
50 else if (_line.startsWith("\\author{"))
51 gotoEndBraces("author");
52 else if (_line.startsWith("\\newcommand{"))
53 gotoEndMacro("newcommand");
54 else if (_line.startsWith("\\documentclass"))
55 flushLaTeXMarkup("documentclass");
56 else if (_line.startsWith("\\maketitle"))
57 flushLaTeXMarkup("maketitle");
58 else if (_line.startsWith("\\end{document}"))
59 flushLaTeXMarkup("document");
60 else if (_line.startsWith("\\bibliography"))
61 flushLaTeXMarkup("bibliography");
62 else
63 _paragraph += ' ' + _line;
64 }
65
66 void texParser::gotoEnd(const QString& e)
67 {
68 QString block_name;
69 QString block_contents;
70 const int pos(e.length() + 2);
71 c2bUtils::inBraces(pos, _line, &block_name);
72 if (block_name == "document")
73 {
74 flushLaTeXMarkup("document");
75 return;
76 }
77 QRegExp end_block(QString("^\\s*\\\\end\\{%1\\}").arg(block_name));
78 _element = _line;
79 while (!_stream.atEnd())
80 {
81 readLine();
82 if (_line.contains(end_block))
83 {
84 _element += '\n' + _line.trimmed();
85 break;
86 }
87 else
88 {
89 _element += '\n' + _line;
90 block_contents += '\n' + _line;
91 }
92 }
93 flushElement(block_name, block_contents);
94 }
95
96 void texParser::gotoEndBraces(const QString& e)
97 {
98 QString in;
99 const int pos(e.length() + 2);
100 _element = _line;
101 while (!_stream.atEnd() && !c2bUtils::inBraces(pos, _element, &in))
102 _element += '\n' + readLine();
103 flushElement(e, c2bUtils::simplifyString(in));
104 }
105
106 void texParser::gotoEndMacro(const QString& e)
107 {
108 // Macro syntax: \newcommand{name}[number of arguments]{definition}
109 int line_init_pos(e.length() + 3);
110 int element_init_pos(0);
111 _element = _line;
112 while (!_stream.atEnd())
113 {
114 // Find the begining of macro definition
115 for (int pos = line_init_pos; pos < _line.length(); ++pos)
116 if (_line.at(pos) == '{')
117 {
118 QString in;
119 while (!_stream.atEnd() && !c2bUtils::inBraces(element_init_pos + pos + 1, _element, &in))
120 _element += '\n' + readLine();
121 in = _element;
122 in.remove('\\' + e);
123 flushElement(e, c2bUtils::simplifyString(in));
124 return;
125 }
126 // Try next line
127 line_init_pos = 0;
128 element_init_pos = _element.length() + 1;
129 _element += '\n' + readLine();
130 }
131 }