mirror of https://github.com/kiwix/libkiwix.git
Fixed CTPP2 not working
* Fixed templates * Changed behavior of getHtml() to use higher level VM API
This commit is contained in:
parent
d2a2802897
commit
2a8d7fde56
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include "searcher.h"
|
||||
|
||||
|
||||
namespace kiwix {
|
||||
|
||||
/* Constructor */
|
||||
|
@ -30,13 +31,17 @@ namespace kiwix {
|
|||
estimatedResultCount(0),
|
||||
resultStart(0),
|
||||
resultEnd(0),
|
||||
resultRange(20) {
|
||||
resultRange(20),
|
||||
template_ct2("_ctpp2results.ct2")
|
||||
{
|
||||
if (!fileExists(template_ct2)) {
|
||||
writeTextFile(template_ct2.c_str(), getResourceAsString("results.ct2"));
|
||||
}
|
||||
}
|
||||
|
||||
/* Search strings in the database */
|
||||
void Searcher::search(std::string &search, const unsigned int resultStart,
|
||||
const unsigned int resultEnd, const bool verbose) {
|
||||
|
||||
this->reset();
|
||||
|
||||
if (verbose == true) {
|
||||
|
@ -108,44 +113,7 @@ namespace kiwix {
|
|||
|
||||
string Searcher::getHtml() {
|
||||
|
||||
VMOpcodeCollector oVMOpcodeCollector;
|
||||
StaticText oSyscalls;
|
||||
StaticData oStaticData;
|
||||
StaticText oStaticText;
|
||||
HashTable oHashTable;
|
||||
CTPP2Compiler oCompiler(oVMOpcodeCollector, oSyscalls, oStaticData, oStaticText, oHashTable);
|
||||
|
||||
/* Load template & create template parser */
|
||||
// cout << getResourceAsString("results.tmpl") << endl;
|
||||
|
||||
/* Parse template */
|
||||
const STLW::string & sSourceFile = getResourceAsString("results.tmpl");
|
||||
CTPP2TextLoader oSourceLoader;
|
||||
oSourceLoader.LoadTemplate(sSourceFile.c_str());
|
||||
CTPP2Parser oCTPP2Parser(&oSourceLoader, &oCompiler, "template");
|
||||
oCTPP2Parser.Compile();
|
||||
|
||||
// Get program core
|
||||
UINT_32 iCodeSize = 0;
|
||||
const VMInstruction * oVMInstruction = oVMOpcodeCollector.GetCode(iCodeSize);
|
||||
|
||||
// Dump program
|
||||
VMDumper oDumper(iCodeSize, oVMInstruction, oSyscalls, oStaticData, oStaticText, oHashTable);
|
||||
UINT_32 iSize = 0;
|
||||
const VMExecutable * aProgramCore = oDumper.GetExecutable(iSize);
|
||||
|
||||
// Memory core
|
||||
const VMMemoryCore vm_core(aProgramCore);
|
||||
|
||||
// Initiate the VM
|
||||
SyscallFactory oSyscallFactory(100);
|
||||
// Load standard library
|
||||
STDLibInitializer::InitLibrary(oSyscallFactory);
|
||||
|
||||
VM * pVM = new VM(&oSyscallFactory);
|
||||
|
||||
// Initiate the logger
|
||||
FileLogger oLogger(stderr);
|
||||
SimpleVM oSimpleVM;
|
||||
|
||||
// Fill data
|
||||
CDT oData;
|
||||
|
@ -159,10 +127,10 @@ namespace kiwix {
|
|||
result["snippet"] = this->resultOffset->snippet;
|
||||
|
||||
if (this->resultOffset->size >= 0)
|
||||
result["size"] = kiwix::beautifyInteger(this->resultOffset->size);
|
||||
result["size"] = kiwix::beautifyInteger(this->resultOffset->size);
|
||||
|
||||
if (this->resultOffset->wordCount >= 0)
|
||||
result["wordCount"] = kiwix::beautifyInteger(this->resultOffset->wordCount);
|
||||
result["wordCount"] = kiwix::beautifyInteger(this->resultOffset->wordCount);
|
||||
|
||||
resultsCDT.PushBack(result);
|
||||
this->resultOffset++;
|
||||
|
@ -188,7 +156,7 @@ namespace kiwix {
|
|||
page["end"] = (i+1) * this->resultCountPerPage;
|
||||
|
||||
if (i * this->resultCountPerPage == this->resultStart)
|
||||
page["selected"] = true;
|
||||
page["selected"] = true;
|
||||
|
||||
pagesCDT.PushBack(page);
|
||||
}
|
||||
|
@ -205,15 +173,28 @@ namespace kiwix {
|
|||
oData["searchProtocolPrefix"] = this->searchProtocolPrefix;
|
||||
oData["contentId"] = this->contentHumanReadableId;
|
||||
|
||||
STLW::string sResult;
|
||||
StringOutputCollector oDataCollector(sResult);
|
||||
// Load template file
|
||||
VMFileLoader oLoader(this->template_ct2.c_str());
|
||||
|
||||
// Run VM
|
||||
pVM->Init(&vm_core, &oDataCollector, &oLogger);
|
||||
UINT_32 iIP = 0;
|
||||
pVM -> Run(&vm_core, &oDataCollector, iIP, oData, &oLogger);
|
||||
// Create logger object
|
||||
FileLogger oLogger(stderr);
|
||||
|
||||
// Execute template and write data to standard output
|
||||
oSimpleVM.Run(oData, oLoader, stdout, oLogger);
|
||||
|
||||
// Execute template and write data to string
|
||||
std::string sResult;
|
||||
oSimpleVM.Run(oData, oLoader, sResult, oLogger);
|
||||
|
||||
return sResult;
|
||||
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
Searcher::~Searcher() {
|
||||
if (fileExists(this->template_ct2)) {
|
||||
remove(template_ct2.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,21 +29,13 @@
|
|||
#include <cctype>
|
||||
#include <vector>
|
||||
#include <resourceTools.h>
|
||||
#include <pathTools.h>
|
||||
#include <stringTools.h>
|
||||
|
||||
#include <CTPP2Parser.hpp>
|
||||
#include <CTPP2FileSourceLoader.hpp>
|
||||
#include <CTPP2ParserException.hpp>
|
||||
#include <CTPP2HashTable.hpp>
|
||||
#include <CTPP2VMDumper.hpp>
|
||||
#include <CTPP2VMOpcodes.h>
|
||||
#include <CTPP2VM.hpp>
|
||||
#include <CTPP2VMSTDLib.hpp>
|
||||
#include <CTPP2StringOutputCollector.hpp>
|
||||
#include <CTPP2SyscallFactory.hpp>
|
||||
#include <CDT.hpp>
|
||||
#include <CTPP2FileLogger.hpp>
|
||||
|
||||
#include "ctpp2/CTPP2TextLoader.hpp"
|
||||
#include <CTPP2SimpleVM.hpp>
|
||||
#include <CTPP2VMFileLoader.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace CTPP;
|
||||
|
@ -74,6 +66,7 @@ namespace kiwix {
|
|||
string getHtml();
|
||||
void reset();
|
||||
void setContentHumanReadableId(const string &contentHumanReadableId);
|
||||
~Searcher();
|
||||
|
||||
protected:
|
||||
std::string beautifyInteger(const unsigned int number);
|
||||
|
@ -86,6 +79,7 @@ namespace kiwix {
|
|||
std::string searchPattern;
|
||||
std::string protocolPrefix;
|
||||
std::string searchProtocolPrefix;
|
||||
const std::string template_ct2;
|
||||
unsigned int resultCountPerPage;
|
||||
unsigned int estimatedResultCount;
|
||||
unsigned int resultStart;
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1 +1,127 @@
|
|||
SALUT
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
|
||||
<style type="text/css">
|
||||
body{
|
||||
color: #00000;
|
||||
font: small/normal Arial,Helvetica,Sans-Serif;
|
||||
margin-top: 0.5em;
|
||||
font-size: 90%;
|
||||
}
|
||||
|
||||
a{
|
||||
color: #04c;
|
||||
}
|
||||
|
||||
a:visited {
|
||||
color: #639
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline
|
||||
}
|
||||
|
||||
.header {
|
||||
font-size: 120%;
|
||||
}
|
||||
|
||||
ul {
|
||||
margin:0;
|
||||
padding:0
|
||||
}
|
||||
|
||||
.results {
|
||||
font-size: 110%;
|
||||
}
|
||||
|
||||
.results li {
|
||||
list-style-type:none;
|
||||
margin-top: 0.5em;
|
||||
}
|
||||
|
||||
.results a {
|
||||
font-size: 110%;
|
||||
text-decoration: underline
|
||||
}
|
||||
|
||||
cite {
|
||||
font-style:normal;
|
||||
word-wrap:break-word;
|
||||
display: block;
|
||||
font-size: 100%;
|
||||
}
|
||||
|
||||
.informations {
|
||||
color: #388222;
|
||||
font-size: 100%;
|
||||
}
|
||||
|
||||
.footer {
|
||||
padding: 0;
|
||||
margin-top: 1em;
|
||||
width: 100%;
|
||||
float: left
|
||||
}
|
||||
|
||||
.footer a, .footer span {
|
||||
display: block;
|
||||
padding: .3em .7em;
|
||||
margin: 0 .38em 0 0;
|
||||
text-align:center;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.footer a:hover {
|
||||
background: #ededed;
|
||||
}
|
||||
|
||||
.footer ul, .footer li {
|
||||
list-style:none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.footer li {
|
||||
float: left;
|
||||
}
|
||||
|
||||
.selected {
|
||||
background: #ededed;
|
||||
}
|
||||
|
||||
</style>
|
||||
<title>Search: <TMPL_var searchPattern></title>
|
||||
</head>
|
||||
<body bgcolor="white">
|
||||
<div class="header">
|
||||
<TMPL_if results>Results <b><TMPL_var resultStart>-<TMPL_var resultEnd></b> of <b><TMPL_var count></b> for <b><TMPL_var searchPattern></b><TMPL_else>No result were found for <b><TMPL_var searchPattern></b></TMPL_if>
|
||||
</div>
|
||||
|
||||
<div class="results">
|
||||
<ul>
|
||||
<TMPL_foreach results as result>
|
||||
<li><a href="<TMPL_var protocolPrefix><TMPL_var contentId>/<TMPL_var result.url>"><TMPL_var result.title></a>
|
||||
<cite><TMPL_if result.snippet><TMPL_var result.snippet>...</TMPL_if></cite>
|
||||
<TMPL_if wordCount><div class="informations"><TMPL_var wordCount> words</div></TMPL_if>
|
||||
</li>
|
||||
</TMPL_foreach>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<ul>
|
||||
<TMPL_if (resultLastPageStart>0)>
|
||||
<li><a href="<TMPL_var searchProtocolPrefix>content=<TMPL_var contentId>&pattern=<TMPL_var searchPatternEncoded>&start=0&end=<TMPL_var resultRange>">◀</a></li>
|
||||
</TMPL_if>
|
||||
<TMPL_foreach pages as page>
|
||||
<li><a <TMPL_if page.selected>class="selected"</TMPL_if> href="<TMPL_var searchProtocolPrefix>content=<TMPL_var contentId>&pattern=<TMPL_var searchPatternEncoded>&start=<TMPL_var page.start>&end=<TMPL_var page.end>"><TMPL_var page.label></a></li>
|
||||
</TMPL_foreach>
|
||||
<TMPL_if (resultLastPageStart>0)>
|
||||
<li><a href="<TMPL_var searchProtocolPrefix>content=<TMPL_var contentId>&pattern=<TMPL_var searchPatternEncoded>&start=<TMPL_var resultLastPageStart>&end=<TMPL_var (resultLastPageStart+resultRange)>">▶</a></li>
|
||||
</TMPL_if>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue