tdf#143990: writer: default value for SetIncludeUpperLevels is 1
[LibreOffice.git] / helpcompiler / source / HelpCompiler.cxx
blobee4a27461afa0906445c6dcebfff194999e44381
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <algorithm>
22 #include <memory>
23 #include <HelpCompiler.hxx>
24 #include <BasCodeTagger.hxx>
25 #include <iostream>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <libxslt/xsltInternals.h>
29 #include <libxslt/transform.h>
30 #include <rtl/character.hxx>
31 #include <sal/log.hxx>
33 HelpCompiler::HelpCompiler(StreamTable &in_streamTable, const fs::path &in_inputFile,
34 const fs::path &in_src, const fs::path &in_zipdir, const fs::path &in_resCompactStylesheet,
35 const fs::path &in_resEmbStylesheet, const std::string &in_module, const std::string &in_lang,
36 bool in_bExtensionMode)
37 : streamTable(in_streamTable), inputFile(in_inputFile),
38 src(in_src), zipdir(in_zipdir), module(in_module), lang(in_lang), resCompactStylesheet(in_resCompactStylesheet),
39 resEmbStylesheet(in_resEmbStylesheet), bExtensionMode( in_bExtensionMode )
41 xmlKeepBlanksDefaultValue = 0;
42 char* os = getenv("OS");
43 if (os)
45 gui = (strcmp(os, "WNT") ? "UNIX" : "WIN");
46 gui = (strcmp(os, "MACOSX") ? gui : "MAC");
50 void HelpCompiler::tagBasicCodeExamples( xmlDocPtr doc )
52 try
54 BasicCodeTagger bct( doc );
55 bct.tagBasicCodes();
57 catch ( BasicCodeTagger::TaggerException &ex )
59 if ( ex != BasicCodeTagger::EMPTY_DOCUMENT )
60 throw;
64 xmlDocPtr HelpCompiler::compactXhpForJar( xmlDocPtr doc )
66 static xsltStylesheetPtr compact = nullptr;
67 static const char *params[2 + 1];
68 params[0] = nullptr;
69 xmlDocPtr compacted;
71 if (!compact)
73 compact = xsltParseStylesheetFile(reinterpret_cast<const xmlChar *>(resCompactStylesheet.native_file_string().c_str()));
76 compacted = xsltApplyStylesheet(compact, doc, params);
77 return compacted;
80 void HelpCompiler::saveXhpForJar( xmlDocPtr doc, const fs::path &filePath )
82 //save processed xhp document in ziptmp<module>_<lang>/text directory
83 #ifdef _WIN32
84 std::string pathSep = "\\";
85 #else
86 std::string pathSep = "/";
87 #endif
88 const std::string& sourceXhpPath = filePath.native_file_string();
89 std::string zipdirPath = zipdir.native_file_string();
90 const std::string srcdirPath( src.native_file_string() );
91 // srcdirPath contains trailing /, but we want the file path with / at the beginning
92 std::string jarXhpPath = sourceXhpPath.substr( srcdirPath.length() - 1 );
93 std::string xhpFileName = jarXhpPath.substr( jarXhpPath.rfind( pathSep ) + 1 );
94 jarXhpPath = jarXhpPath.substr( 0, jarXhpPath.rfind( pathSep ) );
95 if ( !jarXhpPath.compare( 1, 11, "text" + pathSep + "sbasic" ) )
97 tagBasicCodeExamples( doc );
99 if ( !jarXhpPath.compare( 1, 11, "text" + pathSep + "shared" ) )
101 const size_t pos = zipdirPath.find( "ziptmp" );
102 if ( pos != std::string::npos )
103 zipdirPath.replace( pos + 6, module.length(), "shared" );
105 xmlDocPtr compacted = compactXhpForJar( doc );
106 fs::create_directory( fs::path( zipdirPath + jarXhpPath, fs::native ) );
107 if ( -1 == xmlSaveFormatFileEnc( (zipdirPath + jarXhpPath + pathSep + xhpFileName).c_str(), compacted, "utf-8", 0 ) )
108 std::cerr << "Error saving file to " << (zipdirPath + jarXhpPath + pathSep + xhpFileName).c_str() << std::endl;
109 xmlFreeDoc(compacted);
112 xmlDocPtr HelpCompiler::getSourceDocument(const fs::path &filePath)
114 xmlDocPtr res;
115 if (bExtensionMode)
117 // this is the mode when used within LibreOffice for importing help
118 // bundled with an extension
119 res = xmlParseFile(filePath.native_file_string().c_str());
121 else
123 // this is the mode when used at build time to generate LibreOffice
124 // help from its xhp source
125 static xsltStylesheetPtr cur = nullptr;
126 static const char *params[2 + 1];
127 if (!cur)
129 static std::string fsroot('\'' + src.toUTF8() + '\'');
131 cur = xsltParseStylesheetFile(reinterpret_cast<const xmlChar *>(resEmbStylesheet.native_file_string().c_str()));
133 int nbparams = 0;
134 params[nbparams++] = "fsroot";
135 params[nbparams++] = fsroot.c_str();
136 params[nbparams] = nullptr;
138 xmlDocPtr doc = xmlParseFile(filePath.native_file_string().c_str());
140 saveXhpForJar( doc, filePath );
142 res = xsltApplyStylesheet(cur, doc, params);
143 xmlFreeDoc(doc);
145 return res;
148 // returns a node representing the whole stuff compiled for the current
149 // application.
150 xmlNodePtr HelpCompiler::clone(xmlNodePtr node, const std::string& appl)
152 xmlNodePtr root = xmlCopyNode(node, 2);
153 if (node->xmlChildrenNode)
155 xmlNodePtr list = node->xmlChildrenNode;
156 while (list)
158 if (strcmp(reinterpret_cast<const char*>(list->name), "switchinline") == 0 || strcmp(reinterpret_cast<const char*>(list->name), "switch") == 0)
160 std::string tmp="";
161 xmlChar * prop = xmlGetProp(list, reinterpret_cast<xmlChar const *>("select"));
162 if (prop != nullptr)
164 if (strcmp(reinterpret_cast<char *>(prop), "sys") == 0)
166 tmp = gui;
168 else if (strcmp(reinterpret_cast<char *>(prop), "appl") == 0)
170 tmp = appl;
172 xmlFree(prop);
174 if (tmp.compare("") != 0)
176 bool isCase=false;
177 xmlNodePtr caseList=list->xmlChildrenNode;
178 while (caseList)
180 xmlChar *select = xmlGetProp(caseList, reinterpret_cast<xmlChar const *>("select"));
181 if (select)
183 if (!strcmp(reinterpret_cast<char*>(select), tmp.c_str()) && !isCase)
185 isCase=true;
186 xmlNodePtr clp = caseList->xmlChildrenNode;
187 while (clp)
189 xmlAddChild(root, clone(clp, appl));
190 clp = clp->next;
193 xmlFree(select);
195 else
197 if ((strcmp(reinterpret_cast<const char*>(caseList->name), "defaultinline") != 0) && (strcmp(reinterpret_cast<const char*>(caseList->name), "default") != 0))
199 xmlAddChild(root, clone(caseList, appl));
201 else
203 if (!isCase)
205 xmlNodePtr clp = caseList->xmlChildrenNode;
206 while (clp)
208 xmlAddChild(root, clone(clp, appl));
209 clp = clp->next;
214 caseList = caseList->next;
218 else
220 xmlAddChild(root, clone(list, appl));
222 list = list->next;
225 return root;
228 namespace {
230 class myparser
232 public:
233 std::string documentId;
234 std::string fileName;
235 std::string title;
236 std::unique_ptr< std::vector<std::string> > hidlist;
237 std::unique_ptr<Hashtable> keywords;
238 std::unique_ptr<Stringtable> helptexts;
239 private:
240 std::vector<std::string> extendedHelpText;
241 public:
242 myparser(const std::string &indocumentId, const std::string &infileName,
243 const std::string &intitle) : documentId(indocumentId), fileName(infileName),
244 title(intitle)
246 hidlist.reset(new std::vector<std::string>);
247 keywords.reset(new Hashtable);
248 helptexts.reset(new Stringtable);
250 void traverse( xmlNodePtr parentNode );
251 private:
252 std::string dump(xmlNodePtr node);
257 std::string myparser::dump(xmlNodePtr node)
259 std::string app;
260 if (node->xmlChildrenNode)
262 xmlNodePtr list = node->xmlChildrenNode;
263 while (list)
265 app += dump(list);
266 list = list->next;
269 if (xmlNodeIsText(node))
271 xmlChar *pContent = xmlNodeGetContent(node);
272 app += std::string(reinterpret_cast<char*>(pContent));
273 xmlFree(pContent);
275 return app;
278 static void trim(std::string& str)
280 std::string::size_type pos = str.find_last_not_of(' ');
281 if(pos != std::string::npos)
283 str.erase(pos + 1);
284 pos = str.find_first_not_of(' ');
285 if(pos != std::string::npos)
286 str.erase(0, pos);
288 else
289 str.clear();
292 void myparser::traverse( xmlNodePtr parentNode )
294 // traverse all nodes that belong to the parent
295 xmlNodePtr test ;
296 for (test = parentNode->xmlChildrenNode; test; test = test->next)
298 if (fileName.empty() && !strcmp(reinterpret_cast<const char*>(test->name), "filename"))
300 xmlNodePtr node = test->xmlChildrenNode;
301 if (xmlNodeIsText(node))
303 xmlChar *pContent = xmlNodeGetContent(node);
304 fileName = std::string(reinterpret_cast<char*>(pContent));
305 xmlFree(pContent);
308 else if (title.empty() && !strcmp(reinterpret_cast<const char*>(test->name), "title"))
310 title = dump(test);
311 if (title.empty())
312 title = "<notitle>";
314 else if (!strcmp(reinterpret_cast<const char*>(test->name), "bookmark"))
316 xmlChar *branchxml = xmlGetProp(test, reinterpret_cast<const xmlChar*>("branch"));
317 if (branchxml == nullptr) {
318 throw HelpProcessingException(
319 HelpProcessingErrorClass::XmlParsing, "bookmark lacks branch attribute");
321 std::string branch(reinterpret_cast<char*>(branchxml));
322 xmlFree (branchxml);
323 xmlChar *idxml = xmlGetProp(test, reinterpret_cast<const xmlChar*>("id"));
324 if (idxml == nullptr) {
325 throw HelpProcessingException(
326 HelpProcessingErrorClass::XmlParsing, "bookmark lacks id attribute");
328 std::string anchor(reinterpret_cast<char*>(idxml));
329 xmlFree (idxml);
331 if (branch.compare(0, 3, "hid") == 0)
333 size_t index = branch.find('/');
334 if (index != std::string::npos)
336 auto hid = branch.substr(1 + index);
337 // one shall serve as a documentId
338 if (documentId.empty())
339 documentId = hid;
340 extendedHelpText.push_back(hid);
341 HCDBG(std::cerr << "hid pushback" << (anchor.empty() ? hid : hid + "#" + anchor) << std::endl);
342 hidlist->push_back( anchor.empty() ? hid : hid + "#" + anchor);
344 else
345 continue;
347 else if (branch.compare("index") == 0)
349 LinkedList ll;
351 for (xmlNodePtr nd = test->xmlChildrenNode; nd; nd = nd->next)
353 if (strcmp(reinterpret_cast<const char*>(nd->name), "bookmark_value"))
354 continue;
356 std::string embedded;
357 xmlChar *embeddedxml = xmlGetProp(nd, reinterpret_cast<const xmlChar*>("embedded"));
358 if (embeddedxml)
360 embedded = std::string(reinterpret_cast<char*>(embeddedxml));
361 xmlFree (embeddedxml);
362 std::transform (embedded.begin(), embedded.end(),
363 embedded.begin(), tocharlower);
366 bool isEmbedded = !embedded.empty() && embedded.compare("true") == 0;
367 if (isEmbedded)
368 continue;
370 std::string keyword = dump(nd);
371 size_t keywordSem = keyword.find(';');
372 if (keywordSem != std::string::npos)
374 std::string tmppre =
375 keyword.substr(0,keywordSem);
376 trim(tmppre);
377 std::string tmppos =
378 keyword.substr(1+keywordSem);
379 trim(tmppos);
380 keyword = tmppre + ";" + tmppos;
382 ll.push_back(keyword);
384 if (!ll.empty())
385 (*keywords)[anchor] = ll;
387 else if (branch.compare("contents") == 0)
389 // currently not used
392 else if (!strcmp(reinterpret_cast<const char*>(test->name), "ahelp"))
394 //tool-tip
395 std::string text = dump(test);
396 std::replace(text.begin(), text.end(), '\n', ' ');
397 trim(text);
399 //tool-tip target
400 std::string hidstr("."); //. == previous seen hid bookmarks
401 xmlChar *hid = xmlGetProp(test, reinterpret_cast<const xmlChar*>("hid"));
402 if (hid)
404 hidstr = std::string(reinterpret_cast<char*>(hid));
405 xmlFree (hid);
408 if (hidstr != "." && !hidstr.empty()) //simple case of explicitly named target
410 assert(!hidstr.empty());
411 (*helptexts)[hidstr] = text;
413 else //apply to list of "current" hids determined by recent bookmarks that have hid in their branch
415 //TODO: make these asserts and flush out all our broken help ids
416 SAL_WARN_IF(hidstr.empty(), "helpcompiler", "hid='' for text:" << text);
417 SAL_WARN_IF(!hidstr.empty() && extendedHelpText.empty(), "helpcompiler", "hid='.' with no hid bookmark branches in file: " << fileName + " for text: " << text);
418 for (const std::string& name : extendedHelpText)
420 (*helptexts)[name] = text;
423 extendedHelpText.clear();
425 // traverse children
426 traverse(test);
430 void HelpCompiler::compile()
432 // we now have the jaroutputstream, which will contain the document.
433 // now determine the document as a dom tree in variable docResolved
435 xmlDocPtr docResolvedOrg = getSourceDocument(inputFile);
437 // now add path to the document
438 // resolve the dom
440 if (!docResolvedOrg)
442 std::stringstream aStrStream;
443 aStrStream << "ERROR: file not existing: " << inputFile.native_file_string().c_str() << std::endl;
444 throw HelpProcessingException( HelpProcessingErrorClass::General, aStrStream.str() );
447 std::string documentId;
448 std::string fileName;
449 std::string title;
450 // returns a clone of the document with switch-cases resolved
451 std::string appl = module.substr(1);
452 for (char & i : appl)
454 i=rtl::toAsciiUpperCase(static_cast<unsigned char>(i));
456 xmlNodePtr docResolved = clone(xmlDocGetRootElement(docResolvedOrg), appl);
457 myparser aparser(documentId, fileName, title);
458 aparser.traverse(docResolved);
459 documentId = aparser.documentId;
460 fileName = aparser.fileName;
461 title = aparser.title;
463 HCDBG(std::cerr << documentId << " : " << fileName << " : " << title << std::endl);
465 xmlDocPtr docResolvedDoc = xmlCopyDoc(docResolvedOrg, false);
466 xmlDocSetRootElement(docResolvedDoc, docResolved);
468 streamTable.dropappl();
469 streamTable.appl_doc = docResolvedDoc;
470 streamTable.appl_hidlist = std::move(aparser.hidlist);
471 streamTable.appl_helptexts = std::move(aparser.helptexts);
472 streamTable.appl_keywords = std::move(aparser.keywords);
474 streamTable.document_path = fileName;
475 streamTable.document_title = title;
476 std::string actMod = module;
478 if ( !bExtensionMode && !fileName.empty())
480 if (fileName.compare(0, 6, "/text/") == 0)
482 actMod = fileName.substr(strlen("/text/"));
483 actMod = actMod.substr(0, actMod.find('/'));
486 streamTable.document_module = actMod;
487 xmlFreeDoc(docResolvedOrg);
490 namespace fs
492 void create_directory(const fs::path& indexDirName)
494 HCDBG(
495 std::cerr << "creating " <<
496 OUStringToOString(indexDirName.data, RTL_TEXTENCODING_UTF8).getStr()
497 << std::endl
499 osl::Directory::createPath(indexDirName.data);
502 void copy(const fs::path &src, const fs::path &dest)
504 osl::File::copy(src.data, dest.data);
508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */