Skip to content

Commit

Permalink
fix meta-search; new custom gettext extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelloPerathoner committed Sep 12, 2024
1 parent 524c6df commit c6f097e
Show file tree
Hide file tree
Showing 52 changed files with 845 additions and 478 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ black:
-black server/ python/


TARGETS = csslint jslint phplint mo po pot
TARGETS = csslint jslint phplint mo po pot poedit

define TARGETS_TEMPLATE

Expand Down
184 changes: 184 additions & 0 deletions docs/cap-gettext-extractor.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import { GettextExtractor, JsExtractors, HtmlExtractors } from 'gettext-extractor';
import sfc from '@vue/compiler-sfc';
import { glob } from 'glob';
import { readFileSync } from 'node:fs';
import { ArgumentParser } from 'argparse';
import sourceMap from 'source-map';
import _ from 'lodash-es';

const sourceMaps = {};

const JSExtractor = new GettextExtractor();
const VueExtractor = new GettextExtractor();

/**
* Extractor for JS files and <script> sections of Vue files.
*/
const jsParser = JSExtractor.createJsParser([
JsExtractors.callExpression(['wp.i18n.__', '[this].$t', 'vm.$t'], {
arguments: {
text: 0,
}
}),
JsExtractors.callExpression(['wp.i18n._x', '[this].$x', 'vm.$x'], {
arguments: {
text: 0,
context: 1,
}
}),
JsExtractors.callExpression(['wp.i18n._n', '[this].$n', 'vm.$n'], {
arguments: {
text: 0,
textPlural: 1,
context: 3
}
})
]);

/**
* Extractor for a compiled Vue template.
*/
const vueParser = VueExtractor.createJsParser([
JsExtractors.callExpression(['_ctx.$t'], {
arguments: {
text: 0,
context: 1
}
}),
JsExtractors.callExpression(['_ctx.$n'], {
arguments: {
text: 0,
textPlural: 1,
context: 2
}
})
]);

/**
* Extractor for a HTML Vue template.
*/
const htmlParser = JSExtractor.createHtmlParser([
HtmlExtractors.elementContent('*[v-translate]', {})
]);


function jsParseString (code, file, line) {
jsParser.parseString (
code,
file,
{ lineNumberStart: line }
)
}

async function main (args) {

for (const glb of args.inputs) {
const filenames = glob.sync (glb).sort ();

for (const filename of filenames) {
if (args.verbose)
console.log(`Scanning: ${filename}`);

const file = readFileSync (filename, { 'encoding' : 'utf-8' });

if (filename.endsWith ('.vue')) {
const { descriptor } = sfc.parse (file, { filename });

if (descriptor.template && descriptor.template.content) {
const template = descriptor.template;
// if (filename.endsWith ('selector.vue')) {
// console.dir(template, { depth: 4 });
// exit ();
// }

// parse the template in HTML
htmlParser.parseString (
template.loc.source,
filename,
{
lineNumberStart: template.loc.start.line,
trimWhiteSpace: true,
}
);

// compile the template into JS
const compiled = sfc.compileTemplate ({
source: template.loc.source,
filename,
id: filename,
inMap: template.map,
});

// if (filename.endsWith ('selector.vue')) {
// console.log(compiled.code);
// exit();
// }

vueParser.parseString (
compiled.code,
filename,
// FIXME: it should be made possible to pass a sourceMap into this
{ lineNumberStart: 1 }
)

// build a map that actually works because
// SourceMapConsumer.originalPositionFor(generatedPosition) surely
// does not!
new sourceMap.SourceMapConsumer(compiled.map).eachMapping((m) => {
sourceMaps[`${m.source}:${m.generatedLine}`] = `${m.source}:${m.originalLine}`;
// console.log(`${m.source}:${m.generatedLine} => ${m.source}:${m.originalLine}`);
}, this, sourceMap.SourceMapConsumer.ORIGINAL_ORDER);
}

if (descriptor.script && descriptor.script.content) {
// the <script> section of a Vue file
const script = descriptor.script;
jsParseString (
script.loc.source,
filename,
script.loc.start.line,
);
}
} else {
// a plain old JS file
jsParseString (
file,
filename,
0,
);
}
}
}

for (const vueMsg of VueExtractor.getMessages()) {
const msg = _.pick (vueMsg, ['text', 'references', 'comments']);
if (vueMsg.textPlural)
msg.textPlural = vueMsg.textPlural;
if (vueMsg.context)
msg.context = vueMsg.context;
msg.references = vueMsg.references.map ((ref) => {
const sref = sourceMaps[ref];
console.log (ref, sref, msg.text);
return sref || ref;
});
JSExtractor.addMessage (msg);
}

if (args.output == '-')
process.stdout.write (JSExtractor.getPotString ());
else
JSExtractor.savePotFile (args.output);

if (args.verbose)
JSExtractor.printStats ();
}

