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

Section_edit: Add parse / intermediate html form

This commit is contained in:
gnosygnu
2016-12-03 10:49:33 -05:00
parent 5eaaae0749
commit 496068e9aa
14 changed files with 226 additions and 32 deletions

View File

@@ -24,17 +24,17 @@ public class Xop_hdr_tkn extends Xop_tkn_itm_base {
public int Manual_end() {return manual_end;} private int manual_end; // unbalanced count; EX: == A === -> 1
public boolean First_in_doc() {return first_in_doc;} private boolean first_in_doc; // true if 1st hdr in doc
public void First_in_doc_y_() {first_in_doc = true;}
public int Section_editable_idx() {return section_editable_idx;} private int section_editable_idx; // EX: 1 as in "section=1"
public byte[] Section_editable_page() {return section_editable_page;} private byte[] section_editable_page; // EX: Earth as in 'href="/wiki/Earth"'
public int Section_editable_idx() {return section_editable_idx;} private int section_editable_idx; // EX: 1 as in "section=1"
public void Init_by_parse(int num, int manual_bgn, int manual_end) {
this.num = num;
this.manual_bgn = manual_bgn;
this.manual_end = manual_end;
}
public void Init_by_section_editable(int section_editable_idx, byte[] section_editable_page) {
this.section_editable_idx = section_editable_idx;
public void Section_editable_(byte[] section_editable_page, int section_editable_idx) {
this.section_editable_page = section_editable_page;
this.section_editable_idx = section_editable_idx;
}
public static final Xop_hdr_tkn[] Ary_empty = new Xop_hdr_tkn[0];

View File

@@ -17,7 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.parsers.hdrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
import gplx.xowa.parsers.xndes.*;
public class Xop_hdr_wkr implements Xop_ctx_wkr {
import gplx.xowa.parsers.hdrs.sections.*;
public class Xop_hdr_wkr implements Xop_ctx_wkr {
public void Ctor_ctx(Xop_ctx ctx) {}
public void Page_bgn(Xop_ctx ctx, Xop_root_tkn root) {}
public void Page_end(Xop_ctx ctx, Xop_root_tkn root, byte[] src, int src_len) {}
@@ -76,6 +77,12 @@ public class Xop_hdr_wkr implements Xop_ctx_wkr {
if (dirty)
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);
}
// 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);
ctx.Para().Process_block__bgn_n__end_y(Xop_xnde_tag_.Tag__h2);

View File

@@ -0,0 +1,62 @@
/*
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.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();
}
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);
// 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;
}
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 static final byte[] Bry__meta = Bry_.new_a7("<!--xo_meta|section_edit|");
public static final int Len__meta = Bry__meta.length;
}

View File

@@ -0,0 +1,50 @@
/*
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");
}
}