Section_edit: Add parse / intermediate html form

v3.3.4
gnosygnu 8 years ago
parent 5eaaae0749
commit 496068e9aa

@ -22,6 +22,7 @@ public class Gftest {
public static void Eq__ary(boolean[] expd, boolean[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bool, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(boolean[] expd, boolean[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bool, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(int[] expd, int[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__int, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(int[] expd, int[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__int, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary(long[] expd, long[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__long, expd, actl, msg_fmt, msg_args);} public static void Eq__ary(long[] expd, long[] actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__long, expd, actl, msg_fmt, msg_args);}
public static void Eq__ary__lines(String expd, byte[] actl) {Eq__ary__lines(expd, String_.new_u8(actl), "no_msg");}
public static void Eq__ary__lines(String expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__ary__lines(expd, String_.new_u8(actl), msg_fmt, msg_args);} public static void Eq__ary__lines(String expd, byte[] actl, String msg_fmt, Object... msg_args) {Eq__ary__lines(expd, String_.new_u8(actl), msg_fmt, msg_args);}
public static void Eq__ary__lines(String expd, String actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bry, Bry_split_.Split_lines(Bry_.new_u8(expd)), Bry_split_.Split_lines(Bry_.new_u8(actl)), msg_fmt, msg_args);} public static void Eq__ary__lines(String expd, String actl, String msg_fmt, Object... msg_args) {Eq__array(Type_adp_.Tid__bry, Bry_split_.Split_lines(Bry_.new_u8(expd)), Bry_split_.Split_lines(Bry_.new_u8(actl)), msg_fmt, msg_args);}
public static void Eq__ary(String[] expd, String[] actl) {Eq__array(Type_adp_.Tid__bry, Bry_.Ary(expd), Bry_.Ary(actl), "no_msg");} public static void Eq__ary(String[] expd, String[] actl) {Eq__array(Type_adp_.Tid__bry, Bry_.Ary(expd), Bry_.Ary(actl), "no_msg");}

@ -34,7 +34,7 @@ public class Xocfg_db_mgr {
public Xoitm_data_tbl Tbl__itm_data() {return tbl__itm_data;} private final Xoitm_data_tbl tbl__itm_data; public Xoitm_data_tbl Tbl__itm_data() {return tbl__itm_data;} private final Xoitm_data_tbl tbl__itm_data;
public Xonde_i18n_tbl Tbl__nde_i18n() {return tbl__nde_i18n;} private final Xonde_i18n_tbl tbl__nde_i18n; public Xonde_i18n_tbl Tbl__nde_i18n() {return tbl__nde_i18n;} private final Xonde_i18n_tbl tbl__nde_i18n;
public String Get_str(String ctx, String key) { public String Get_str1(String ctx, String key) {
Xoitm_meta_itm meta_itm = tbl__itm_meta.Select_by_key_or_null(key); Xoitm_meta_itm meta_itm = tbl__itm_meta.Select_by_key_or_null(key);
if (meta_itm == null) throw Err_.new_wo_type("cfg not defined", "ctx", ctx, "key", key); if (meta_itm == null) throw Err_.new_wo_type("cfg not defined", "ctx", ctx, "key", key);
Xoitm_data_itm data_itm = tbl__itm_data.Select_by_id_or_null(meta_itm.Id()); Xoitm_data_itm data_itm = tbl__itm_data.Select_by_id_or_null(meta_itm.Id());

@ -25,7 +25,7 @@ public class Xoapi_addon implements Gfo_invk {
public boolean App__scripting__enabled() {return app__scripting__enabled;} private boolean app__scripting__enabled = false; public boolean App__scripting__enabled() {return app__scripting__enabled;} private boolean app__scripting__enabled = false;
public String App__update__restart_cmd() {return app__update__restart_cmd;} private String app__update__restart_cmd = ""; public String App__update__restart_cmd() {return app__update__restart_cmd;} private String app__update__restart_cmd = "";
public String App__update__update_db_src() {return app__update__update_db_src;} private String app__update__update_db_src = "http://xowa.org"; public String App__update__update_db_src() {return app__update__update_db_src;} private String app__update__update_db_src = "http://xowa.org";
public boolean Parser__hdr__section_editable() {return parser__hdr__section_editable;} private boolean parser__hdr__section_editable = false; public boolean Parser__hdr__section_editable() {return parser__hdr__section_editable;} private boolean parser__hdr__section_editable = true;
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) { public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk__search)) return search; if (ctx.Match(k, Invk__search)) return search;
else if (ctx.Match(k, Invk__bldr)) return bldr; else if (ctx.Match(k, Invk__bldr)) return bldr;

@ -24,7 +24,7 @@ public class Xoh_hdr_html {
private final Bry_bfr hdr_text_bfr = Bry_bfr_.New(); private final Bry_bfr hdr_text_bfr = Bry_bfr_.New();
private final Xoh_toc_itm invalid_toc_itm = new Xoh_toc_itm().Set__txt(Bry_.Empty, Bry_.Empty); private final Xoh_toc_itm invalid_toc_itm = new Xoh_toc_itm().Set__txt(Bry_.Empty, Bry_.Empty);
private final Bry_fmt section_editable_fmt = Bry_fmt.Auto_nl_apos 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=~{section_idx}' title='Edit section: ~{section_name}'>edit</a><span class='mw-editsection-bracket'>]</span></span>" ( "<span class='mw-editsection'><span class='mw-editsection-bracket'>[</span><a href='/wiki/~{page_ttl}?action=edit&section=~{section_idx}' title='Edit section: ~{section_name}'>edit</a><span class='mw-editsection-bracket'>]</span></span>"
); );
public void Write_html(Bry_bfr bfr, Xoh_html_wtr wtr, Xowe_wiki wiki, Xoae_page page, Xop_ctx ctx, Xoh_wtr_ctx hctx, Xoh_html_wtr_cfg cfg, Xop_tkn_grp grp, int sub_idx, byte[] src, Xop_hdr_tkn hdr) { public void Write_html(Bry_bfr bfr, Xoh_html_wtr wtr, Xowe_wiki wiki, Xoae_page page, Xop_ctx ctx, Xoh_wtr_ctx hctx, Xoh_html_wtr_cfg cfg, Xop_tkn_grp grp, int sub_idx, byte[] src, Xop_hdr_tkn hdr) {
@ -66,13 +66,13 @@ public class Xoh_hdr_html {
if (hdr.Manual_end() > 0) bfr.Add_byte_repeat(Byte_ascii.Eq, hdr.Manual_end()); // '=' if (hdr.Manual_end() > 0) bfr.Add_byte_repeat(Byte_ascii.Eq, hdr.Manual_end()); // '='
if (cfg.Toc__show()) if (cfg.Toc__show())
bfr.Add(Gfh_tag_.Span_rhs); // '</span>' bfr.Add(Gfh_tag_.Span_rhs); // '</span>'
bfr.Add(Bry__hdr_rhs_bgn).Add_int(hdr_num, 1, 1); // '</h', '2'
bfr.Add(Bry__hdr_rhs_end); // '>\n'
// write section editable // write section editable
if (cfg.Section_editable()) { if (cfg.Section_editable())
section_editable_fmt.Bld_many(bfr, hdr.Section_editable_page(), hdr.Section_editable_idx(), toc_itm.Anch()); section_editable_fmt.Bld_many(bfr, hdr.Section_editable_page(), hdr.Section_editable_idx(), toc_itm.Anch());
}
bfr.Add(Bry__hdr_rhs_bgn).Add_int(hdr_num, 1, 1); // '</h', '2'
bfr.Add(Bry__hdr_rhs_end); // '>\n'
} }
} }

@ -20,7 +20,7 @@ import gplx.core.primitives.*; import gplx.core.brys.fmtrs.*;
import gplx.xowa.wikis.*; import gplx.core.envs.*; import gplx.xowa.wikis.*; import gplx.core.envs.*;
import gplx.xowa.files.*; import gplx.xowa.files.*;
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.wbases.hwtrs.*; import gplx.xowa.xtns.pfuncs.ifs.*; import gplx.xowa.xtns.pfuncs.times.*; import gplx.xowa.xtns.pfuncs.ttls.*; import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.wbases.hwtrs.*; import gplx.xowa.xtns.pfuncs.ifs.*; import gplx.xowa.xtns.pfuncs.times.*; import gplx.xowa.xtns.pfuncs.ttls.*;
import gplx.xowa.parsers.uniqs.*; import gplx.xowa.parsers.uniqs.*; import gplx.xowa.parsers.hdrs.sections.*;
public class Xow_parser_mgr { public class Xow_parser_mgr {
private final Xowe_wiki wiki; private final Xop_tkn_mkr tkn_mkr; private final Xowe_wiki wiki; private final Xop_tkn_mkr tkn_mkr;
public Xow_parser_mgr(Xowe_wiki wiki) { public Xow_parser_mgr(Xowe_wiki wiki) {
@ -42,7 +42,9 @@ 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 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_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 Bry_fmtr Wbase__time__fmtr() {return wbase__time__fmtr;} private final Bry_fmtr wbase__time__fmtr = Bry_fmtr.new_();
public boolean Hdr__section_editable__enabled() {return hdr__section_editable__enabled;} private boolean hdr__section_editable__enabled;
public Bry_fmt Hdr__section_editable__imt_fmt() {return hdr__section_editable__imt_fmt;} private final Bry_fmt hdr__section_editable__imt_fmt = Bry_fmt.New("<!--xo_meta|section_edit|~{page}|~{section}-->"); public Bry_fmt Hdr__section_editable__imt_fmt() {return hdr__section_editable__imt_fmt;} private final Bry_fmt hdr__section_editable__imt_fmt = Bry_fmt.New("<!--xo_meta|section_edit|~{page}|~{section}-->");
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() { public Wdata_hwtr_msgs Wbase__time__msgs() {
if (wbase__time__msgs == null) if (wbase__time__msgs == null)
wbase__time__msgs = Wdata_hwtr_msgs.new_(wiki.Msg_mgr()); wbase__time__msgs = Wdata_hwtr_msgs.new_(wiki.Msg_mgr());
@ -77,9 +79,12 @@ public class Xow_parser_mgr {
} private Pfunc_anchorencode_mgr anchor_encoder_mgr; } private Pfunc_anchorencode_mgr anchor_encoder_mgr;
public void Parse(Xoae_page page, boolean clear) { // main parse method; should never be called nested public void Parse(Xoae_page page, boolean clear) { // main parse method; should never be called nested
if (!Env_.Mode_testing()) wiki.Init_assert(); if (!Env_.Mode_testing()) wiki.Init_assert();
// init
tmpl_stack_ary = Bry_.Ary_empty; tmpl_stack_ary = Bry_.Ary_empty;
tmpl_stack_ary_len = tmpl_stack_ary_max = 0; tmpl_stack_ary_len = tmpl_stack_ary_max = 0;
uniq_mgr.Clear(); uniq_mgr.Clear();
hdr__section_editable__enabled = page.Wikie().Appe().Api_root().Addon().Parser__hdr__section_editable();
scrib.When_page_changed(page); // notify scribunto about page changed scrib.When_page_changed(page); // notify scribunto about page changed
ctx.Page_(page); ctx.Page_(page);

@ -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 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 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 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 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) { public void Init_by_parse(int num, int manual_bgn, int manual_end) {
this.num = num; this.num = num;
this.manual_bgn = manual_bgn; this.manual_bgn = manual_bgn;
this.manual_end = manual_end; this.manual_end = manual_end;
} }
public void Init_by_section_editable(int section_editable_idx, byte[] section_editable_page) { public void Section_editable_(byte[] section_editable_page, int section_editable_idx) {
this.section_editable_idx = section_editable_idx;
this.section_editable_page = section_editable_page; 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]; public static final Xop_hdr_tkn[] Ary_empty = new Xop_hdr_tkn[0];

@ -17,6 +17,7 @@ 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.*; package gplx.xowa.parsers.hdrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.xndes.*;
import gplx.xowa.parsers.hdrs.sections.*;
public class Xop_hdr_wkr implements Xop_ctx_wkr { public class Xop_hdr_wkr implements Xop_ctx_wkr {
public void Ctor_ctx(Xop_ctx ctx) {} public void Ctor_ctx(Xop_ctx ctx) {}
public void Page_bgn(Xop_ctx ctx, Xop_root_tkn root) {} public void Page_bgn(Xop_ctx ctx, Xop_root_tkn root) {}
@ -76,6 +77,12 @@ public class Xop_hdr_wkr implements Xop_ctx_wkr {
if (dirty) if (dirty)
hdr.Init_by_parse(hdr_len, bgn_manual, end_manual); 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/> // 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); 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); ctx.Para().Process_block__bgn_n__end_y(Xop_xnde_tag_.Tag__h2);

@ -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;
}

@ -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");
}
}

@ -17,5 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.parsers.mws; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; package gplx.xowa.parsers.mws; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*;
public class Xomw_parser_ctx { public class Xomw_parser_ctx {
public Xomw_parser_ctx(Xoa_ttl page_ttl) {
this.page_ttl = page_ttl;
}
public Xoa_ttl Page_ttl() {return page_ttl;} private Xoa_ttl page_ttl;
public static final int Pos__bos = -1; public static final int Pos__bos = -1;
} }

@ -0,0 +1,21 @@
/*
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.mws.wkrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.mws.*;
public interface Xomw_hdr_cbk {
void Write(Bry_bfr bfr, Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr);
}

@ -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.mws.wkrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.mws.*;
public class Xomw_hdr_cbk__html implements Xomw_hdr_cbk {
public void Write(Bry_bfr bfr, Xomw_parser_ctx pctx, Xomw_hdr_wkr wkr) {
int hdr_len = wkr.Hdr_len();
bfr.Add(Tag__lhs).Add_int_digits(1, hdr_len).Add(Byte_ascii.Angle_end_bry); // <h2>
bfr.Add_mid(wkr.Src(), wkr.Hdr_lhs_end(), wkr.Hdr_rhs_bgn());
bfr.Add(Tag__rhs).Add_int_digits(1, hdr_len).Add(Byte_ascii.Angle_end_bry); // </h2>
}
private static final byte[]
Tag__lhs = Bry_.new_a7("<h")
, Tag__rhs = Bry_.new_a7("</h")
;
}

@ -18,7 +18,25 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.parsers.mws.wkrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.mws.*; package gplx.xowa.parsers.mws.wkrs; import gplx.*; import gplx.xowa.*; import gplx.xowa.parsers.*; import gplx.xowa.parsers.mws.*;
import gplx.core.btries.*; import gplx.xowa.langs.*; import gplx.core.btries.*; import gplx.xowa.langs.*;
public class Xomw_hdr_wkr { public class Xomw_hdr_wkr {
public void Parse(Bry_bfr bfr, Xomw_parser_ctx pctx, byte[] src, int src_bgn, int src_end) { // REF.MW: /includes/parser/Parser.php|doHeadings private Bry_bfr bfr;
private Xomw_parser_ctx pctx;
private int src_end;
private Xomw_hdr_cbk cbk;
public byte[] Src() {return src;} private byte[] src;
public int Hdr_len() {return hdr_len;} private int hdr_len;
public int Hdr_lhs_bgn() {return hdr_lhs_bgn;} private int hdr_lhs_bgn;
public int Hdr_lhs_end() {return hdr_lhs_end;} private int hdr_lhs_end;
public int Hdr_rhs_bgn() {return hdr_rhs_bgn;} private int hdr_rhs_bgn;
public int Hdr_rhs_end() {return hdr_rhs_end;} private int hdr_rhs_end;
public void Parse(Bry_bfr bfr, Xomw_parser_ctx pctx, byte[] src, int src_bgn, int src_end, Xomw_hdr_cbk cbk) { // REF.MW: /includes/parser/Parser.php|doHeadings
// init members
this.bfr = bfr;
this.pctx = pctx;
this.src = src;
this.src_end = src_end;
this.cbk = cbk;
// do loop
int pos = src_bgn; int pos = src_bgn;
int txt_bgn = pos == Xomw_parser_ctx.Pos__bos ? 0 : pos; int txt_bgn = pos == Xomw_parser_ctx.Pos__bos ? 0 : pos;
byte b = Byte_ascii.Nl; byte b = Byte_ascii.Nl;
@ -28,7 +46,7 @@ public class Xomw_hdr_wkr {
&& nxt < src_end && nxt < src_end
&& src[nxt] == Byte_ascii.Eq && src[nxt] == Byte_ascii.Eq
) { // if \n, check if "=" ) { // if \n, check if "="
int rv = Parse_hdr_nl(bfr, pctx, src, src_bgn, src_end, txt_bgn, pos, nxt + 1); int rv = Parse_hdr_nl(txt_bgn, pos, nxt + 1);
if (rv < 0) { if (rv < 0) {
pos = rv * -1; pos = rv * -1;
} }
@ -45,36 +63,32 @@ public class Xomw_hdr_wkr {
b = src[pos]; b = src[pos];
} }
} }
private int Parse_hdr_nl(Bry_bfr bfr, Xomw_parser_ctx pctx, byte[] src, int src_bgn, int src_end, int txt_bgn, int nl_lhs, int pos) { private int Parse_hdr_nl(int txt_bgn, int nl_lhs, int pos) {
// calc pos and len // calc pos and len
int hdr_lhs_bgn = nl_lhs + 1; this.hdr_lhs_bgn = nl_lhs + 1;
int hdr_lhs_end = Bry_find_.Find_fwd_while(src, pos, src_end, Byte_ascii.Eq); this.hdr_lhs_end = Bry_find_.Find_fwd_while(src, pos, src_end, Byte_ascii.Eq);
int nl_rhs = Bry_find_.Find_fwd_or(src, Byte_ascii.Nl, hdr_lhs_end + 1, src_end, src_end); // no "\n"; src_end is rest of text; EX: "\n==<text>EOS int nl_rhs = Bry_find_.Find_fwd_or(src, Byte_ascii.Nl, hdr_lhs_end + 1, src_end, src_end); // no "\n"; src_end is rest of text; EX: "\n==<text>EOS
int hdr_rhs_end = Bry_find_.Find_bwd__skip_ws(src, nl_rhs, hdr_lhs_end); this.hdr_rhs_end = Bry_find_.Find_bwd__skip_ws(src, nl_rhs, hdr_lhs_end);
int hdr_rhs_bgn = Bry_find_.Find_bwd__skip(src, hdr_rhs_end - 1, hdr_lhs_end, Byte_ascii.Eq); this.hdr_rhs_bgn = Bry_find_.Find_bwd__skip(src, hdr_rhs_end - 1, hdr_lhs_end, Byte_ascii.Eq);
int hdr_lhs_len = hdr_lhs_end - hdr_lhs_bgn; int hdr_lhs_len = hdr_lhs_end - hdr_lhs_bgn;
int hdr_rhs_len = hdr_rhs_end - hdr_rhs_bgn; int hdr_rhs_len = hdr_rhs_end - hdr_rhs_bgn;
if (hdr_rhs_len == 0) { // handle rare situations like "\n====\n" if (hdr_rhs_len == 0) { // handle rare situations like "\n====\n"
int hdr_lhs_len_half = hdr_lhs_len / 2; int hdr_lhs_len_half = hdr_lhs_len / 2;
hdr_rhs_len = hdr_lhs_len - hdr_lhs_len_half; hdr_rhs_len = hdr_lhs_len - hdr_lhs_len_half;
hdr_lhs_len = hdr_lhs_len_half; hdr_lhs_len = hdr_lhs_len_half;
hdr_lhs_end = hdr_lhs_bgn + hdr_lhs_len; this.hdr_lhs_end = hdr_lhs_bgn + hdr_lhs_len;
hdr_rhs_bgn = hdr_lhs_end; this.hdr_rhs_bgn = hdr_lhs_end;
} }
// bld bry // bld bry
int hdr_len = hdr_lhs_len < hdr_rhs_len ? hdr_lhs_len : hdr_rhs_len; this.hdr_len = hdr_lhs_len < hdr_rhs_len ? hdr_lhs_len : hdr_rhs_len;
if (nl_lhs > txt_bgn) if (nl_lhs > txt_bgn)
bfr.Add_mid(src, txt_bgn, nl_lhs); // add all txt up to nl_lhs bfr.Add_mid(src, txt_bgn, nl_lhs); // add all txt up to nl_lhs
if (nl_lhs != Xomw_parser_ctx.Pos__bos) bfr.Add_byte_nl(); if (nl_lhs != Xomw_parser_ctx.Pos__bos) bfr.Add_byte_nl();
bfr.Add(Tag__lhs).Add_int_digits(1, hdr_len).Add(Byte_ascii.Angle_end_bry); // <h2> cbk.Write(bfr, pctx, this);
bfr.Add_mid(src, hdr_lhs_end, hdr_rhs_bgn);
bfr.Add(Tag__rhs).Add_int_digits(1, hdr_len).Add(Byte_ascii.Angle_end_bry); // </h2>
return nl_rhs; return nl_rhs;
} }
private static final byte[]
Tag__lhs = Bry_.new_a7("<h")
, Tag__rhs = Bry_.new_a7("</h")
;
} }
// for ( $i = 6; $i >= 1; --$i ) { // for ( $i = 6; $i >= 1; --$i ) {
// $h = str_repeat( '=', $i ); // $h = str_repeat( '=', $i );

@ -30,10 +30,10 @@ public class Xomw_hdr_wkr_tst {
} }
class Xomw_hdr_wkr_fxt { class Xomw_hdr_wkr_fxt {
private final Xomw_hdr_wkr wkr = new Xomw_hdr_wkr(); private final Xomw_hdr_wkr wkr = new Xomw_hdr_wkr();
private final Bry_bfr bfr = Bry_bfr_.New(); private final Xomw_parser_ctx pctx = new Xomw_parser_ctx(); private final Bry_bfr bfr = Bry_bfr_.New(); private final Xomw_parser_ctx pctx = new Xomw_parser_ctx(null);
public void Test__parse(String src_str, String expd) { public void Test__parse(String src_str, String expd) {
byte[] src_bry = Bry_.new_u8(src_str); byte[] src_bry = Bry_.new_u8(src_str);
wkr.Parse(bfr, pctx, src_bry, -1, src_bry.length); wkr.Parse(bfr, pctx, src_bry, -1, src_bry.length, new Xomw_hdr_cbk__html());
Tfds.Eq_str_lines(expd, bfr.To_str_and_clear(), src_str); Tfds.Eq_str_lines(expd, bfr.To_str_and_clear(), src_str);
} }
} }

Loading…
Cancel
Save