Skip to content

Commit

Permalink
enhancements
Browse files Browse the repository at this point in the history
improved handling of English wiktionary parsing
  • Loading branch information
javalc6 committed Sep 30, 2024
1 parent e7d16be commit a9fccc5
Show file tree
Hide file tree
Showing 7 changed files with 4,715 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ In the ``wiki`` folder you can find the WikiFind class that can be used as start
# WikiFind
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.
```
compile: javac -cp .;lib\luaj-jse-3.0.2p.jar wiki\WikiFind.java
compile: javac -encoding UTF-8 -cp .;lib\luaj-jse-3.0.2p.jar wiki\WikiFi
nd.java
usage: java -cp .;lib\luaj-jse-3.0.2p.jar wiki.WikiFind -random|-longest|<word>
meaning of options:
Expand Down
38 changes: 33 additions & 5 deletions info/bliki/extensions/scribunto/engine/lua/ScribuntoLuaEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,11 @@ private LuaValue loadModule(String chunkName) throws LuaError {
return new LuaClosure(prototype, globals);
} else {
try (InputStream is = findPackage(chunkName)) {
return new LuaClosure(
loadAndCache(is, chunkName),
globals);
if (is != null)
return new LuaClosure(
loadAndCache(is, chunkName),
globals);
else return LuaValue.FALSE;//02-09-2024: return FALSE when package not found
} catch (ScribuntoException | IOException e) {
throw new LuaError(e);
}
Expand Down Expand Up @@ -550,16 +552,42 @@ private InputStream findModule(final String moduleName) throws IOException {
if (is != null) {
return is;
} else {
throw e;
System.out.println("Warning: file not found: " + moduleName);//02-09-2024: print warning instead of throw e;
return null;//02-09-2024: return null instead of throw e;
}
}
}

protected String getRawWikiContent(String pageName) throws FileNotFoundException {
final String content = wp.getModule(pageName);
String content = wp.getModule(pageName);
if (content == null) {
throw new FileNotFoundException("could not find module \"" + pageName + "\"");
}
//02-09-2024: patch to handle variable arguments ... as in old versions of LUA
int idx = 0;
while ((idx = content.indexOf("arg", idx)) != -1) {
if (content.charAt(idx - 1) == ' ' && content.charAt(idx + 3) == '.' && content.charAt(idx + 4) == 'n') {// arg.n
char ch = content.charAt(idx + 5);
if (ch == ' ' || ch == '\n') {
content = content.substring(0, idx) + "#{...}" + content.substring(idx + 5);
}
} else idx += 5;
}
idx = 0;
while ((idx = content.indexOf("function ", idx)) != -1) {
char ch = content.charAt(idx - 1);
idx += 9; //length of "function "
if (ch == ' ' || ch == '\n') {
int vararg = content.indexOf("(...)", idx);
if (vararg != -1 && vararg < idx + 50 && content.substring(idx, vararg).matches("[a-zA-Z0-9: ]*")) {//evitiamo di considerare (...) lontani da 'function'
content = content.substring(0, vararg + 5) + " local arg = table.pack(...) " + content.substring(vararg + 5);//length of "(...)"
idx = vararg;
// System.out.println("-------------------");
// System.out.println(content);
// System.out.println("-------------------");
}
}
}
return content;
}

Expand Down
3 changes: 2 additions & 1 deletion wiki/TestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void do_parser_eval_tests(TemplateParser tp) throws ParseException {//au
testEvaluate(tp, "{{#iferror: {{#time: r|2000 December 20}} | error | correct }}", wp, "error");
testEvaluate(tp, "{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 2 }}", wp, "bar/baz");
testEvaluate(tp, "{{#titleparts: Talk:Foo/bar/baz/quok | -1 }}", wp, "Talk:Foo/bar/baz");
testEvaluate(tp, "{{#ifexist: textbook | exists | doesn't exist }}", wp, "exists");
// testEvaluate(tp, "{{#ifexist: textbook | exists | doesn't exist }}", wp, "exists"); 30-09-2024: disabled because getContent() in WikiPage returns always null
testEvaluate(tp, "{{#tag:img | image | src=images/a.png }}", wp, "<img src=\"images/a.png\">image</img>");
testEvaluate(tp, "{{#switch: baz | foo = Foo | baz = Baz | Bar }}", wp, "Baz");
testEvaluate(tp, "{{#switch: foo | foo| baz = Baz | Bar }}", wp, "Baz");
Expand Down Expand Up @@ -225,6 +225,7 @@ private void do_debug_test(TemplateParser tp) throws ParseException {//debug tes
private void do_template_test(TemplateParser tp, String template) throws ParseException {//test of template expansion using files templates.dat and modules.dat
readfile(name2template, "templates.dat", false);
readfile(name2module, "modules.dat", false);
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();
WikiPage wp = new WikiPage("textbook", new SimpleDateFormat("dd-MM-yyyy hh:mm").parse("01-01-2020 15:30"),
getLocale("en"), tp, name2template, name2module, true, null, true);
String result = tp.parse(template, wp);
Expand Down
Loading

0 comments on commit a9fccc5

Please sign in to comment.