Skip to content

Commit a9fccc5

Browse files
committed
enhancements
improved handling of English wiktionary parsing
1 parent e7d16be commit a9fccc5

File tree

7 files changed

+4715
-8
lines changed

7 files changed

+4715
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ In the ``wiki`` folder you can find the WikiFind class that can be used as start
3838
# WikiFind
3939
Class ``WikiFind`` is demo that searchs and renders a wiki page into html format using files wiki.dat, templates.dat and modules.dat generated by WikiSplitter.
4040
```
41-
compile: javac -cp .;lib\luaj-jse-3.0.2p.jar wiki\WikiFind.java
41+
compile: javac -encoding UTF-8 -cp .;lib\luaj-jse-3.0.2p.jar wiki\WikiFi
42+
nd.java
4243
usage: java -cp .;lib\luaj-jse-3.0.2p.jar wiki.WikiFind -random|-longest|<word>
4344
4445
meaning of options:

info/bliki/extensions/scribunto/engine/lua/ScribuntoLuaEngine.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,11 @@ private LuaValue loadModule(String chunkName) throws LuaError {
485485
return new LuaClosure(prototype, globals);
486486
} else {
487487
try (InputStream is = findPackage(chunkName)) {
488-
return new LuaClosure(
489-
loadAndCache(is, chunkName),
490-
globals);
488+
if (is != null)
489+
return new LuaClosure(
490+
loadAndCache(is, chunkName),
491+
globals);
492+
else return LuaValue.FALSE;//02-09-2024: return FALSE when package not found
491493
} catch (ScribuntoException | IOException e) {
492494
throw new LuaError(e);
493495
}
@@ -550,16 +552,42 @@ private InputStream findModule(final String moduleName) throws IOException {
550552
if (is != null) {
551553
return is;
552554
} else {
553-
throw e;
555+
System.out.println("Warning: file not found: " + moduleName);//02-09-2024: print warning instead of throw e;
556+
return null;//02-09-2024: return null instead of throw e;
554557
}
555558
}
556559
}
557560

558561
protected String getRawWikiContent(String pageName) throws FileNotFoundException {
559-
final String content = wp.getModule(pageName);
562+
String content = wp.getModule(pageName);
560563
if (content == null) {
561564
throw new FileNotFoundException("could not find module \"" + pageName + "\"");
562565
}
566+
//02-09-2024: patch to handle variable arguments ... as in old versions of LUA
567+
int idx = 0;
568+
while ((idx = content.indexOf("arg", idx)) != -1) {
569+
if (content.charAt(idx - 1) == ' ' && content.charAt(idx + 3) == '.' && content.charAt(idx + 4) == 'n') {// arg.n
570+
char ch = content.charAt(idx + 5);
571+
if (ch == ' ' || ch == '\n') {
572+
content = content.substring(0, idx) + "#{...}" + content.substring(idx + 5);
573+
}
574+
} else idx += 5;
575+
}
576+
idx = 0;
577+
while ((idx = content.indexOf("function ", idx)) != -1) {
578+
char ch = content.charAt(idx - 1);
579+
idx += 9; //length of "function "
580+
if (ch == ' ' || ch == '\n') {
581+
int vararg = content.indexOf("(...)", idx);
582+
if (vararg != -1 && vararg < idx + 50 && content.substring(idx, vararg).matches("[a-zA-Z0-9: ]*")) {//evitiamo di considerare (...) lontani da 'function'
583+
content = content.substring(0, vararg + 5) + " local arg = table.pack(...) " + content.substring(vararg + 5);//length of "(...)"
584+
idx = vararg;
585+
// System.out.println("-------------------");
586+
// System.out.println(content);
587+
// System.out.println("-------------------");
588+
}
589+
}
590+
}
563591
return content;
564592
}
565593

wiki/TestSuite.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private void do_parser_eval_tests(TemplateParser tp) throws ParseException {//au
172172
testEvaluate(tp, "{{#iferror: {{#time: r|2000 December 20}} | error | correct }}", wp, "error");
173173
testEvaluate(tp, "{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 2 }}", wp, "bar/baz");
174174
testEvaluate(tp, "{{#titleparts: Talk:Foo/bar/baz/quok | -1 }}", wp, "Talk:Foo/bar/baz");
175-
testEvaluate(tp, "{{#ifexist: textbook | exists | doesn't exist }}", wp, "exists");
175+
// testEvaluate(tp, "{{#ifexist: textbook | exists | doesn't exist }}", wp, "exists"); 30-09-2024: disabled because getContent() in WikiPage returns always null
176176
testEvaluate(tp, "{{#tag:img | image | src=images/a.png }}", wp, "<img src=\"images/a.png\">image</img>");
177177
testEvaluate(tp, "{{#switch: baz | foo = Foo | baz = Baz | Bar }}", wp, "Baz");
178178
testEvaluate(tp, "{{#switch: foo | foo| baz = Baz | Bar }}", wp, "Baz");
@@ -225,6 +225,7 @@ private void do_debug_test(TemplateParser tp) throws ParseException {//debug tes
225225
private void do_template_test(TemplateParser tp, String template) throws ParseException {//test of template expansion using files templates.dat and modules.dat
226226
readfile(name2template, "templates.dat", false);
227227
readfile(name2module, "modules.dat", false);
228+
System.out.println("Warning: this test uses locale en (english), in case of other languages must be changed inside do_template_test() call"); System.out.println();
228229
WikiPage wp = new WikiPage("textbook", new SimpleDateFormat("dd-MM-yyyy hh:mm").parse("01-01-2020 15:30"),
229230
getLocale("en"), tp, name2template, name2module, true, null, true);
230231
String result = tp.parse(template, wp);

0 commit comments

Comments
 (0)