mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Xomw: Move Mw_parse classes into separate project
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
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.mediawiki.includes.parsers.doubleunders; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
public class Xomw_doubleunder_data {
|
||||
// XO.MW: MW stores these as mDoubleUnderscores in Parser
|
||||
public boolean toc;
|
||||
public boolean no_toc;
|
||||
public boolean force_toc;
|
||||
|
||||
public boolean no_gallery;
|
||||
public boolean force_gallery;
|
||||
|
||||
public boolean no_title_convert;
|
||||
public boolean no_content_convert;
|
||||
|
||||
public boolean no_edit_section;
|
||||
public boolean new_section_link;
|
||||
|
||||
public boolean static_redirect;
|
||||
|
||||
public boolean hidden_cat;
|
||||
|
||||
public boolean index;
|
||||
public boolean no_index;
|
||||
|
||||
// XO.MW: MW stores these as member variables in Parser
|
||||
public boolean show_toc;
|
||||
public boolean force_toc_position;
|
||||
|
||||
public void Reset() {
|
||||
toc = no_toc = force_toc =
|
||||
no_gallery = force_gallery =
|
||||
no_title_convert = no_content_convert =
|
||||
no_edit_section = new_section_link =
|
||||
static_redirect =
|
||||
hidden_cat = index = no_index =
|
||||
false;
|
||||
|
||||
show_toc = force_toc_position = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
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.mediawiki.includes.parsers.doubleunders; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
import gplx.core.btries.*;
|
||||
import gplx.xowa.langs.*; import gplx.xowa.langs.kwds.*;
|
||||
public class Xomw_doubleunder_wkr {
|
||||
private final Btrie_slim_mgr trie = Btrie_slim_mgr.ci_u8();
|
||||
private final Btrie_rv trv = new Btrie_rv();
|
||||
private Xomw_doubleunder_data data;
|
||||
public void Init_by_wiki(Xomw_doubleunder_data data, Xol_lang_itm lang) {
|
||||
this.data = data;
|
||||
Reg(trie, lang.Kwd_mgr()
|
||||
, Xol_kwd_grp_.Id_notoc
|
||||
, Xol_kwd_grp_.Id_nogallery
|
||||
, Xol_kwd_grp_.Id_forcetoc
|
||||
, Xol_kwd_grp_.Id_toc
|
||||
, Xol_kwd_grp_.Id_noeditsection
|
||||
, Xol_kwd_grp_.Id_newsectionlink
|
||||
, Xol_kwd_grp_.Id_hiddencat
|
||||
, Xol_kwd_grp_.Id_index
|
||||
, Xol_kwd_grp_.Id_noindex
|
||||
, Xol_kwd_grp_.Id_staticredirect
|
||||
, Xol_kwd_grp_.Id_notitleconvert
|
||||
, Xol_kwd_grp_.Id_nocontentconvert
|
||||
);
|
||||
}
|
||||
public void Do_double_underscore(Xomw_parser_ctx pctx, Xomw_parser_bfr pbfr) {
|
||||
// XO.PBFR
|
||||
Bry_bfr src_bfr = pbfr.Src();
|
||||
byte[] src = src_bfr.Bfr();
|
||||
int src_bgn = 0;
|
||||
int src_end = src_bfr.Len();
|
||||
Bry_bfr bfr = pbfr.Trg();
|
||||
|
||||
data.Reset();
|
||||
|
||||
// XO.MW: MW does TOC before others; XO does it at the same time
|
||||
// Now match and remove the rest of them
|
||||
// XO.MW.BGN: $this->mDoubleUnderscores = $mwa->matchAndRemove( $text );
|
||||
int cur = src_bgn;
|
||||
int prv = cur;
|
||||
boolean dirty = false;
|
||||
while (true) {
|
||||
// reached end; stop
|
||||
if (cur == src_end) {
|
||||
if (dirty) {
|
||||
bfr.Add_mid(src, prv, src_end);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// no match; keep searching
|
||||
byte b = src[cur];
|
||||
Object o = trie.Match_at_w_b0(trv, b, src, cur, src_end);
|
||||
if (o == null) {
|
||||
cur += gplx.core.intls.Utf8_.Len_of_char_by_1st_byte(b);
|
||||
continue;
|
||||
}
|
||||
|
||||
// if cs, ensure exact-match (trie is case-insensitive)
|
||||
int kwd_end = trv.Pos();
|
||||
Xomw_doubleunder_itm itm = (Xomw_doubleunder_itm)o;
|
||||
if (itm.case_match && !Bry_.Match(src, cur, kwd_end, itm.val)) {
|
||||
cur = kwd_end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// match; replace __KWD__ with "" (or "<!--MWTOC-->" if __TOC__)
|
||||
dirty = true;
|
||||
bfr.Add_mid(src, prv, cur);
|
||||
switch (itm.tid) {
|
||||
case Xol_kwd_grp_.Id_toc:
|
||||
// The position of __TOC__ needs to be recorded
|
||||
boolean already_seen = !data.show_toc;
|
||||
data.toc = true;
|
||||
data.show_toc = true;
|
||||
data.force_toc_position = true;
|
||||
|
||||
if (already_seen) { // Set a placeholder. At the end we'll fill it in with the TOC.
|
||||
bfr.Add_str_a7("<!--MWTOC-->");
|
||||
}
|
||||
else { // Only keep the first one. XO.MW:ignore by not adding anything to bfr
|
||||
}
|
||||
break;
|
||||
// XO.MW: MW adds boolean to hash_table; XO uses boolean props; note that "remove" is done by not adding to bfr
|
||||
case Xol_kwd_grp_.Id_notoc: data.no_toc = true; break;
|
||||
case Xol_kwd_grp_.Id_nogallery: data.no_gallery = true; break;
|
||||
case Xol_kwd_grp_.Id_forcetoc: data.force_toc = true; break;
|
||||
case Xol_kwd_grp_.Id_noeditsection: data.no_edit_section = true; break;
|
||||
case Xol_kwd_grp_.Id_newsectionlink: data.new_section_link = true; break;
|
||||
case Xol_kwd_grp_.Id_hiddencat: data.hidden_cat = true; break;
|
||||
case Xol_kwd_grp_.Id_index: data.index = true; break;
|
||||
case Xol_kwd_grp_.Id_noindex: data.no_index = true; break;
|
||||
case Xol_kwd_grp_.Id_staticredirect: data.static_redirect = true; break;
|
||||
case Xol_kwd_grp_.Id_notitleconvert: data.no_title_convert = true; break;
|
||||
case Xol_kwd_grp_.Id_nocontentconvert: data.no_content_convert = true; break;
|
||||
default: throw Err_.new_unhandled_default(itm.tid);
|
||||
}
|
||||
cur = kwd_end;
|
||||
prv = cur;
|
||||
}
|
||||
// XO.MW.END: $this->mDoubleUnderscores = $mwa->matchAndRemove( $text );
|
||||
|
||||
if (data.no_toc && !data.force_toc_position) {
|
||||
data.show_toc = false;
|
||||
}
|
||||
|
||||
// XO.MW.EDIT: hidden_cat, index, noindex are used to add to tracking category
|
||||
|
||||
if (dirty)
|
||||
pbfr.Switch();
|
||||
}
|
||||
private static void Reg(Btrie_slim_mgr trie, Xol_kwd_mgr mgr, int... ids) {
|
||||
for (int id : ids) {
|
||||
Xol_kwd_grp grp = mgr.Get_or_new(id);
|
||||
Xol_kwd_itm[] itms = grp.Itms();
|
||||
for (Xol_kwd_itm itm : itms) {
|
||||
byte[] val = itm.Val();
|
||||
trie.Add_obj(val, new Xomw_doubleunder_itm(id, grp.Case_match(), val));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
class Xomw_doubleunder_itm {
|
||||
public int tid;
|
||||
public boolean case_match;
|
||||
public byte[] val;
|
||||
public Xomw_doubleunder_itm(int tid, boolean case_match, byte[] val) {
|
||||
this.tid = tid;
|
||||
this.case_match = case_match;
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
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.mediawiki.includes.parsers.doubleunders; import gplx.*; import gplx.xowa.*; import gplx.xowa.mediawiki.*; import gplx.xowa.mediawiki.includes.*; import gplx.xowa.mediawiki.includes.parsers.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Xomw_doubleunder_wkr__tst {
|
||||
private final Xomw_doubleunder_wkr__fxt fxt = new Xomw_doubleunder_wkr__fxt();
|
||||
@Test public void No_match() {fxt.Test__parse("a b c" , "a b c");}
|
||||
@Test public void Force_toc() {fxt.Test__parse("a __FORCETOC__ b" , "a b").Test__prop_y(fxt.data.force_toc);}
|
||||
@Test public void Toc() {fxt.Test__parse("a __TOC__ b __TOC__ c" , "a <!--MWTOC--> b c").Test__prop_y(fxt.data.toc, fxt.data.show_toc, fxt.data.force_toc_position);}
|
||||
@Test public void Notoc_only() {fxt.Test__parse("a __NOTOC__ b" , "a b").Test__prop_y(fxt.data.no_toc).Test__prop_n(fxt.data.show_toc);} // show_toc is false
|
||||
@Test public void Notoc_w_toc() {fxt.Test__parse("a __TOC__ b __NOTOC__ c" , "a <!--MWTOC--> b c").Test__prop_y(fxt.data.toc, fxt.data.show_toc, fxt.data.force_toc_position);} // show_toc is true
|
||||
@Test public void Case_match() {fxt.Test__parse("a __index__ b" , "a __index__ b");}
|
||||
}
|
||||
class Xomw_doubleunder_wkr__fxt {
|
||||
private final Xomw_parser_ctx pctx = new Xomw_parser_ctx();
|
||||
private final Xomw_parser_bfr pbfr = new Xomw_parser_bfr();
|
||||
private final Xomw_doubleunder_wkr wkr = new Xomw_doubleunder_wkr();
|
||||
public Xomw_doubleunder_data data = new Xomw_doubleunder_data();
|
||||
public Xomw_doubleunder_wkr__fxt() {
|
||||
Xoae_app app = Xoa_app_fxt.Make__app__edit();
|
||||
Xowe_wiki wiki = Xoa_app_fxt.Make__wiki__edit(app);
|
||||
wkr.Init_by_wiki(data, wiki.Lang());
|
||||
}
|
||||
public Xomw_doubleunder_wkr__fxt Test__parse(String src_str, String expd) {
|
||||
byte[] src_bry = Bry_.new_u8(src_str);
|
||||
wkr.Do_double_underscore(pctx, pbfr.Init(src_bry));
|
||||
Gftest.Eq__str(expd, pbfr.Rslt().To_str_and_clear(), src_str);
|
||||
return this;
|
||||
}
|
||||
public Xomw_doubleunder_wkr__fxt Test__prop_y(boolean... ary) {return Test__prop(Bool_.Y, ary);}
|
||||
public Xomw_doubleunder_wkr__fxt Test__prop_n(boolean... ary) {return Test__prop(Bool_.N, ary);}
|
||||
private Xomw_doubleunder_wkr__fxt Test__prop(boolean expd, boolean... ary) {
|
||||
for (boolean v : ary)
|
||||
Gftest.Eq__bool(expd, v);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user