1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Source: Restore broken commit

This commit is contained in:
gnosygnu
2017-02-06 22:14:55 -05:00
parent 938beac9f9
commit 3bfeb94b43
4380 changed files with 328018 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.tmpls.*; import gplx.xowa.parsers.hdrs.*;
import gplx.xowa.wikis.nss.*; import gplx.xowa.wikis.pages.wtxts.*;
public class Lst_pfunc_itm {
public Lst_pfunc_itm(byte[] itm_src, Lst_section_nde_mgr sec_mgr, Xopg_toc_mgr toc_mgr) {
this.itm_src = itm_src; this.sec_mgr = sec_mgr; this.toc_mgr = toc_mgr;
}
public byte[] Itm_src() {return itm_src;} private final byte[] itm_src;
public Lst_section_nde_mgr Sec_mgr() {return sec_mgr;} private final Lst_section_nde_mgr sec_mgr;
public Xopg_toc_mgr Toc_mgr() {return toc_mgr;} private final Xopg_toc_mgr toc_mgr;
public static Lst_pfunc_itm New_sect_or_null(Xop_ctx ctx, byte[] ttl_bry) {
// init wiki, ttl
Xowe_wiki wiki = ctx.Wiki();
Xoa_ttl ttl = wiki.Ttl_parse(ttl_bry); if (ttl == null) return null; // EX:{{#lst:<>}} -> ""
// get from cache
Lst_pfunc_itm rv = (Lst_pfunc_itm)wiki.Cache_mgr().Lst_cache().Get_by_bry(ttl_bry);
if (rv == null) { // cache transclusions to prevent multiple parsings; DATE:2014-02-22
// get sub_src;
Xop_ctx sub_ctx = Xop_ctx.New__top(wiki).Ref_ignore_(true);
sub_ctx.Page().Ttl_(ctx.Page().Ttl()); // NOTE: must set ttl on page, else test fails;
byte[] sub_src = wiki.Cache_mgr().Page_cache().Get_or_load_as_src(ttl); if (sub_src == null) return null; // {{#lst:missing}} -> ""
// parse page; note adding to stack to prevent circular recursions
if (!wiki.Parser_mgr().Tmpl_stack_add(ttl.Full_db())) return null;
Xot_defn_tmpl tmpl = wiki.Parser_mgr().Main().Parse_text_to_defn_obj(sub_ctx, sub_ctx.Tkn_mkr(), ttl.Ns(), ttl_bry, sub_src); // NOTE: parse as tmpl to ignore <noinclude>
wiki.Parser_mgr().Tmpl_stack_del(); // take template off stack; evaluate will never recurse, but will fail if ttl is still on stack; DATE:2014-03-10
// eval tmpl
Bry_bfr tmp_bfr = wiki.Utl__bfr_mkr().Get_m001();
tmpl.Tmpl_evaluate(sub_ctx, Xot_invk_temp.Page_is_caller, tmp_bfr);
sub_src = tmp_bfr.To_bry_and_rls();
// parse again
if (!wiki.Parser_mgr().Tmpl_stack_add(ttl.Full_db())) return null; // put template back on stack;
sub_ctx.Page().Wtxt().Toc().Clear(); // HACK: must clear toc hdrs; should probably create a new top sub_ctx; DATE:2016-08-17
Xop_root_tkn root = wiki.Parser_mgr().Main().Parse_text_to_wdom(sub_ctx, sub_src, true); // NOTE: pass sub_ctx as old_ctx b/c entire document will be parsed, and references outside the section should be ignored;
wiki.Parser_mgr().Tmpl_stack_del();
sub_src = root.Data_mid(); // NOTE: must set src to root.Data_mid() which is result of parse; else <nowiki> will break text; DATE:2013-07-11
// add to cache
rv = new Lst_pfunc_itm(sub_src, Clone(sub_ctx.Lst_section_mgr()), Clone(sub_ctx.Page().Wtxt().Toc(), sub_src, ttl_bry));
wiki.Cache_mgr().Lst_cache().Add(ttl_bry, rv);
}
return rv;
}
public static Lst_pfunc_itm New_hdr_or_null(Xop_ctx ctx, byte[] ttl_bry) {
// init wiki, ttl
Xowe_wiki wiki = ctx.Wiki();
Xoa_ttl ttl = wiki.Ttl_parse(ttl_bry); if (ttl == null) return null; // EX:{{#lst:<>}} -> ""
// get from cache
Lst_pfunc_itm rv = (Lst_pfunc_itm)wiki.Cache_mgr().Lst_cache().Get_by_bry(ttl_bry);
if (rv == null) { // cache transclusions to prevent multiple parsings; DATE:2014-02-22
// get sub_ctx: note new ctx is needed b/c sub_page objects must not get added to owner_page; for example, references / hdrs / lnki.files
Xop_ctx sub_ctx = Xop_ctx.New__top(wiki).Ref_ignore_(true);
sub_ctx.Page().Ttl_(ctx.Page().Ttl()); // NOTE: must set ttl on page, else test fails;
byte[] sub_src = wiki.Cache_mgr().Page_cache().Get_or_load_as_src(ttl); if (sub_src == null) return null; // {{#lst:missing}} -> ""
// parse sub_src; note adding to page's stack to prevent circular recursions
if (!wiki.Parser_mgr().Tmpl_stack_add(ttl.Full_db())) return null;
Xop_root_tkn root = wiki.Parser_mgr().Main().Parse_text_to_wdom(sub_ctx, sub_src, true); // NOTE: parse as defn will drop <onlyinclude>; PAGE:en.w:10s_BC; DATE:2016-08-13
wiki.Parser_mgr().Tmpl_stack_del();
// HACK: parse sub_src again b/c transcluded templates will add their hdrs to toc_mgr; PAGE:en.w:Germany_national_football_team DATE:2016-08-13
sub_src = root.Data_mid(); // NOTE: must set src to root.Data_mid() which is result of parse; else <nowiki> will break text; DATE:2013-07-11
sub_ctx.Page().Wtxt().Toc().Clear(); // HACK: must clear toc hdrs; should probably create a new top sub_ctx; DATE:2016-08-17
root = wiki.Parser_mgr().Main().Parse_text_to_wdom(sub_ctx, sub_src, true);
sub_src = root.Data_mid(); // NOTE: must call root.Data_mid() again b/c previous src may have nowiki which will get removed in 2nd pass; see TEST:Tmpl_w_nowiki; DATE:2016-08-13
// add to cache
rv = new Lst_pfunc_itm(sub_src, Clone(sub_ctx.Lst_section_mgr()), Clone(sub_ctx.Page().Wtxt().Toc(), sub_src, ttl_bry));
wiki.Cache_mgr().Lst_cache().Add(ttl_bry, rv);
}
return rv;
}
private static Xopg_toc_mgr Clone(Xopg_toc_mgr prime, byte[] src, byte[] ttl_bry) {
Xopg_toc_mgr rv = new Xopg_toc_mgr();
int len = prime.Len();
int src_len = src.length;
for (int i = 0; i < len; ++i) {
Xop_hdr_tkn hdr = prime.Get_at(i);
if (hdr.Src_bgn() > src_len || hdr.Src_end() > src_len) // DEBUG:handle random cases where hdr is out of bounds of source; PAGE:en.w:Germany_national_football_team DATE:2016-08-13
Gfo_usr_dlg_.Instance.Warn_many("", "", "lst:headers are not in bounds of source; ttl=~{0} src=~{1} hdr_bgn=~{2} hdr_end=~{3}", ttl_bry, src_len, hdr.Src_bgn(), hdr.Src_end());
rv.Add(hdr);
}
return rv;
}
private static Lst_section_nde_mgr Clone(Lst_section_nde_mgr src) {
Lst_section_nde_mgr rv = new Lst_section_nde_mgr();
int len = src.Len();
for (int i = 0; i < len; ++i)
rv.Add(src.Get_at(i));
return rv;
}
public static final byte[] Null_arg = null;
}

