mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Section_edit: Support headers with covering behavior, dupes, xml, math, and templates
This commit is contained in:
@@ -17,13 +17,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
|
||||
class Xop_section_itm {
|
||||
public Xop_section_itm(int idx, byte[] key, int src_bgn, int src_end) {
|
||||
public Xop_section_itm(int idx, int num, byte[] key, int src_bgn, int src_end) {
|
||||
this.idx = idx;
|
||||
this.num = num;
|
||||
this.key = key;
|
||||
this.src_bgn = src_bgn;
|
||||
this.src_end = src_end;
|
||||
}
|
||||
public int Idx() {return idx;} private final int idx;
|
||||
public int Num() {return num;} private final int num;
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public int Src_bgn() {return src_bgn;} private final int src_bgn;
|
||||
public int Src_end() {return src_end;} private final int src_end;
|
||||
|
||||
@@ -17,19 +17,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
|
||||
import gplx.xowa.parsers.mws.*; import gplx.xowa.parsers.mws.wkrs.*;
|
||||
import gplx.xowa.addons.htmls.tocs.*; import gplx.xowa.htmls.core.htmls.tidy.*;
|
||||
class Xop_section_list implements Xomw_hdr_cbk {
|
||||
private final Xomw_hdr_wkr hdr_wkr = new Xomw_hdr_wkr();
|
||||
private final Ordered_hash hash = Ordered_hash_.New_bry();
|
||||
private final Xoh_toc_mgr toc_mgr = new Xoh_toc_mgr();
|
||||
private byte[] src;
|
||||
private Xowe_wiki wiki;
|
||||
|
||||
public Xop_section_list Parse(byte[] src) {
|
||||
public Xop_section_list Parse(Xowe_wiki wiki, Xow_tidy_mgr_interface tidy_mgr, byte[] src) {
|
||||
// clear
|
||||
this.wiki = wiki;
|
||||
this.src = src;
|
||||
hash.Clear();
|
||||
toc_mgr.Clear();
|
||||
toc_mgr.Init(tidy_mgr, Bry_.Empty, Bry_.Empty);
|
||||
|
||||
// parse
|
||||
Xomw_parser_ctx pctx = new Xomw_parser_ctx();
|
||||
hdr_wkr.Parse(pctx, src, 0, src.length, this);
|
||||
return this;
|
||||
}
|
||||
public byte[] Extract_bry_or_null(byte[] key) {
|
||||
public byte[] Slice_bry_or_null(byte[] key) {
|
||||
// find section matching key
|
||||
Xop_section_itm itm = (Xop_section_itm)hash.Get_by(key);
|
||||
if (itm == null) return null;
|
||||
@@ -60,23 +69,41 @@ class Xop_section_list implements Xomw_hdr_cbk {
|
||||
return src_bgn;
|
||||
}
|
||||
private int Get_src_end(Xop_section_itm itm) {
|
||||
int src_end = src.length;
|
||||
if (itm.Idx() != hash.Len() - 1) { // if not last, get next
|
||||
Xop_section_itm nxt = (Xop_section_itm)hash.Get_at(itm.Idx() + 1);
|
||||
int src_end = -1; // default to last
|
||||
int hash_len = hash.Len();
|
||||
for (int i = itm.Idx() + 1; i < hash_len; i++) {
|
||||
Xop_section_itm nxt = (Xop_section_itm)hash.Get_at(i);
|
||||
if (nxt.Num() > itm.Num()) continue; // skip headers that are at lower level; EX: == H2 == should skip === H3 ===
|
||||
src_end = nxt.Src_bgn();
|
||||
break;
|
||||
}
|
||||
if (src_end == -1) src_end = src.length; // no headers found; default to EOS
|
||||
src_end = Bry_find_.Find_bwd__skip_ws(src, src_end, itm.Src_bgn());
|
||||
return src_end;
|
||||
}
|
||||
public void On_hdr_seen(Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr) {
|
||||
// get key by taking everything between ==; EX: "== abc ==" -> " abc "
|
||||
byte[] src = wkr.Src();
|
||||
int hdr_txt_bgn = wkr.Hdr_lhs_end();
|
||||
int hdr_txt_end = wkr.Hdr_rhs_bgn();
|
||||
|
||||
// trim ws
|
||||
hdr_txt_bgn = Bry_find_.Find_fwd_while_ws(src, hdr_txt_bgn, hdr_txt_end);
|
||||
hdr_txt_end = Bry_find_.Find_bwd__skip_ws(src, hdr_txt_end, hdr_txt_bgn);
|
||||
byte[] key = Bry_.Mid(wkr.Src(), hdr_txt_bgn, hdr_txt_end);
|
||||
|
||||
byte[] key = Bry_.Mid(wkr.Src(), hdr_txt_bgn, hdr_txt_end);
|
||||
Xop_section_itm itm = new Xop_section_itm(hash.Count(), key, wkr.Hdr_bgn(), wkr.Hdr_end());
|
||||
// handle nested templates; EX: "== {{A}} ==" note that calling Parse_text_to_html is expensive (called per header) but should be as long as its not nested
|
||||
key = wiki.Parser_mgr().Main().Parse_text_to_html(wiki.Parser_mgr().Ctx(), key);
|
||||
|
||||
// handle math; EX: "== <math>\delta</math> =="
|
||||
key = wiki.Parser_mgr().Uniq_mgr().Convert(key);
|
||||
|
||||
// convert key to toc_text to handle (a) XML ("<i>a</i>" -> "a"); (b) dupes ("text" -> "text_2")
|
||||
int num = wkr.Hdr_num();
|
||||
Xoh_toc_itm toc_itm = toc_mgr.Add(num, key);
|
||||
key = toc_itm.Anch();
|
||||
|
||||
Xop_section_itm itm = new Xop_section_itm(hash.Count(), num, key, wkr.Hdr_bgn(), wkr.Hdr_end());
|
||||
hash.Add(key, itm);
|
||||
}
|
||||
public void On_src_done(Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr) {}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class Xop_section_list__merge__tst {
|
||||
, "== Hdr 3 =="
|
||||
, "Para 3"
|
||||
);
|
||||
fxt.Test__merge_bry_or_null("Hdr 2", String_.Concat_lines_nl_skip_last
|
||||
fxt.Test__merge_bry_or_null("Hdr_2", String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 2 =="
|
||||
, "Para 2a"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
@@ -60,7 +60,7 @@ public class Xop_section_list__merge__tst {
|
||||
, "== Hdr 3 =="
|
||||
, "Para 3"
|
||||
);
|
||||
fxt.Test__merge_bry_or_null("Hdr 2", String_.Concat_lines_nl_skip_last
|
||||
fxt.Test__merge_bry_or_null("Hdr_2", String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 2 =="
|
||||
, "Para 2a"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
@@ -87,7 +87,7 @@ public class Xop_section_list__merge__tst {
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__merge_bry_or_null("Hdr 1", String_.Concat_lines_nl_skip_last
|
||||
fxt.Test__merge_bry_or_null("Hdr_1", String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 1 =="
|
||||
, "Para 1a"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
@@ -108,7 +108,7 @@ public class Xop_section_list__merge__tst {
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__merge_bry_or_null("Hdr 1", String_.Concat_lines_nl_skip_last
|
||||
fxt.Test__merge_bry_or_null("Hdr_1", String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 1 =="
|
||||
, "Para 1a"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
@@ -129,7 +129,7 @@ public class Xop_section_list__merge__tst {
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__merge_bry_or_null("Hdr 2", String_.Concat_lines_nl_skip_last
|
||||
fxt.Test__merge_bry_or_null("Hdr_2", String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 2 =="
|
||||
, "Para 2a"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
|
||||
@@ -16,7 +16,7 @@ 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.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
import org.junit.*; import gplx.core.tests.*; import gplx.xowa.htmls.core.htmls.tidy.*;
|
||||
public class Xop_section_list__slice__tst {
|
||||
private final Xop_section_list__fxt fxt = new Xop_section_list__fxt();
|
||||
@Test public void Basic() {
|
||||
@@ -30,28 +30,95 @@ public class Xop_section_list__slice__tst {
|
||||
, "== Hdr 3 =="
|
||||
, "Para 3"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null("Hdr 1"
|
||||
fxt.Test__slice_bry_or_null("Hdr_1"
|
||||
, "== Hdr 1 =="
|
||||
, "Para 1"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null("Hdr 2"
|
||||
fxt.Test__slice_bry_or_null("Hdr_2"
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null("Hdr 3"
|
||||
fxt.Test__slice_bry_or_null("Hdr_3"
|
||||
, "== Hdr 3 =="
|
||||
, "Para 3"
|
||||
);
|
||||
}
|
||||
@Test public void Covering() {
|
||||
fxt.Exec__parse
|
||||
( "== Hdr 1 =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "=== Hdr 1a ==="
|
||||
, "Para 1a"
|
||||
, ""
|
||||
, "=== Hdr 1b ==="
|
||||
, "Para 1b"
|
||||
, ""
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null("Hdr_1"
|
||||
, "== Hdr 1 =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "=== Hdr 1a ==="
|
||||
, "Para 1a"
|
||||
, ""
|
||||
, "=== Hdr 1b ==="
|
||||
, "Para 1b"
|
||||
);
|
||||
}
|
||||
@Test public void Xml() {
|
||||
fxt.Exec__parse
|
||||
( "== <i>Hdr 1</i> =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null("Hdr_1", String_.Concat_lines_nl_skip_last
|
||||
( "== <i>Hdr 1</i> =="
|
||||
, "Para 1"
|
||||
));
|
||||
}
|
||||
@Test public void Math() {
|
||||
fxt.Exec__parse
|
||||
( "== <math>\\delta</math> =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null(".5Cdelta", String_.Concat_lines_nl_skip_last
|
||||
( "== <math>\\delta</math> =="
|
||||
, "Para 1"
|
||||
));
|
||||
}
|
||||
@Test public void Template() {
|
||||
fxt.Init__template("mock", "''{{{1}}}''");
|
||||
fxt.Exec__parse
|
||||
( "== {{mock|a}} =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
);
|
||||
fxt.Test__slice_bry_or_null("a", String_.Concat_lines_nl_skip_last
|
||||
( "== {{mock|a}} =="
|
||||
, "Para 1"
|
||||
));
|
||||
}
|
||||
}
|
||||
class Xop_section_list__fxt {
|
||||
private final Xop_section_list list = new Xop_section_list();
|
||||
private final Xop_fxt parser_fxt = new Xop_fxt();
|
||||
public void Init__template(String page, String text) {parser_fxt.Init_defn_add(page, text);}
|
||||
public void Exec__parse(String... lines) {
|
||||
list.Parse(Bry_.new_u8(String_.Concat_lines_nl_skip_last(lines)));
|
||||
list.Parse(parser_fxt.Wiki(), Xow_tidy_mgr_interface_.Noop, Bry_.new_u8(String_.Concat_lines_nl_skip_last(lines)));
|
||||
}
|
||||
public void Test__slice_bry_or_null(String key, String... lines) {
|
||||
String expd = String_.Concat_lines_nl_skip_last(lines);
|
||||
byte[] actl = list.Extract_bry_or_null(Bry_.new_u8(key));
|
||||
byte[] actl = list.Slice_bry_or_null(Bry_.new_u8(key));
|
||||
Gftest.Eq__ary__lines(expd, actl, key);
|
||||
}
|
||||
public void Test__merge_bry_or_null(String key, String edit, String expd) {
|
||||
|
||||
@@ -17,18 +17,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
|
||||
import gplx.langs.htmls.*;
|
||||
import gplx.xowa.parsers.mws.*; import gplx.xowa.parsers.mws.wkrs.*; import gplx.xowa.parsers.hdrs.*;
|
||||
import gplx.xowa.parsers.mws.*; import gplx.xowa.parsers.mws.wkrs.*; import gplx.xowa.parsers.hdrs.*; import gplx.xowa.htmls.core.htmls.tidy.*;
|
||||
public class Xop_section_mgr implements Gfo_invk {
|
||||
public boolean Enabled() {return enabled;} private boolean enabled;
|
||||
private Xoae_app app; private Xowe_wiki wiki;
|
||||
private Xow_tidy_mgr_interface tidy_mgr;
|
||||
private final Bry_fmt section_editable_fmt = Bry_fmt.Auto_nl_apos
|
||||
( "<span class='mw-editsection'><span class='mw-editsection-bracket'>[</span><a href='/wiki/~{page_ttl}?action=edit§ion_key=~{section_key}' title='Edit section: ~{section_name}' class='xowa-hover-off'>edit</a><span class='mw-editsection-bracket'>]</span></span>"
|
||||
);
|
||||
private static final byte[] Qarg__section_key = Bry_.new_u8("section_key");
|
||||
|
||||
public boolean Enabled() {return enabled;} private boolean enabled;
|
||||
public void Init_by_wiki(Xowe_wiki wiki) {
|
||||
enabled = wiki.App().Cfg().Bind_bool(wiki, gplx.xowa.htmls.core.wkrs.hdrs.Xoh_section_editable_.Cfg__section_editing__enabled, this); // SECTION_EDIT
|
||||
this.app = wiki.Appe();
|
||||
this.wiki = wiki;
|
||||
this.enabled = wiki.App().Cfg().Bind_bool(wiki, gplx.xowa.htmls.core.wkrs.hdrs.Xoh_section_editable_.Cfg__section_editing__enabled, this); // SECTION_EDIT
|
||||
this.tidy_mgr = wiki.Html_mgr().Tidy_mgr();
|
||||
}
|
||||
public byte[] Extract_section(Xoae_app app, Xoa_url url, Xoa_ttl ttl, byte[] src) {
|
||||
public byte[] Slice_section(Xoa_url url, Xoa_ttl ttl, byte[] src) {
|
||||
// return orig if section_editing not enabled
|
||||
if (!enabled) return src;
|
||||
|
||||
@@ -37,8 +41,8 @@ public class Xop_section_mgr implements Gfo_invk {
|
||||
if (section_key == null) return src;
|
||||
|
||||
// parse wikitext into list of headers
|
||||
Xop_section_list section_list = new Xop_section_list().Parse(src);
|
||||
byte[] rv = section_list.Extract_bry_or_null(section_key);
|
||||
Xop_section_list section_list = new Xop_section_list().Parse(wiki, tidy_mgr, src);
|
||||
byte[] rv = section_list.Slice_bry_or_null(section_key);
|
||||
if (rv == null) {
|
||||
app.Gui_mgr().Kit().Ask_ok("", "", String_.Format("Section extraction failed!\nPlease do not edit this page else data will be lost!!\n\nwiki={0}\npage={1}\nsection={2}", url.Wiki_bry(), ttl.Full_db(), section_key));
|
||||
throw Err_.new_wo_type("section_key not found", "wiki", url.Wiki_bry(), "page", ttl.Full_db(), "section_key", section_key);
|
||||
@@ -54,23 +58,15 @@ public class Xop_section_mgr implements Gfo_invk {
|
||||
if (section_key == null) return edit;
|
||||
|
||||
// parse orig
|
||||
Xop_section_list section_list = new Xop_section_list().Parse(orig);
|
||||
Xop_section_list section_list = new Xop_section_list().Parse(wiki, tidy_mgr, orig);
|
||||
byte[] rv = section_list.Merge_bry_or_null(section_key, edit);
|
||||
if (rv == null)
|
||||
throw Err_.new_wo_type("could not merge section_key", "page", url.To_str(), "section_key", section_key);
|
||||
return rv;
|
||||
}
|
||||
public void Write_html(Bry_bfr bfr, byte[] src, byte[] page_ttl, Xop_hdr_tkn hdr, byte[] name) {
|
||||
// make key by (a) taking 1st and nth sub; (b) skipping ws at both ends
|
||||
Xop_tkn_itm[] subs = hdr.Subs();
|
||||
if (subs.length == 0) return; // GUARD:should not happen, but avoid array-index error
|
||||
int key_bgn = subs[0].Src_bgn();
|
||||
int key_end = subs[hdr.Subs_len() - 1].Src_end();
|
||||
key_bgn = Bry_find_.Find_fwd_while_ws(src, key_bgn, key_end);
|
||||
key_end = Bry_find_.Find_bwd__skip_ws(src, key_end, key_bgn);
|
||||
byte[] key = Bry_.Mid(src, key_bgn, key_end);
|
||||
|
||||
section_editable_fmt.Bld_many(bfr, page_ttl, key, name);
|
||||
name = wiki.Parser_mgr().Uniq_mgr().Convert(name); // need to swap out uniqs for Math; DATE:2016-12-09
|
||||
section_editable_fmt.Bld_many(bfr, page_ttl, name, name);
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, gplx.xowa.htmls.core.wkrs.hdrs.Xoh_section_editable_.Cfg__section_editing__enabled)) enabled = m.ReadBool("v");
|
||||
@@ -80,4 +76,5 @@ public class Xop_section_mgr implements Gfo_invk {
|
||||
|
||||
public static final byte[] Bry__meta = Bry_.new_a7("<!--xo_meta|section_edit|");
|
||||
public static final int Len__meta = Bry__meta.length;
|
||||
private static final byte[] Qarg__section_key = Bry_.new_u8("section_key");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user