mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Section_edit: Add basic merge support
This commit is contained in:
@@ -42,7 +42,7 @@ public class Xow_parser_mgr {
|
||||
public boolean Lst__recursing() {return lst_recursing;} private boolean lst_recursing; public void Lst__recursing_(boolean v) {lst_recursing = v;}
|
||||
public Bry_bfr Wbase__time__bfr() {return wbase__time__bfr;} private final Bry_bfr wbase__time__bfr = Bry_bfr_.New();
|
||||
public Bry_fmtr Wbase__time__fmtr() {return wbase__time__fmtr;} private final Bry_fmtr wbase__time__fmtr = Bry_fmtr.new_();
|
||||
public Xop_section_mgr Hdr__section_editable__mgr() {return hdr__section_editable__mgr;} private final Xop_section_mgr hdr__section_editable__mgr = new Xop_section_mgr();
|
||||
public Xop_section_mgr Hdr__section_editable__mgr() {return hdr__section_editable__mgr;} private final Xop_section_mgr hdr__section_editable__mgr = new Xop_section_mgr();
|
||||
public Wdata_hwtr_msgs Wbase__time__msgs() {
|
||||
if (wbase__time__msgs == null)
|
||||
wbase__time__msgs = Wdata_hwtr_msgs.new_(wiki.Msg_mgr());
|
||||
|
||||
@@ -22,7 +22,7 @@ class Xop_section_list implements Xomw_hdr_cbk {
|
||||
private final Ordered_hash hash = Ordered_hash_.New_bry();
|
||||
private byte[] src;
|
||||
|
||||
public Xop_section_list Parse(byte[] ttl_full_db, byte[] src) {
|
||||
public Xop_section_list Parse(byte[] src) {
|
||||
this.src = src;
|
||||
Xomw_parser_ctx pctx = new Xomw_parser_ctx();
|
||||
hdr_wkr.Parse(pctx, src, 0, src.length, this);
|
||||
@@ -33,17 +33,39 @@ class Xop_section_list implements Xomw_hdr_cbk {
|
||||
Xop_section_itm itm = (Xop_section_itm)hash.Get_by(key);
|
||||
if (itm == null) return null;
|
||||
|
||||
int src_bgn = itm.Src_bgn();
|
||||
if (src[src_bgn] == Byte_ascii.Nl) src_bgn++;
|
||||
int src_bgn = Get_src_bgn(itm.Src_bgn());
|
||||
int src_end = Get_src_end(itm);
|
||||
|
||||
return Bry_.Mid(src, src_bgn, src_end);
|
||||
}
|
||||
public byte[] Merge_bry_or_null(byte[] key, byte[] edit) {
|
||||
// find section matching key
|
||||
Xop_section_itm itm = (Xop_section_itm)hash.Get_by(key);
|
||||
if (itm == null) return null;
|
||||
|
||||
int src_bgn = Get_src_bgn(itm.Src_bgn());
|
||||
int src_end = Get_src_end(itm);
|
||||
|
||||
// merge edit into orig
|
||||
Bry_bfr bfr = Bry_bfr_.New();
|
||||
bfr.Add_mid(src, 0, src_bgn);
|
||||
bfr.Add(edit);
|
||||
bfr.Add_mid(src, src_end, src.length);
|
||||
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
private int Get_src_bgn(int src_bgn) {
|
||||
if (src[src_bgn] == Byte_ascii.Nl) src_bgn++;
|
||||
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);
|
||||
src_end = nxt.Src_bgn();
|
||||
}
|
||||
src_end = Bry_find_.Find_bwd__skip_ws(src, src_end, src_bgn);
|
||||
|
||||
return Bry_.Mid(src, src_bgn, src_end);
|
||||
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) {
|
||||
byte[] src = wkr.Src();
|
||||
|
||||
@@ -19,7 +19,7 @@ package gplx.xowa.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; impo
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
public class Xop_section_list__tst {
|
||||
private final Xop_section_list__fxt fxt = new Xop_section_list__fxt();
|
||||
@Test public void Basic() {
|
||||
@Test public void Extract__basic() {
|
||||
fxt.Exec__parse
|
||||
( "== Hdr 1 =="
|
||||
, "Para 1"
|
||||
@@ -43,15 +43,45 @@ public class Xop_section_list__tst {
|
||||
, "Para 3"
|
||||
);
|
||||
}
|
||||
@Test public void Merge__basic() {
|
||||
fxt.Exec__parse
|
||||
( "== Hdr 1 =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "== Hdr 2 =="
|
||||
, "Para 2"
|
||||
, ""
|
||||
, "== Hdr 3 =="
|
||||
, "Para 3"
|
||||
);
|
||||
fxt.Test__merge_bry_or_null("Hdr 2", String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 2 =="
|
||||
, "abcd"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
( "== Hdr 1 =="
|
||||
, "Para 1"
|
||||
, ""
|
||||
, "== Hdr 2 =="
|
||||
, "abcd"
|
||||
, ""
|
||||
, "== Hdr 3 =="
|
||||
, "Para 3"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
class Xop_section_list__fxt {
|
||||
private final Xop_section_list list = new Xop_section_list();
|
||||
public void Exec__parse(String... lines) {
|
||||
list.Parse(Bry_.Empty, Bry_.new_u8(String_.Concat_lines_nl_skip_last(lines)));
|
||||
list.Parse(Bry_.new_u8(String_.Concat_lines_nl_skip_last(lines)));
|
||||
}
|
||||
public void Test__extract_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));
|
||||
Gftest.Eq__ary__lines(expd, actl, key);
|
||||
}
|
||||
public void Test__merge_bry_or_null(String key, String edit, String expd) {
|
||||
byte[] actl = list.Merge_bry_or_null(Bry_.new_u8(key), Bry_.new_u8(edit));
|
||||
Gftest.Eq__ary__lines(expd, actl, key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,34 +37,29 @@ 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(ttl.Full_db(), src);
|
||||
Xop_section_list section_list = new Xop_section_list().Parse(src);
|
||||
byte[] rv = section_list.Extract_bry_or_null(section_key);
|
||||
if (rv == null)
|
||||
throw Err_.new_wo_type("section_key not found", "page", ttl.Full_db(), "section_key", section_key);
|
||||
return rv;
|
||||
}
|
||||
public void Parse(Xop_hdr_tkn hdr, byte[] page_ttl, byte[] src, int cur_pos, int src_len) {
|
||||
// get page ttl
|
||||
int page_ttl_bgn = cur_pos + Bry__meta.length;
|
||||
int page_ttl_end = Bry_find_.Find_fwd(src, Byte_ascii.Pipe, page_ttl_bgn, src_len);
|
||||
if (page_ttl_end == Bry_find_.Not_found) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "invalid section ttl; page=~{0} excerpt=~{1}", page_ttl, Bry_.Mid(src, cur_pos, cur_pos + 100));
|
||||
return;
|
||||
}
|
||||
byte[] section_page = Bry_.Mid(src, page_ttl_bgn, page_ttl_end);
|
||||
public byte[] Merge_section(Xoa_url url, byte[] edit, byte[] orig) {
|
||||
// return edit if not enabled
|
||||
if (!enabled) return edit;
|
||||
|
||||
// get section idx
|
||||
int section_idx_bgn = page_ttl_end + 1;
|
||||
int section_idx_end = Bry_find_.Find_fwd(src, Gfh_tag_.Comm_end, section_idx_bgn, src_len);
|
||||
int section_idx = Bry_.To_int_or(src, section_idx_bgn, section_idx_end, -1);
|
||||
if (page_ttl_end == -1) {
|
||||
Gfo_usr_dlg_.Instance.Warn_many("", "", "invalid section idx; page=~{0} excerpt=~{1}", page_ttl, Bry_.Mid(src, cur_pos, cur_pos + 100));
|
||||
return;
|
||||
}
|
||||
// return edit if section_key not in qargs
|
||||
byte[] section_key = url.Qargs_mgr().Get_val_bry_or(Qarg__section_key, null);
|
||||
if (section_key == null) return edit;
|
||||
|
||||
hdr.Section_editable_(section_page, section_idx);
|
||||
// parse orig
|
||||
Xop_section_list section_list = new Xop_section_list().Parse(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();
|
||||
@@ -72,6 +67,7 @@ public class Xop_section_mgr implements Gfo_invk {
|
||||
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);
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
|
||||
Reference in New Issue
Block a user