Merge pull request #64 from kiwix/generate_ctpp2_template

Use ctpp2c to generate template from source instead of use generated one.
This commit is contained in:
Matthieu Gautier 2017-07-12 15:50:24 +02:00 committed by GitHub
commit baf254f1aa
7 changed files with 173 additions and 12 deletions

View File

@ -38,12 +38,21 @@ extern const std::string {identifier};
{namespaces_close}""" {namespaces_close}"""
class Resource: class Resource:
def __init__(self, base_dir, filename): def __init__(self, base_dirs, filename):
filename = filename.strip() filename = filename.strip()
self.filename = filename self.filename = filename
self.identifier = full_identifier(filename) self.identifier = full_identifier(filename)
found = False
for base_dir in base_dirs:
try:
with open(os.path.join(base_dir, filename), 'rb') as f: with open(os.path.join(base_dir, filename), 'rb') as f:
self.data = f.read() self.data = f.read()
found = True
break
except FileNotFoundError:
continue
if not found:
raise Exception("Impossible to found {}".format(filename))
def dump_impl(self): def dump_impl(self):
nb_row = len(self.data)//16 + (1 if len(self.data) % 16 else 0) nb_row = len(self.data)//16 + (1 if len(self.data) % 16 else 0)
@ -151,13 +160,17 @@ if __name__ == "__main__":
help='The Cpp file name to generate') help='The Cpp file name to generate')
parser.add_argument('--hfile', parser.add_argument('--hfile',
help='The h file name to generate') help='The h file name to generate')
parser.add_argument('--source_dir',
help="Additional directory where to look for resources.",
action='append')
parser.add_argument('resource_file', parser.add_argument('resource_file',
help='The list of resources to compile.') help='The list of resources to compile.')
args = parser.parse_args() args = parser.parse_args()
base_dir = os.path.dirname(os.path.realpath(args.resource_file)) base_dir = os.path.dirname(os.path.realpath(args.resource_file))
with open(args.resource_file, 'r') as f: with open(args.resource_file, 'r') as f:
resources = [Resource(base_dir, filename) for filename in f.readlines()] resources = [Resource([base_dir]+args.source_dir, filename)
for filename in f.readlines()]
h_identifier = to_identifier(os.path.basename(args.hfile)) h_identifier = to_identifier(os.path.basename(args.hfile))
with open(args.hfile, 'w') as f: with open(args.hfile, 'w') as f:

8
scripts/ctpp2c.sh Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
ctpp2c=$1
SOURCE=$(pwd)/$2
DEST=$3
$ctpp2c $SOURCE $DEST

View File

@ -1,4 +1,5 @@
res_compiler = find_program('compile_resources.py') res_compiler = find_program('compile_resources.py')
intermediate_ctpp2c = find_program('ctpp2c.sh')
install_data(res_compiler.path(), install_dir:get_option('bindir')) install_data(res_compiler.path(), install_dir:get_option('bindir'))

View File

@ -1,7 +1,19 @@
ctpp2c = find_program('ctpp2c')
search_result_template = custom_target('result_template',
input: 'results.tmpl',
output: 'results.ct2',
command: [intermediate_ctpp2c, ctpp2c, '@INPUT@', '@OUTPUT@']
)
lib_resources = custom_target('resources', lib_resources = custom_target('resources',
input: 'resources_list.txt', input: 'resources_list.txt',
output: ['kiwixlib-resources.cpp', 'kiwixlib-resources.h'], output: ['kiwixlib-resources.cpp', 'kiwixlib-resources.h'],
command:[res_compiler, '--cxxfile', '@OUTPUT0@', '--hfile', '@OUTPUT1@', '@INPUT@'] command:[res_compiler,
'--cxxfile', '@OUTPUT0@',
'--hfile', '@OUTPUT1@',
'--source_dir', '@OUTDIR@',
'@INPUT@'],
depends: [search_result_template]
) )

Binary file not shown.

127
static/results.tmpl Normal file
View File

@ -0,0 +1,127 @@
<!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>

View File

@ -8,22 +8,22 @@ ARCHIVE_NAME=deps_${PLATFORM}_${REPO_NAME}.tar.gz
# Packages. # Packages.
case ${PLATFORM} in case ${PLATFORM} in
"native_static") "native_static")
PACKAGES="gcc cmake libbz2-dev ccache zlib1g-dev uuid-dev libctpp2-dev" PACKAGES="gcc cmake libbz2-dev ccache zlib1g-dev uuid-dev libctpp2-dev ctpp2-utils"
;; ;;
"native_dyn") "native_dyn")
PACKAGES="gcc cmake libbz2-dev ccache zlib1g-dev uuid-dev libctpp2-dev libmicrohttpd-dev" PACKAGES="gcc cmake libbz2-dev ccache zlib1g-dev uuid-dev libctpp2-dev ctpp2-utils libmicrohttpd-dev"
;; ;;
"win32_static") "win32_static")
PACKAGES="g++-mingw-w64-i686 gcc-mingw-w64-i686 gcc-mingw-w64-base mingw-w64-tools ccache" PACKAGES="g++-mingw-w64-i686 gcc-mingw-w64-i686 gcc-mingw-w64-base mingw-w64-tools ccache ctpp2-utils"
;; ;;
"win32_dyn") "win32_dyn")
PACKAGES="g++-mingw-w64-i686 gcc-mingw-w64-i686 gcc-mingw-w64-base mingw-w64-tools ccache" PACKAGES="g++-mingw-w64-i686 gcc-mingw-w64-i686 gcc-mingw-w64-base mingw-w64-tools ccache ctpp2-utils"
;; ;;
"android_arm") "android_arm")
PACKAGES="gcc cmake ccache" PACKAGES="gcc cmake ccache ctpp2-utils"
;; ;;
"android_arm64") "android_arm64")
PACKAGES="gcc cmake ccache" PACKAGES="gcc cmake ccache ctpp2-utils"
;; ;;
esac esac