View File

@@ -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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.xtns.pfuncs.*;
import gplx.xowa.langs.*; import gplx.xowa.langs.kwds.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.tmpls.*;
public class Lst_pfunc_lst extends Pf_func_base {
@Override public int Id() {return Xol_kwd_grp_.Id_lst;}
@Override public Pf_func New(int id, byte[] name) {return new Lst_pfunc_lst().Name_(name);}
@Override public void Func_evaluate(Bry_bfr bfr, Xop_ctx ctx, Xot_invk caller, Xot_invk self, byte[] src) {
// get args
byte[] page_ttl = Eval_argx(ctx, src, caller, self); if (Bry_.Len_eq_0(page_ttl)) return; // {{#lst:}} -> ""
int args_len = self.Args_len();
byte[] sect_bgn = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 0, Lst_pfunc_itm.Null_arg);
byte[] sect_end = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 1, Lst_pfunc_itm.Null_arg);
// parse
Lst_pfunc_itm itm = Lst_pfunc_itm.New_sect_or_null(ctx, page_ttl); if (itm == null) return;
Lst_pfunc_lst_.Sect_include(bfr, itm.Sec_mgr(), itm.Itm_src(), sect_bgn, sect_end);
}
public static final Lst_pfunc_lst Prime = new Lst_pfunc_lst(); Lst_pfunc_lst() {}
}

View File