const parser = new ArgumentParser ({
description: 'Extract translatable strings from js and vue files.'
});

parser.add_argument('-v', '--verbose', { action: 'store_true', help: 'Be verbose' });
parser.add_argument('-o', '--output', { default: '-', help: 'Output to file (default stdout).' });
parser.add_argument('inputs', { nargs: '+', help: 'Input files' });

main (parser.parse_args());
6 changes: 3 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def extension(path):

extension("sphinxcontrib-autojsdoc")
extension("sphinxcontrib-autophpdoc")
extension("sphinxcontrib-pic")
extension("sphinxcontrib-minilang")

# -- General configuration ------------------------------------------------

Expand All @@ -60,7 +60,7 @@ def extension(path):
"sphinxcontrib.phpdomain",
"autophpdoc",
"autojsdoc",
"pic",
"minilang",
]

autojsdoc_structure_json = "build/jsdoc/structure.json"
Expand All @@ -71,7 +71,7 @@ def extension(path):
autophpdoc_members = True
autophpdoc_title = True

pic_options = {
minilang_options = {
"pic": {
"program": ["dpic", "-v"],
"align": "center",
Expand Down
Binary file modified docs/gh-pages/artifact.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions docs/sphinxcontrib-minilang
1 change: 0 additions & 1 deletion docs/sphinxcontrib-pic

This file was deleted.

10 changes: 5 additions & 5 deletions docs/src/_static/my_theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
font-style: italic;
}

.rst-content .pic {
.rst-content .minilang {
margin-bottom: 1em;
}

.rst-content .pic table {
.rst-content .minilang table {
min-width: 100%;
}

.rst-content .pic table th,
.rst-content .pic table td {
.rst-content .minilang table th,
.rst-content .minilang table td {
border: 1px solid black;
padding: 0.2em;
text-align: left;
}

.rst-content .pic-w100 {
.rst-content .minilang-w100 {
width: 100%;
}

Expand Down
1 change: 1 addition & 0 deletions docs/src/maintenance/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Maintenance
.. toctree::
:maxdepth: 2

scripts
logfiles


Expand Down
16 changes: 16 additions & 0 deletions docs/src/maintenance/scripts.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.. _maintenance-scripts:


Scripts
=======

Documentation of the Python scripts.

.. contents::
:local:

.. autoprogram:: scripts.import_data:build_parser()
:prog: import_data.py

.. autoprogram:: scripts.import_solr:build_parser()
:prog: import_solr.py
6 changes: 3 additions & 3 deletions docs/src/overviews/collation_tool.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ version are extracted.

The online collation tool knows about all versions and offers them to the user.

.. pic:: uml
.. minilang:: uml
:caption: Data flow during pre-processing

database "Manuscript files\n(XML+TEI)" as tei
Expand Down Expand Up @@ -66,7 +66,7 @@ The application server retrieves the chapters from the database and collates the
results of the collation are sent in json to the frontend that does the formatting for
display.

.. pic:: uml
.. minilang:: uml
:caption: Data flow during collation

cloud "Backend" {
Expand Down Expand Up @@ -214,7 +214,7 @@ This is sometimes called the

An example calculation follows:

.. pic:: trigram hlodouuico ludouico
.. minilang:: trigram hlodouuico ludouico
:caption: Calculating similarity using trigrams

The similarity based on trigrams was chosen because its calculation can be done in
Expand Down
8 changes: 4 additions & 4 deletions docs/src/overviews/fulltext_search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The command for updating the Solr database is: :command:`make solr-import`.

That same command is run nightly by cron.

.. pic:: uml
.. minilang:: uml
:caption: Data flow during text extraction

database "Manuscript files\n(TEI)" as tei
Expand All @@ -56,7 +56,7 @@ We extract the metadata from the manuscript files and store them in the Postgres
database on the Capitularia VM. The process is similar to the pre-processing
done for the Collation Tool.

.. pic:: uml
.. minilang:: uml
:caption: Data flow during metadata extraction

database "Manuscript files\n(TEI)" as tei
Expand Down Expand Up @@ -116,7 +116,7 @@ Lucene <https://github.com/cceh/capitularia-lucene-tools>`_.
Searches in Mordek and Wordpress posts use more traditional search methods like
stemming.

.. pic:: uml
.. minilang:: uml
:caption: Components used in searching

component "Frontend\n(Javascript)" as client
Expand All @@ -135,7 +135,7 @@ stemming.
api --> solr


.. pic:: uml
.. minilang:: uml
:caption: Data flow while searching

participant "Frontend" as client
Expand Down
4 changes: 2 additions & 2 deletions docs/src/overviews/html_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugin<file-includer>`.
XSLT Transformations
--------------------

.. pic:: uml
.. minilang:: uml
:caption: Data flow during HTML generation

database "Manuscript files\n(XML+TEI)" as tei
Expand Down Expand Up @@ -45,7 +45,7 @@ The HTML files are stored in the cache directory.
User Delivery
-------------

.. pic:: uml
.. minilang:: uml
:align: center
:caption: Data flow during user access

Expand Down
2 changes: 1 addition & 1 deletion docs/src/overviews/makefile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cron runs as the :ref:`user capitularia <user>`.
- :ref:`fulltext-search-overview`


.. pic:: uml
.. minilang:: uml
:caption: Makefile Overview
:svg-width: 100%

Expand Down
4 changes: 2 additions & 2 deletions docs/src/vm/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ indicate the same concept.

.. Palette https://github.com/d3/d3-scale-chromatic/blob/master/src/categorical/Paired.js
.. pic:: sauml -i manuscripts -i msparts -i capitularies -i chapters -i mss_capitularies
.. minilang:: sauml -i manuscripts -i msparts -i capitularies -i chapters -i mss_capitularies
-i mss_chapters -i mss_chapters_text
:caption: Schema *capitularia*
:align: center
Expand All @@ -49,7 +49,7 @@ place names, which is the one displayed in the manuscript search box. This hier
extracted from the file :file:`mss/lists/capitularia_geo.xml`. Unfortunately it is
linked to manuscripts and not to manuscript parts.

.. pic:: sauml -s gis
.. minilang:: sauml -s gis
:caption: Schema *gis*
:align: center
:svg-width: 100%
Expand Down
2 changes: 1 addition & 1 deletion docs/src/vm/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ We wrote a :ref:`Wordpress theme <wordpress-theme>` and many :ref:`Wordpress plu
application server (and its API at https://api.capitularia.uni-koeln.de) for all
functionality that is too inconvenient to implement in Wordpress plugins.

.. pic:: pic
.. minilang:: pic
:caption: Main Components of the Capitularia VM

down
Expand Down
Loading

0 comments on commit c6f097e

Please sign in to comment.