mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
Embeddable: Add basic parse wikitext function
This commit is contained in:
parent
f04cb73679
commit
a056a5b2e6
@ -0,0 +1,38 @@
|
||||
/*
|
||||
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 class Xop_mediawiki_mgr {
|
||||
private final Xoae_app app;
|
||||
public Xop_mediawiki_mgr(Io_url root_dir) {
|
||||
Gfo_usr_dlg usr_dlg = Xoa_app_.New__usr_dlg__console();
|
||||
Gfo_usr_dlg_.Instance = usr_dlg;
|
||||
|
||||
this.app = new Xoae_app(usr_dlg, gplx.xowa.apps.Xoa_app_mode.Itm_cmd
|
||||
, root_dir
|
||||
, root_dir.GenSubDir("wiki")
|
||||
, root_dir.GenSubDir("file")
|
||||
, root_dir.GenSubDir("user")
|
||||
, root_dir.GenSubDir_nest("user", "anonymous", "wiki")
|
||||
, gplx.xowa.apps.boots.Xoa_cmd_arg_mgr.Bin_dir_name()
|
||||
);
|
||||
}
|
||||
public Xop_mediawiki_wkr Make(String domain_str) {
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
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.*;
|
||||
import gplx.xowa.wikis.*; import gplx.xowa.parsers.*; import gplx.xowa.wikis.pages.*; import gplx.xowa.htmls.core.htmls.*;
|
||||
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) {
|
||||
this.wiki = wiki;
|
||||
}
|
||||
public String Parse(String page, String wikitext) {
|
||||
Xoa_ttl ttl = wiki.Ttl_parse(Bry_.new_u8(page));
|
||||
|
||||
byte[] wtxt = Bry_.new_u8(wikitext);
|
||||
Xoae_page wpg = Xoae_page.New(wiki, ttl);
|
||||
wpg.Db().Text().Text_bry_(wtxt);
|
||||
|
||||
Xow_parser_mgr parser_mgr = wiki.Parser_mgr();
|
||||
|
||||
// parse page
|
||||
Xop_ctx pctx = parser_mgr.Ctx();
|
||||
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;
|
||||
if (is_wikitext) {
|
||||
wiki.Html_mgr().Page_wtr_mgr().Wkr(Xopg_page_.Tid_read).Write_hdump(tmp_bfr, pctx, Xoh_wtr_ctx.Hdump, wpg);
|
||||
orig_bry = tmp_bfr.To_bry_and_clear();
|
||||
wpg.Db().Html().Html_bry_(orig_bry);
|
||||
}
|
||||
else { // not wikitext; EX: pages in MediaWiki: ns; DATE:2016-09-12
|
||||
wpg.Db().Html().Html_bry_(wpg.Db().Text().Text_bry());
|
||||
}
|
||||
|
||||
return String_.new_u8(orig_bry);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
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.*;
|
||||
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.Test__parse("Page_1", "''{{PAGENAME}}''"
|
||||
, "<p><i>Page 1</i>"
|
||||
, "</p>"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Xop_mediawiki_wkr__fxt {
|
||||
private final Xop_mediawiki_mgr mgr = new Xop_mediawiki_mgr(Io_url_.new_dir_("mem/xowa/wiki/en.wikipedia.org"));
|
||||
private Xop_mediawiki_wkr wkr;
|
||||
public void Init__wkr(String wiki) {
|
||||
this.wkr = mgr.Make(wiki);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
@ -87,7 +87,7 @@ public class Xoa_cmd_arg_mgr {
|
||||
return new Xoa_cmd_arg_mgr(arg_mgr);
|
||||
}
|
||||
private static final String User_name_default = gplx.xowa.users.Xoue_user.Key_xowa_user;
|
||||
private static String Bin_dir_name() {
|
||||
public static String Bin_dir_name() {
|
||||
String rv = "";
|
||||
Op_sys op_sys = Op_sys.Cur();
|
||||
switch (op_sys.Tid()) {
|
||||
|
Loading…
Reference in New Issue
Block a user