@@ -0,0 +1,72 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.tmpls.*;
public class Lst_pfunc_lst_ {
private static final byte Include_between = 0, Include_to_eos = 1, Include_to_bos = 2;
public static void Sect_include(Bry_bfr bfr, Lst_section_nde_mgr sec_mgr, byte[] src, byte[] lst_bgn, byte[] lst_end) {
if (lst_end == Lst_pfunc_itm.Null_arg) { // no end arg; EX: {{#lst:page|bgn}}; NOTE: different than {{#lst:page|bgn|}}
if (lst_bgn == Lst_pfunc_itm.Null_arg) { // no bgn arg; EX: {{#lst:page}}
bfr.Add(src); // write all and exit
return;
}
else // bgn exists; set end to bgn; EX: {{#lst:page|bgn}} is same as {{#lst:page|bgn|bgn}}; NOTE: {{#lst:page|bgn|}} means write from bgn to eos
lst_end = lst_bgn;
}
byte include_mode = Include_between;
if (Bry_.Len_eq_0(lst_end))
include_mode = Include_to_eos;
else if (Bry_.Len_eq_0(lst_bgn))
include_mode = Include_to_bos;
int bgn_pos = 0; boolean bgn_found = false; int src_page_bry_len = src.length;
int sections_len = sec_mgr.Len();
for (int i = 0; i < sections_len; i++) {
Lst_section_nde section = sec_mgr.Get_at(i);
byte section_tid = section.Name_tid();
byte[] section_key = section.Section_name();
if (section_tid == Lst_section_nde.Xatr_bgn && Bry_.Eq(section_key, lst_bgn)) {
int sect_bgn_rhs = section.Xnde().Tag_close_end();
if (include_mode == Include_to_eos) { // write from cur to eos; EX: {{#lst:page|bgn|}}
bfr.Add_mid(src, sect_bgn_rhs, src_page_bry_len);
return;
}
else { // bgn and end
if (!bgn_found) { // NOTE: !bgn_found to prevent "resetting" of dupe; EX: <s begin=key0/>a<s begin=key0/>b; should start from a not b
bgn_pos = sect_bgn_rhs;
bgn_found = true;
}
}
}
else if (section_tid == Lst_section_nde.Xatr_end && Bry_.Eq(section_key, lst_end)) {
int sect_end_lhs = section.Xnde().Tag_open_bgn();
if (include_mode == Include_to_bos) { // write from bos to cur; EX: {{#lst:page||end}}
bfr.Add_mid(src, 0, sect_end_lhs);
return;
}
else {
if (bgn_found) { // NOTE: bgn_found to prevent writing from bos; EX: a<s end=key0/>b should not write anything
bfr.Add_mid(src, bgn_pos, sect_end_lhs);
bgn_found = false;
}
}
}
}
if (bgn_found) // bgn_found, but no end; write to end of page; EX: "a <section begin=key/> b" -> " b"
bfr.Add_mid(src, bgn_pos, src_page_bry_len);
}
}

View File

