LibreOffice Module helpcompiler (master) 1
BasCodeTagger.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include <BasCodeTagger.hxx>
11#include <iostream>
12
14{
15 if ( doc == nullptr )
17 m_pCurrentNode = xmlDocGetRootElement( doc );
18 if ( m_pCurrentNode == nullptr )
20 else if ( m_pCurrentNode->xmlChildrenNode != nullptr )
21 m_Queue.push_back( m_pCurrentNode->xmlChildrenNode );
22 nextNode();
23}
24
26{
27
28 //next node
29 if ( m_pCurrentNode->next == nullptr )
30 {
31 m_pCurrentNode = m_Queue.front();
32 m_Queue.pop_front();
33 }
34 else
36 //queue children if they exist
37 if ( m_pCurrentNode->xmlChildrenNode != nullptr )
38 m_Queue.push_back( m_pCurrentNode->xmlChildrenNode );
39}
40
42{
43 if ( m_pCurrentNode->xmlChildrenNode != nullptr )
44 m_Queue.pop_back();
45}
46
48{
49 return m_pCurrentNode->next == nullptr && m_Queue.empty();
50}
51
52
54 m_Highlighter(HighlighterLanguage::Basic)
55{
56 if ( rootDoc == nullptr )
57 throw NULL_DOCUMENT;
58 m_pDocument = rootDoc;
59 m_pXmlTreeWalker = nullptr;
60 m_bTaggingCompleted = false;
61}
62
64{
65}
67
71{
72 xmlNodePtr currentNode;
73
75
77
78 currentNode = m_pXmlTreeWalker->currentNode();
79 if ( !( xmlStrcmp( currentNode->name, reinterpret_cast<const xmlChar*>("bascode") ) ) )
80 { //Found <bascode>
81 m_BasicCodeContainerTags.push_back( currentNode ); //it goes to the end of the list
82 }
83 while ( !m_pXmlTreeWalker->end() )
84 {
85 m_pXmlTreeWalker->nextNode();
86 if ( !( xmlStrcmp( m_pXmlTreeWalker->currentNode()->name, reinterpret_cast<const xmlChar*>("bascode") ) ) )
87 { //Found <bascode>
88 m_BasicCodeContainerTags.push_back( m_pXmlTreeWalker->currentNode() ); //it goes to the end of the list
89 m_pXmlTreeWalker->ignoreCurrNodesChildren();
90 }
91 }
92}
93
95
100{
101 //helper variables
102 xmlNodePtr currParagraph;
103 for (auto const& currBascodeNode : m_BasicCodeContainerTags)
104 {
105 currParagraph = currBascodeNode->xmlChildrenNode; //first <paragraph>
106 while ( currParagraph != nullptr )
107 {
108 tagParagraph( currParagraph );
109 currParagraph=currParagraph->next;
110 }
111 }
113}
114
116void BasicCodeTagger::tagParagraph( xmlNodePtr paragraph )
117{
118 //1. get paragraph text
119 xmlChar* codeSnippet;
120 codeSnippet = xmlNodeListGetString( m_pDocument, paragraph->xmlChildrenNode, 1 );
121 if ( codeSnippet == nullptr )
122 {
123 return; //no text, nothing more to do here
124 }
125 //2. delete every child from paragraph (except attributes)
126 xmlNodePtr curNode = paragraph->xmlChildrenNode;
127 xmlNodePtr sibling;
128 while ( curNode != nullptr )
129 {
130 sibling = curNode->next;
131 xmlUnlinkNode( curNode );
132 xmlFreeNode( curNode );
133 curNode = sibling;
134 }
135
136 //3. create new paragraph content
137 OUString strLine( reinterpret_cast<const char*>(codeSnippet),
138 strlen(reinterpret_cast<const char*>(codeSnippet)),
139 RTL_TEXTENCODING_UTF8 );
140 std::vector<HighlightPortion> portions;
141 m_Highlighter.getHighlightPortions( strLine, portions );
142 for (auto const& portion : portions)
143 {
144 OString sToken(OUStringToOString(strLine.subView(portion.nBegin, portion.nEnd-portion.nBegin), RTL_TEXTENCODING_UTF8));
145 xmlNodePtr text = xmlNewText(reinterpret_cast<const xmlChar*>(sToken.getStr()));
146 if ( portion.tokenType != TokenType::Whitespace )
147 {
148 xmlChar* typeStr = getTypeString( portion.tokenType );
149 curNode = xmlNewTextChild( paragraph, nullptr, reinterpret_cast<xmlChar const *>("item"), nullptr );
150 xmlNewProp( curNode, reinterpret_cast<xmlChar const *>("type"), typeStr );
151 xmlAddChild( curNode, text );
152 xmlFree( typeStr );
153 }
154 else
155 xmlAddChild( paragraph, text );
156 }
157 xmlFree( codeSnippet );
158}
159
161
165{
167 return;
168 //gather <bascode> nodes
169 try
170 {
172 }
173 catch (TaggerException &ex)
174 {
175 std::cout << "BasCodeTagger error occurred. Error code:" << ex << std::endl;
176 }
177
178 //tag basic code paragraphs in <bascode> tag
180 m_bTaggingCompleted = true;
181}
182
185{
186 const char* str;
187 switch ( tokenType )
188 {
189 case TokenType::Unknown :
190 str = "unknown";
191 break;
192 case TokenType::Identifier :
193 str = "identifier";
194 break;
195 case TokenType::Whitespace :
196 str = "whitespace";
197 break;
198 case TokenType::Number :
199 str = "number";
200 break;
201 case TokenType::String :
202 str = "string";
203 break;
204 case TokenType::EOL :
205 str = "eol";
206 break;
207 case TokenType::Comment :
208 str = "comment";
209 break;
210 case TokenType::Error :
211 str = "error";
212 break;
213 case TokenType::Operator :
214 str = "operator";
215 break;
216 case TokenType::Keywords :
217 str = "keyword";
218 break;
219 case TokenType::Parameter :
220 str = "parameter";
221 break;
222 default :
223 str = "unknown";
224 break;
225 }
226 return xmlCharStrdup( str );
227}
228
229/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< LibXmlTreeWalker > m_pXmlTreeWalker
BasicCodeTagger(xmlDocPtr rootDoc)
xmlDocPtr m_pDocument
void tagBasCodeParagraphs()
Extracts Basic Codes contained in <bascode> tags.
std::vector< xmlNodePtr > m_BasicCodeContainerTags
static xmlChar * getTypeString(TokenType tokenType)
Converts SyntaxHighlighter's TokenTypes enum to a type string for
void getBasicCodeContainerNodes()
Gathers all the <bascode> tag nodes from xml tree.
void tagBasicCodes()
Manages tagging process.
SyntaxHighlighter m_Highlighter
void tagParagraph(xmlNodePtr paragraph)
Used by tagBasCodeParagraphs(). It does the work on the current paragraph containing Basic code.
xmlNodePtr m_pCurrentNode
LibXmlTreeWalker(xmlDocPtr doc)
Queue for breath-first search.
void ignoreCurrNodesChildren()
std::deque< xmlNodePtr > m_Queue
bool end() const
void getHighlightPortions(std::u16string_view rLine, std::vector< HighlightPortion > &pPortions) const
def text(shape, orig_st)
@ paragraph
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
HighlighterLanguage
TokenType