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

Section_edit: Show section when section_key is passed

This commit is contained in:
gnosygnu
2016-12-05 17:43:00 -05:00
parent d64136ab9d
commit 150454fd67
25 changed files with 293 additions and 165 deletions

View File

@@ -78,10 +78,10 @@ public class Xop_hdr_wkr implements Xop_ctx_wkr {
hdr.Init_by_parse(hdr_len, bgn_manual, end_manual);
// section-editable
if ( ctx.Wiki().Parser_mgr().Hdr__section_editable__enabled()
&& Bry_.Eq(src, cur_pos, cur_pos + Xop_section_mgr.Len__meta, Xop_section_mgr.Bry__meta)) {
ctx.Wiki().Parser_mgr().Hdr__section_editable__mgr().Parse(hdr, ctx.Page().Ttl().Full_db(), src, cur_pos, src_len);
}
// if ( ctx.Wiki().Parser_mgr().Hdr__section_editable__enabled()
// && Bry_.Eq(src, cur_pos, cur_pos + Xop_section_mgr.Len__meta, Xop_section_mgr.Bry__meta)) {
// ctx.Wiki().Parser_mgr().Hdr__section_editable__mgr().Parse(hdr, ctx.Page().Ttl().Full_db(), src, cur_pos, src_len); // SECTION_EDIT
// }
// gobble ws; hdr gobbles up trailing ws; EX: "==a== \n\t \n \nb" gobbles up all 3 "\n"s; otherwise para_wkr will process <br/>
cur_pos = Find_fwd_while_ws_hdr_version(src, cur_pos, src_len);

View File

@@ -0,0 +1,30 @@
/*
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.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) {
this.idx = idx;
this.key = key;
this.src_bgn = src_bgn;
this.src_end = src_end;
}
public int Idx() {return idx;} private final int idx;
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;
}

View File

@@ -0,0 +1,60 @@
/*
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.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.*;
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 byte[] src;
public Xop_section_list Parse(byte[] ttl_full_db, byte[] src) {
this.src = src;
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) {
// find section matching key
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_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);
}
public void On_hdr_seen(Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr) {
byte[] src = wkr.Src();
int hdr_txt_bgn = wkr.Hdr_lhs_end();
int hdr_txt_end = wkr.Hdr_rhs_bgn();
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);
Xop_section_itm itm = new Xop_section_itm(hash.Count(), key, wkr.Hdr_bgn(), wkr.Hdr_end());
hash.Add(key, itm);
}
public void On_src_done(Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr) {}
}

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.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
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() {
fxt.Exec__parse
( "== Hdr 1 =="
, "Para 1"
, ""
, "== Hdr 2 =="
, "Para 2"
, ""
, "== Hdr 3 =="
, "Para 3"
);
fxt.Test__extract_bry_or_null("Hdr 1"
, "== Hdr 1 =="
, "Para 1"
);
fxt.Test__extract_bry_or_null("Hdr 2"
, "== Hdr 2 =="
, "Para 2"
);
fxt.Test__extract_bry_or_null("Hdr 3"
, "== 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)));
}
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);
}
}

View File

@@ -17,16 +17,31 @@ 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.*;
public class Xop_section_mgr implements Xomw_hdr_cbk {
private final Bry_bfr bfr = Bry_bfr_.New();
private final Xomw_hdr_wkr hdr_wkr = new Xomw_hdr_wkr();
private int section_idx;
public byte[] Insert(Xoa_ttl ttl, byte[] src) {
section_idx = 0;
Xomw_parser_ctx pctx = new Xomw_parser_ctx(ttl);
hdr_wkr.Parse(bfr, pctx, src, 0, src.length, this);
return bfr.To_bry_and_clear();
import gplx.xowa.parsers.mws.*; import gplx.xowa.parsers.mws.wkrs.*; import gplx.xowa.parsers.hdrs.*;
public class Xop_section_mgr implements Gfo_invk {
public boolean Enabled() {return enabled;} private boolean enabled;
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&section_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 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
}
public byte[] Extract_section(Xoa_url url, Xoa_ttl ttl, byte[] src) {
// return orig if section_editing not enabled
if (!enabled) return src;
// return orig 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 src;
// parse wikitext into list of headers
Xop_section_list section_list = new Xop_section_list().Parse(ttl.Full_db(), 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
@@ -49,14 +64,22 @@ public class Xop_section_mgr implements Xomw_hdr_cbk {
hdr.Section_editable_(section_page, section_idx);
}
public void Write(Bry_bfr bfr, Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr) {
bfr.Add_mid(wkr.Src(), wkr.Hdr_lhs_bgn(), wkr.Hdr_rhs_end());
bfr.Add(Bry__meta); // <!--xo_meta|section_edit|
bfr.Add(pctx.Page_ttl().Full_db()); // Page_1
bfr.Add_byte_pipe(); // |
bfr.Add_int_variable(++section_idx); // 123
bfr.Add(Gfh_tag_.Comm_end); // -->
public void Write_html(Bry_bfr bfr, byte[] src, byte[] page_ttl, Xop_hdr_tkn hdr, byte[] name) {
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);
}
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");
else return Gfo_invk_.Rv_unhandled;
return this;
}
public static final byte[] Bry__meta = Bry_.new_a7("<!--xo_meta|section_edit|");
public static final int Len__meta = Bry__meta.length;
}

View File

@@ -1,50 +0,0 @@
/*
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.parsers.hdrs.sections; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.hdrs.*;
import org.junit.*; import gplx.core.tests.*;
public class Xop_section_mgr__tst {
private final Xop_section_mgr__fxt fxt = new Xop_section_mgr__fxt();
@Test public void Basic() {
fxt.Test__insert("Page_1", String_.Concat_lines_nl_skip_last
( "A"
, "== Hdr_1 =="
, "B"
, "== Hdr_2 =="
, "C"
), String_.Concat_lines_nl_skip_last
( "A"
, "== Hdr_1 ==<!--xo_meta|section_edit|Page_1|1-->"
, "B"
, "== Hdr_2 ==<!--xo_meta|section_edit|Page_1|2-->"
, "C"
));
}
}
class Xop_section_mgr__fxt {
private final Xowe_wiki wiki;
private final Xop_section_mgr mgr = new Xop_section_mgr();
public Xop_section_mgr__fxt() {
Xoae_app app = Xoa_app_fxt.Make__app__edit();
this.wiki = Xoa_app_fxt.Make__wiki__edit(app);
}
public void Test__insert(String page, String raw, String expd) {
Xoa_ttl ttl = wiki.Ttl_parse(Bry_.new_u8(page));
byte[] actl = mgr.Insert(ttl, Bry_.new_u8(raw));
Gftest.Eq__ary__lines(expd, actl, "section_edit:insert");
}
}