@@ -0,0 +1,138 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Lst_pfunc_lst_tst {
@Before public void init() {fxt.Clear();} private Lst_pfunc_lst_fxt fxt = new Lst_pfunc_lst_fxt();
@Test public void Bgn_only() {
fxt.Clear().Page_txt_("a<section begin=key0/>val0<section end=key0/> b").Test_lst("{{#lst:section_test|key0}}", "val0");
}
@Test public void Multiple() {
fxt.Clear().Page_txt_("a<section begin=key0/>val00<section end=key0/> b<section begin=key0/> val01<section end=key0/> c").Test_lst("{{#lst:section_test|key0}}", "val00 val01");
}
@Test public void Range() {
fxt.Clear().Page_txt_("a<section begin=key0/>val0<section end=key0/> b<section begin=key1/> val1<section end=key1/> c<section begin=key2/> val2<section end=key2/> d")
.Test_lst("{{#lst:section_test|key0|key2}}", "val0 b val1 c val2");
}
@Test public void Nest() {
fxt.Clear().Page_txt_("<section begin=key0/>val0<section begin=key00/> val00<section end=key00/><section end=key0/>").Test_lst("{{#lst:section_test|key0}}", "val0 val00");
}
@Test public void Wikitext() { // PURPOSE: assert section is expanded to html
fxt.Clear().Page_txt_("a<section begin=key0/>''val0''<section end=key0/> b").Test_lst("{{#lst:section_test|key0}}", "<i>val0</i>");
}
@Test public void Refs_ignored() { // PURPOSE: assert that nearby refs are ignored
fxt.Clear().Page_txt_("a<section begin=key0/>val0<ref>ref1</ref><section end=key0/> b <ref>ref2</ref>").Test_lst("{{#lst:section_test|key0}}<references/>", String_.Concat_lines_nl
( "val0<sup id=\"cite_ref-0\" class=\"reference\"><a href=\"#cite_note-0\">[1]</a></sup><ol class=\"references\">"
, "<li id=\"cite_note-0\"><span class=\"mw-cite-backlink\"><a href=\"#cite_ref-0\">^</a></span> <span class=\"reference-text\">ref1</span></li>"
, "</ol>"
));
}
@Test public void Missing_bgn_end() {
fxt.Page_txt_("a<section bgn=key0/> b<section end=key0/> c");
fxt.Clear().Test_lst("{{#lst:section_test}}", "a b c");
}
@Test public void Missing_bgn() {
fxt.Page_txt_("a<section bgn=key0/> b<section end=key0/> c");
fxt.Clear().Test_lst("{{#lst:section_test||key0}}", "a b");
}
@Test public void Missing_end() {
fxt.Page_txt_("a <section begin=key0/>val0<section end=key1/> b");
fxt.Clear().Test_lst("{{#lst:section_test|key0}}", "val0 b"); // end is missing; read to end;
}
@Test public void Missing_end_noinclude() { // EX: de.wikisource.org/wiki/Versuch_einer_mokscha-mordwinischen_Grammatik/Mokscha-Texte; Seite:Ahlqvist_Forschungen_auf_dem_Gebiete_der_ural-altaischen_Sprachen_I.pdf/111
fxt.Page_txt_("a <section begin=key0/>val0<section end=key1/> b<noinclude>c</noinclude>");
fxt.Clear().Test_lst("{{#lst:section_test|key0}}", "val0 b"); // end is missing; ignore noinclude
}
@Test public void Missing_bgn_dupe() {
fxt.Page_txt_("a <section begin=key0/>val0<section end=key0/> b<section begin=key1/>val1<section end=key0/>");
fxt.Clear().Test_lst("{{#lst:section_test|key0}}", "val0");
}
@Test public void Nowiki() { // PURPOSE.fix: <nowiki> was creating incorrect sections; DATE:2013-07-11
fxt.Clear().Page_txt_("a<nowiki>''c''</nowiki><section begin=key0/>val0<section end=key0/> b").Test_lst("{{#lst:section_test|key0}}", "val0");
}
@Test public void Fullpagename() { // PURPOSE.fix: lst creates its own ctx; make sure ctx has same page_name of calling page (Test page) not default (Main page); DATE:2013-07-11
fxt.Clear().Page_txt_("a <section begin=key0/>{{FULLPAGENAME}}<section end=key0/> b").Test_lst("{{#lst:section_test|key0}}", "Test page");
}
@Test public void Nested_forbid_recursion() { // PURPOSE: forbid recursive calls; DATE:2014-02-09
fxt.Fxt().Init_page_create("Sub_0", "<section begin=key_0 />a<section end=key_0 />{{#lst:Sub_0|key_0}}"); // NOTE: recursive call to self
fxt.Fxt().Test_parse_page_all_str("{{#lst:Sub_0|key_0}}", "a");
}
@Test public void Nested_allow() { // PURPOSE: allow nested calls; DATE:2014-02-09
fxt.Fxt().Init_page_create("Template:Sub_1", "<section begin=key_1 />b<section end=key_1 />");
fxt.Fxt().Init_page_create("Sub_0", "<section begin=key_0 />a{{Sub_1}}<section end=key_0 />");
fxt.Fxt().Test_parse_page_all_str("{{#lst:Sub_0|key_0}}", "ab");
}
@Test public void Nested_recursion() { // PURPOSE: allow nested calls; it.s:Main_Page; DATE:2014-02-09
fxt.Fxt().Init_page_create("Sub_1", "<section begin=key_0 />b<section end=key_0 />");
fxt.Fxt().Init_page_create("Template:Sub_1", "{{#section:Sub_1|key_0}}");
fxt.Fxt().Init_page_create("Sub_0", "<section begin=key_0 />a{{Sub_1}}<section end=key_0 />");
fxt.Fxt().Test_parse_page_all_str("{{#section:Sub_0|key_0}}", "ab");
}
@Test public void Nested__ref() { // PURPOSE: handle tags; PAGE:it.s:La_Secchia_rapita/Canto_primo DATE:2015-12-02
fxt.Fxt().Init_page_create("Template:TagTemplate", "<ref>xyz</ref>");
fxt.Fxt().Init_page_create("PoemPage", "<poem>A{{TagTemplate}}B</poem>");
fxt.Fxt().Test_parse_page_all_str("{{#section:PoemPage}}<references/>", String_.Replace(String_.Concat_lines_nl_skip_last
( "<div class='poem'>"
, "<p>"
, "A<sup id='cite_ref-0' class='reference'><a href='#cite_note-0'>[1]</a></sup>B"
, "</p>"
, "</div><ol class='references'>"
, "<li id='cite_note-0'><span class='mw-cite-backlink'><a href='#cite_ref-0'>^</a></span> <span class='reference-text'>xyz</span></li>"
, "</ol>"
, ""
), "'", "\""));
}
@Test public void Nested__ref_poem() { // PURPOSE: handle tags; PAGE:it.s:La_Secchia_rapita/Canto_primo DATE:2015-12-02
fxt.Fxt().Init_page_create("Template:TagTemplate", "{{#tag:ref|abc<poem>def</poem>xyz}}");
fxt.Fxt().Init_page_create("PoemPage", String_.Concat_lines_nl_skip_last
( "<poem>A{{TagTemplate}}"
, " B"
, "</poem>"
));
fxt.Fxt().Test_parse_page_all_str("{{#section:PoemPage}}<references/>", String_.Replace(String_.Concat_lines_nl_skip_last
( "<div class='poem'>"
, "<p>"
, "A<sup id='cite_ref-0' class='reference'><a href='#cite_note-0'>[1]</a></sup><br/>"
, "&#160;&#160;&#160;&#160;B"
, "</p>"
, "</div><ol class='references'>"
, "<li id='cite_note-0'><span class='mw-cite-backlink'><a href='#cite_ref-0'>^</a></span> <span class='reference-text'>abc<div class='poem'>"
, "<p>"
, "def"
, "</p>"
, "</div>xyz</span></li>"
, "</ol>"
, ""
), "'", "\""));
}
}
class Lst_pfunc_lst_fxt {
public Lst_pfunc_lst_fxt Clear() {
if (fxt == null) fxt = new Xop_fxt();
fxt.Reset();
fxt.Wiki().Cache_mgr().Free_mem__all();
Io_mgr.Instance.InitEngine_mem();
return this;
}
public Xop_fxt Fxt() {return fxt;} private Xop_fxt fxt;
public Lst_pfunc_lst_fxt Page_txt_(String v) {page_txt = v; return this;} private String page_txt;
public void Test_lst(String func, String expd) {
fxt.Init_page_create("section_test", page_txt);
fxt.Test_parse_page_all_str(func, expd);
}
}

