mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Embeddable: Add page loader
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
|
||||
public interface Xop_mediawiki_loader {
|
||||
String LoadWikitext(String page);
|
||||
}
|
||||
@@ -32,8 +32,9 @@ public class Xop_mediawiki_mgr {
|
||||
, gplx.xowa.apps.boots.Xoa_cmd_arg_mgr.Bin_dir_name()
|
||||
);
|
||||
}
|
||||
public Xop_mediawiki_wkr Make(String domain_str) {
|
||||
public Xop_mediawiki_wkr Make(String domain_str) {return Make(domain_str, null);}
|
||||
public Xop_mediawiki_wkr Make(String domain_str, Xop_mediawiki_loader loader) {
|
||||
Xowe_wiki wiki = (Xowe_wiki)app.Wiki_mgr().Make(Bry_.new_u8(domain_str), app.Fsys_mgr().Wiki_dir());
|
||||
return new Xop_mediawiki_wkr(wiki);
|
||||
return new Xop_mediawiki_wkr(wiki, loader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
|
||||
import gplx.xowa.wikis.*; import gplx.xowa.parsers.*; import gplx.xowa.wikis.pages.*; import gplx.xowa.htmls.core.htmls.*;
|
||||
import gplx.xowa.wikis.caches.*;
|
||||
public class Xop_mediawiki_wkr {
|
||||
private final Xowe_wiki wiki;
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
public Xop_mediawiki_wkr(Xowe_wiki wiki) {
|
||||
public Xop_mediawiki_wkr(Xowe_wiki wiki, Xop_mediawiki_loader loader) {
|
||||
this.wiki = wiki;
|
||||
this.Loader_(loader);
|
||||
}
|
||||
public void Loader_(Xop_mediawiki_loader loader) {
|
||||
if (loader != null)
|
||||
wiki.Cache_mgr().Page_cache().Load_wkr_(new Xow_page_cache_wkr__embeddable(loader));
|
||||
}
|
||||
public String Parse(String page, String wikitext) {
|
||||
Xoa_ttl ttl = wiki.Ttl_parse(Bry_.new_u8(page));
|
||||
@@ -37,7 +43,6 @@ public class Xop_mediawiki_wkr {
|
||||
pctx.Clear_all();
|
||||
parser_mgr.Parse(wpg, true);
|
||||
|
||||
|
||||
// write to html
|
||||
boolean is_wikitext = Xow_page_tid.Identify(wpg.Wiki().Domain_tid(), ttl.Ns().Id(), ttl.Page_db()) == Xow_page_tid.Tid_wikitext;
|
||||
byte[] orig_bry = Bry_.Empty;
|
||||
|
||||
@@ -20,20 +20,33 @@ import org.junit.*; import gplx.core.tests.*;
|
||||
public class Xop_mediawiki_wkr__tst {
|
||||
private final Xop_mediawiki_wkr__fxt fxt = new Xop_mediawiki_wkr__fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Init__wkr("en.wikipedia.org");
|
||||
fxt.Init__wkr("en.wikipedia.org", null);
|
||||
fxt.Test__parse("Page_1", "''{{PAGENAME}}''"
|
||||
, "<p><i>Page 1</i>"
|
||||
, "</p>"
|
||||
);
|
||||
}
|
||||
@Test public void Template() {
|
||||
fxt.Init__wkr("en.wikipedia.org", new Xop_mediawiki_loader__mock());
|
||||
fxt.Test__parse("Page_1", "{{bold}}"
|
||||
, "<p><b>bold</b>"
|
||||
, "</p>"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Xop_mediawiki_wkr__fxt {
|
||||
private final Xop_mediawiki_mgr mgr = new Xop_mediawiki_mgr("mem/xowa/wiki/en.wikipedia.org/");
|
||||
private Xop_mediawiki_wkr wkr;
|
||||
public void Init__wkr(String wiki) {
|
||||
this.wkr = mgr.Make(wiki);
|
||||
public void Init__wkr(String wiki, Xop_mediawiki_loader cbk) {
|
||||
this.wkr = mgr.Make(wiki, cbk);
|
||||
}
|
||||
public void Test__parse(String page, String wtxt, String... expd) {
|
||||
Gftest.Eq__ary__lines(String_.Concat_lines_nl_skip_last(expd), wkr.Parse(page, wtxt), "parse failed; wtxt={0}", wtxt);
|
||||
}
|
||||
}
|
||||
class Xop_mediawiki_loader__mock implements Xop_mediawiki_loader {
|
||||
public String LoadWikitext(String page) {
|
||||
if (String_.Eq(page, "Template:Bold")) return "'''bold'''";
|
||||
else return "text";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.addons.parsers.mediawikis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.parsers.*;
|
||||
class Xow_page_cache_wkr__embeddable implements gplx.xowa.wikis.caches.Xow_page_cache_wkr {
|
||||
private final Xop_mediawiki_loader cbk;
|
||||
public Xow_page_cache_wkr__embeddable(Xop_mediawiki_loader cbk) {
|
||||
this.cbk = cbk;
|
||||
}
|
||||
public byte[] Get_page_or_null(byte[] full_db) {
|
||||
return Bry_.new_u8(cbk.LoadWikitext(String_.new_u8(full_db)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user