View File

@@ -0,0 +1,37 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.langs.*; import gplx.xowa.langs.kwds.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.tmpls.*;
import gplx.xowa.xtns.pfuncs.*;
public class Lst_pfunc_lsth extends Pf_func_base {
@Override public int Id() {return Xol_kwd_grp_.Id_lsth;}
@Override public Pf_func New(int id, byte[] name) {return new Lst_pfunc_lsth().Name_(name);}
@Override public void Func_evaluate(Bry_bfr bfr, Xop_ctx ctx, Xot_invk caller, Xot_invk self, byte[] src) {
// get args
byte[] page_ttl = Eval_argx(ctx, src, caller, self); if (Bry_.Len_eq_0(page_ttl)) return; // {{#lsth:}} -> ""
int args_len = self.Args_len();
byte[] hdr_bgn = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 0, Lst_pfunc_itm.Null_arg);
byte[] hdr_end = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 1, Lst_pfunc_itm.Null_arg);
// parse
Lst_pfunc_itm itm = Lst_pfunc_itm.New_hdr_or_null(ctx, page_ttl); if (itm == null) return;
Lst_pfunc_lsth_.Hdr_include(bfr, itm.Itm_src(), itm.Toc_mgr(), hdr_bgn, hdr_end);
}
public static final Lst_pfunc_lsth Prime = new Lst_pfunc_lsth(); Lst_pfunc_lsth() {}
}

View File

@@ -0,0 +1,73 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.wikis.pages.wtxts.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
class Lst_pfunc_lsth_ {
public static void Hdr_include(Bry_bfr bfr, byte[] src, Xopg_toc_mgr toc_mgr, byte[] lhs_hdr, byte[] rhs_hdr) {// REF.MW:LabeledSectionTransclusion.class.php|pfuncIncludeHeading; MW does regex on text; XO uses section_itms
// get <hdr> idxs
int len = toc_mgr.Len();
int lhs_idx = Match_or_neg1(toc_mgr, len, src, lhs_hdr, 0) ; if (lhs_idx == -1) return;
int rhs_idx = Match_or_neg1(toc_mgr, len, src, rhs_hdr, lhs_idx + 1);
// get snip_bgn
Xop_hdr_tkn lhs_tkn = toc_mgr.Get_at(lhs_idx);
int snip_bgn = lhs_tkn.Src_end();
// get snip_end
int snip_end = -1;
if (rhs_idx == -1) { // rhs_idx missing or not supplied
rhs_idx = lhs_idx + 1;
if (rhs_idx < len) { // next hdr after lhs_hdr exists; try to get next "matching" hdr; EX: h2 should match next h2; PAGE:en.w:10s_BC; DATE:2016-08-13
for (int i = rhs_idx; i < len; ++i) {
Xop_hdr_tkn rhs_tkn = toc_mgr.Get_at(i);
if (rhs_tkn.Num() == lhs_tkn.Num()) {
snip_end = rhs_tkn.Src_bgn();
break;
}
}
}
if (snip_end == -1) // no matching rhs exists, or rhs is last; get till EOS
snip_end = src.length;
}
else {
Xop_hdr_tkn rhs_tkn = toc_mgr.Get_at(rhs_idx);
snip_end = rhs_tkn.Src_bgn();
}
bfr.Add_mid(src, snip_bgn, snip_end);
}
private static int Match_or_neg1(Xopg_toc_mgr toc_mgr, int hdrs_len, byte[] src, byte[] match, int hdrs_bgn) {
for (int i = hdrs_bgn; i < hdrs_len; ++i) {
Xop_hdr_tkn hdr = toc_mgr.Get_at(i);
int txt_bgn = hdr.Src_bgn() + hdr.Num(); // skip "\n=="; 1=leading \n
if (hdr.Src_bgn() != Xop_parser_.Doc_bgn_char_0)
++txt_bgn;
// get txt_end; note that this needs to handle multiple trailing \n which is included in hdr.Src_end()
int txt_end = Bry_find_.Find_fwd(src, Bry__hdr_end, txt_bgn); // find "=\n"
txt_end = Bry_find_.Find_bwd__skip(src, txt_end, txt_bgn, Byte_ascii.Eq); // skip bwd to get to pos before 1st "="; EX: "===\n" -> find "=="
// remove ws
txt_bgn = Bry_find_.Find_fwd_while_ws(src, txt_bgn, txt_end);
txt_end = Bry_find_.Find_bwd__skip_ws(src, txt_end, txt_bgn);
if (Bry_.Eq(src, txt_bgn, txt_end, match)) return i;
}
return -1;
}
private static final byte[] Bry__hdr_end = Bry_.new_a7("=\n");
}

View File

@@ -0,0 +1,132 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Lst_pfunc_lsth_tst {
private final Lst_pfunc_lst_fxt fxt = new Lst_pfunc_lst_fxt();
private final String src_str_dflt = String_.Concat_lines_nl_skip_last
( "txt_0"
, "== hdr_1 =="
, "txt_1"
, "== hdr_2 =="
, "txt_2"
, "== hdr_3 =="
, "txt_3"
);
@Before public void init() {fxt.Clear();}
@Test public void Bgn__missing() { // PURPOSE: return ""
fxt.Page_txt_(src_str_dflt).Test_lst("{{#lsth:section_test|hdr_x}}", "");
}
@Test public void End__exists() {
fxt.Page_txt_(src_str_dflt).Test_lst("{{#lsth:section_test|hdr_1|hdr_3}}", String_.Concat_lines_nl_skip_last
( "txt_1"
, ""
, "<h2> hdr_2 </h2>"
, "txt_2"
));
}
@Test public void End__missing() { // PURPOSE: read to end of next section
String expd = String_.Concat_lines_nl_skip_last
( "txt_1"
);
fxt.Clear().Page_txt_(src_str_dflt).Test_lst("{{#lsth:section_test|hdr_1}}" , expd); // argument not given
fxt.Clear().Page_txt_(src_str_dflt).Test_lst("{{#lsth:section_test|hdr_1|hdr_x}}", expd); // argument is wrong
}
@Test public void End__missing_eos() { // PURPOSE: read to EOS if last
String expd = String_.Concat_lines_nl_skip_last
( "txt_3"
);
fxt.Clear().Page_txt_(src_str_dflt).Test_lst("{{#lsth:section_test|hdr_3}}" , expd); // argument not given
fxt.Clear().Page_txt_(src_str_dflt).Test_lst("{{#lsth:section_test|hdr_3|hdr_x}}", expd); // argument is wrong
}
@Test public void End__missing__match__len() { // PURPOSE:match next hdr with same length; PAGE:en.w:10s_BC; DATE:2016-08-13
String src_str = String_.Concat_lines_nl_skip_last
( "txt_0"
, "== hdr_1 =="
, "txt_1"
, "=== hdr_1a ==="
, "txt_1a"
, "== hdr_2 =="
, "txt_2"
);
fxt.Page_txt_(src_str).Test_lst("{{#lsth::section_test|hdr_1}}", String_.Concat_lines_nl_skip_last
( "txt_1"
, ""
, "<h3> hdr_1a </h3>"
, "txt_1a"
));
}
@Test public void Extra_nl() { // PURPOSE: hdr.Src_end() includes trailing nl; PAGE:en.w:10s_BC; DATE:2016-08-13
String src_str = String_.Concat_lines_nl_skip_last
( "txt_0"
, "== hdr_1 =="
, ""
, "txt_1"
, "== hdr_2 =="
, "txt_2"
);
fxt.Clear().Page_txt_(src_str).Test_lst("{{#lsth:section_test|hdr_1}}" , "txt_1");
}
@Test public void Only_include() { // PAGE:en.w:10s_BC; DATE:2016-08-13
String src_str = String_.Concat_lines_nl_skip_last
( "txt_0"
, "== hdr_1 =="
, "<onlyinclude>txt_1</onlyinclude>"
, "== hdr_2 =="
, "txt_2"
, "== hdr_3 =="
, "txt_3"
);
fxt.Page_txt_(src_str).Test_lst("{{#lsth::section_test|hdr_1}}", "txt_1");
}
@Test public void Bos() { // PURPOSE.defensive:handle == at BOS; DATE:2016-08-13
String src_str = String_.Concat_lines_nl_skip_last
( "==hdr_1 =="
, "txt_1"
, "== hdr_2 =="
, "txt_2"
);
fxt.Clear().Page_txt_(src_str).Test_lst("{{#lsth:section_test|hdr_1}}", "txt_1");
}
@Test public void Nested__lst() { // PURPOSE:lst inside lsth will add its toc_mgr to lsth; PAGE:en.w:Germany_national_football_team; DATE:2016-08-13
fxt.Fxt().Init_page_create("Nested_lst", String_.Concat_lines_nl_skip_last
( "test"
, "==hdr_1=="
, "txt_1"
));
String src_str = String_.Concat_lines_nl_skip_last
( "{{#lst:Nested_lst}}"
, "==hdr_2=="
, "txt_2"
, "==hdr_3=="
, "txt_3"
);
fxt.Page_txt_(src_str).Test_lst("{{#lsth::section_test|hdr_1}}", "txt_1"); // will fail with idx_out_of_bounds b/c hdr_1.Src_bgn / hdr_1.Src_end will be for Nested_lst's src
}
@Test public void Tmpl_w_nowiki() { // ISSUE:nowiki inside template can cause wrong offsets; PAGE:en.w:Germany_national_football_team; DATE:2016-08-13
fxt.Fxt().Init_page_create("Template:Nested_nowiki", "<nowiki>test</nowiki>");
String src_str = String_.Concat_lines_nl_skip_last
( "{{Nested_nowiki}}"
, "==hdr_2=="
, "txt_2"
, "==hdr_3=="
, "txt_3"
);
fxt.Page_txt_(src_str).Test_lst("{{#lsth::section_test|hdr_2}}", "txt_2"); // will fail with "" b/c <nowiki> requires a 2nd "sub_src = root.Data_mid()"
}
}

View File

@@ -0,0 +1,36 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.xtns.pfuncs.*;
import gplx.xowa.langs.*; import gplx.xowa.langs.kwds.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.tmpls.*;
public class Lst_pfunc_lstx extends Pf_func_base {
@Override public int Id() {return Xol_kwd_grp_.Id_lstx;}
@Override public Pf_func New(int id, byte[] name) {return new Lst_pfunc_lstx().Name_(name);}
@Override public void Func_evaluate(Bry_bfr bfr, Xop_ctx ctx, Xot_invk caller, Xot_invk self, byte[] src) {
byte[] page_ttl = Eval_argx(ctx, src, caller, self); if (Bry_.Len_eq_0(page_ttl)) return; // {{#lst:}} -> ""
int args_len = self.Args_len();
byte[] sect_exclude = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 0, Lst_pfunc_itm.Null_arg);
byte[] sect_replace = Pf_func_.Eval_arg_or(ctx, src, caller, self, args_len, 1, Lst_pfunc_itm.Null_arg);
// parse
Lst_pfunc_itm itm = Lst_pfunc_itm.New_sect_or_null(ctx, page_ttl); if (itm == null) return;
Lst_pfunc_lstx_.Sect_exclude(bfr, itm.Sec_mgr(), itm.Itm_src(), sect_exclude, sect_replace);
}
public static final Lst_pfunc_lstx Prime = new Lst_pfunc_lstx(); Lst_pfunc_lstx() {}
}

View File

@@ -0,0 +1,44 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.parsers.*;
class Lst_pfunc_lstx_ {
public static void Sect_exclude(Bry_bfr bfr, Lst_section_nde_mgr sec_mgr, byte[] src, byte[] sect_exclude, byte[] sect_replace) {
if (Bry_.Len_eq_0(sect_exclude)) { // no exclude arg; EX: {{#lstx:page}} or {{#lstx:page}}
bfr.Add(src); // write all and exit
return;
}
int sections_len = sec_mgr.Len();
int bgn_pos = 0;
for (int i = 0; i < sections_len; i++) {
Lst_section_nde section = sec_mgr.Get_at(i);
byte section_tid = section.Name_tid();
byte[] section_key = section.Section_name();
if (section_tid == Lst_section_nde.Xatr_bgn && Bry_.Eq(section_key, sect_exclude)) { // exclude section found
bfr.Add_mid(src, bgn_pos, section.Xnde().Tag_open_bgn()); // write everything from bgn_pos up to exclude
}
else if (section_tid == Lst_section_nde.Xatr_end && Bry_.Eq(section_key, sect_exclude)) { // exclude end found
if (sect_replace != null)
bfr.Add(sect_replace); // write replacement
bgn_pos = section.Xnde().Tag_close_end(); // reset bgn_pos
}
}
bfr.Add_mid(src, bgn_pos, src.length);
}
public static final byte[] Null_arg = null;
}

View File

@@ -0,0 +1,35 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Lst_pfunc_lstx_tst {
private Lst_pfunc_lst_fxt fxt = new Lst_pfunc_lst_fxt();
@Before public void init() {fxt.Clear();}
@Test public void Basic() {
fxt.Page_txt_("a<section begin=key0/> val0<section end=key0/> b<section begin=key1/> val1<section end=key1/> c").Test_lst("{{#lstx:section_test|key0}}", "a b val1 c");
}
@Test public void Replace() {
fxt.Page_txt_("a<section begin=key0/> val0<section end=key0/> b<section begin=key1/> val1<section end=key1/> c").Test_lst("{{#lstx:section_test|key0|val3}}", "aval3 b val1 c");
}
@Test public void Section_is_empty() {
fxt.Page_txt_("a<section begin=key0/> val0<section end=key0/> b<section begin=key1/> val1<section end=key1/> c").Test_lst("{{#lstx:section_test|}}", "a val0 b val1 c");
}
@Test public void Missing_bgn_end() {
fxt.Page_txt_("a<section begin=key0/> b<section end=key0/> c").Test_lst("{{#lstx:section_test}}", "a b c");
}
}

View File

@@ -0,0 +1,57 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.primitives.*;
import gplx.xowa.langs.*; import gplx.xowa.htmls.core.htmls.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.htmls.*;
public class Lst_section_nde implements Xox_xnde, Mwh_atr_itm_owner1 {
public byte[] Section_name() {return section_name;} private byte[] section_name;
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, Object xatr_id_obj) {
if (xatr_id_obj == null) return;
byte xatr_id = ((Byte_obj_val)xatr_id_obj).Val();
switch (xatr_id) {
case Xatr_name: case Xatr_bgn: case Xatr_end:
section_name = xatr.Val_as_bry(); name_tid = xatr_id; break;
}
}
public Xop_xnde_tkn Xnde() {return xnde;} private Xop_xnde_tkn xnde;
public byte Name_tid() {return name_tid;} private byte name_tid;
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
Mwh_atr_itm[] atrs = Xox_xnde_.Xatr__set(wiki, this, wiki.Lang().Xatrs_section(), src, xnde);
this.xnde = xnde;
xnde.Atrs_ary_(atrs);
ctx.Lst_section_mgr().Add(this);
}
public void Xtn_write(Bry_bfr bfr, Xoae_app app, Xop_ctx ctx, Xoh_html_wtr html_wtr, Xoh_wtr_ctx hctx, Xoae_page wpg, Xop_xnde_tkn xnde, byte[] src) {} // NOTE: write nothing; <section> is just a bookmark
public static final byte Xatr_name = 0, Xatr_bgn = 1, Xatr_end = 2;
public static Hash_adp_bry new_xatrs_(Xol_lang_itm lang) {
Hash_adp_bry rv = Hash_adp_bry.ci_u8(lang.Case_mgr()); // UTF8:see xatrs below
rv.Add_str_byte("name", Lst_section_nde.Xatr_name);
Xatrs_add(rv, "begin", "end");
switch (lang.Lang_id()) { // NOTE: as of v315572b, i18n is done directly in code, not in magic.php; am wary of adding keywords for general words like begin/end, so adding them manually per language; DATE:2013-02-09
case Xol_lang_stub_.Id_de: Xatrs_add(rv, "Anfang", "Ende"); break;
case Xol_lang_stub_.Id_he: Xatrs_add(rv, "התחלה", "סוף"); break;
case Xol_lang_stub_.Id_pt: Xatrs_add(rv, "começo", "fim"); break;
}
return rv;
}
private static void Xatrs_add(Hash_adp_bry hash, String key_begin, String key_end) {
hash.Add_str_byte(key_begin , Lst_section_nde.Xatr_bgn);
hash.Add_str_byte(key_end , Lst_section_nde.Xatr_end);
}
}

View File

@@ -0,0 +1,25 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
public class Lst_section_nde_mgr {
private final List_adp list = List_adp_.New();
public int Len() {return list.Count();}
public Lst_section_nde Get_at(int i) {return (Lst_section_nde)list.Get_at(i);}
public void Add(Lst_section_nde xnde) {list.Add(xnde);}
public void Clear() {list.Clear();}
}

View File

@@ -0,0 +1,31 @@
/*
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.xtns.lst; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*; import gplx.xowa.langs.*;
public class Lst_section_nde_tst {
private final Xop_fxt fxt = new Xop_fxt();
@Test public void Basic() {
fxt.Test_parse_page_all_str("a<section name=\"b\">c</section>d", "ad");
}
@Test public void German() { // PURPOSE: non-english tags for section DATE:2014-07-18
fxt.Lang_by_id_(Xol_lang_stub_.Id_de);
fxt.Test_parse_page_all_str("a<abschnitt name=\"b\">c</abschnitt>d" , "ad"); // check that German works
fxt.Test_parse_page_all_str("a<section name=\"b\">c</section>d" , "ad"); // check that English still works
fxt.Test_parse_page_all_str("a<trecho name=\"b\">c</trecho>d" , "a&lt;trecho name=&quot;b&quot;&gt;c&lt;/trecho&gt;d"); // check that Portuguese does not work
}
}