Add dirty implementation of custom javascript scripting

v3.3.4 BETA_v3.9.4.6
gnosygnu 8 years ago
parent b7f089a3ab
commit c98404a8fe

@ -15,14 +15,14 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.addons.htmls.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; package gplx.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
interface Script_engine { public interface Gfh_script_engine {
void Load_script(Io_url url); void Load_script(Io_url url);
Object Get_object(String obj_name); Object Get_object(String obj_name);
void Put_object(String name, Object obj); void Put_object(String name, Object obj);
Object Invoke_method(Object obj, String func, Object... args); Object Invoke_method(Object obj, String func, Object... args);
} }
class Script_engine__noop implements Script_engine { class Gfh_script_engine__noop implements Gfh_script_engine {
public void Load_script(Io_url url) {} public void Load_script(Io_url url) {}
public Object Get_object(String obj_name) {return null;} public Object Get_object(String obj_name) {return null;}
public void Put_object(String name, Object obj) {} public void Put_object(String name, Object obj) {}

@ -15,27 +15,36 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.addons.htmls.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; package gplx.langs.htmls.scripts; import gplx.*; import gplx.langs.*; import gplx.langs.htmls.*;
import java.io.FileReader; import java.io.FileReader;
import javax.script.Invocable; import javax.script.Invocable;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
class Script_engine__java implements Script_engine { public class Gfh_script_engine__java implements Gfh_script_engine {
private final ScriptEngine engine; private final ScriptEngine engine;
private final Invocable invk; private final Invocable invk;
public Script_engine__java() { public Gfh_script_engine__java() {
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();
this.engine = manager.getEngineByName("JavaScript"); this.engine = manager.getEngineByName("JavaScript");
this.invk = (Invocable)engine; this.invk = (Invocable)engine;
} }
public void Load_script(Io_url url) { public void Load_script(Io_url url) {
FileReader rdr = null;
try { try {
engine.eval(new FileReader(url.Xto_api())); rdr = new FileReader(url.Xto_api());
engine.eval(rdr);
// return engine.eval(script); // return engine.eval(script);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); System.out.println(e);
} }
finally {
try {
rdr.close();
} catch (Exception e) {
System.out.println(e);
}
}
} }
public void Put_object(String key, Object val) { public void Put_object(String key, Object val) {
engine.put(key, val); engine.put(key, val);

@ -32,7 +32,7 @@ public class Xoa_app_ {
} }
} }
public static final String Name = "xowa"; public static final String Name = "xowa";
public static final String Version = "3.9.4.5"; public static final String Version = "3.9.4.6";
public static String Build_date = "2012-12-30 00:00:00"; public static String Build_date = "2012-12-30 00:00:00";
public static String Op_sys_str; public static String Op_sys_str;
public static String User_agent = ""; public static String User_agent = "";

@ -26,8 +26,8 @@ public class Xoax_addon_mgr {
public void Itms__add(Xoax_addon_itm itm) { public void Itms__add(Xoax_addon_itm itm) {
synchronized (hash) { synchronized (hash) {
String addon_key = itm.Addon__key(); String addon_key = itm.Addon__key();
Xoa_app_.Usr_dlg().Log_many("", "", "addons.init: ~{0}", addon_key);
hash.Add(addon_key, itm); hash.Add(addon_key, itm);
// Xoa_app_.Usr_dlg().Log_many("", "", "addons.init: ~{0}", addon_key);
} }
} }
public Xoax_addon_mgr Add_dflts_by_app(Xoa_app app) { public Xoax_addon_mgr Add_dflts_by_app(Xoa_app app) {

@ -20,7 +20,7 @@ import gplx.dbs.*;
import gplx.xowa.bldrs.*; import gplx.xowa.addons.bldrs.files.dbs.*; import gplx.xowa.bldrs.*; import gplx.xowa.addons.bldrs.files.dbs.*;
class Xodel_find_missing_mgr { class Xodel_find_missing_mgr {
public void Exec(Xow_wiki wiki) { public void Exec(Xow_wiki wiki) {
// get page_file_map; note that it must have fsdb_deleted // get page_file_map; note that it must have fsdb_regy
Db_conn pfm_conn = Xob_db_file.New__page_file_map(wiki).Conn(); Db_conn pfm_conn = Xob_db_file.New__page_file_map(wiki).Conn();
// attach page_db // attach page_db
@ -71,7 +71,7 @@ class Xodel_find_missing_mgr {
deletion_conn.Meta_idx_create(Dbmeta_idx_itm.new_normal_by_tbl(delete_regy_tbl.tbl_name, "main", delete_regy_tbl.fld_fil_id, delete_regy_tbl.fld_thm_id)); deletion_conn.Meta_idx_create(Dbmeta_idx_itm.new_normal_by_tbl(delete_regy_tbl.tbl_name, "main", delete_regy_tbl.fld_fil_id, delete_regy_tbl.fld_thm_id));
} }
/* /*
--create in pfm_db --create fsdb_regy in pfm_db
CREATE TABLE fsdb_regy CREATE TABLE fsdb_regy
( fsdb_id integer NOT NULL PRIMARY KEY AUTOINCREMENT ( fsdb_id integer NOT NULL PRIMARY KEY AUTOINCREMENT
, fsdb_name varchar(255) NOT NULL , fsdb_name varchar(255) NOT NULL
@ -88,7 +88,7 @@ CREATE TABLE fsdb_regy
, fsdb_deleted tinyint NOT NULL , fsdb_deleted tinyint NOT NULL
); );
--run in file.make --export fsdb_regy from file.make
ATTACH 'simple.wikipedia.org-file-page_map.xowa' AS pfm_db; ATTACH 'simple.wikipedia.org-file-page_map.xowa' AS pfm_db;
INSERT INTO pfm_db.fsdb_regy SELECT * FROM fsdb_regy; INSERT INTO pfm_db.fsdb_regy SELECT * FROM fsdb_regy;
CREATE INDEX fsdb_regy__main ON fsdb_regy (fsdb_fil_id, fsdb_thm_id); CREATE INDEX fsdb_regy__main ON fsdb_regy (fsdb_fil_id, fsdb_thm_id);

@ -1,57 +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.addons.htmls.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*;
import gplx.xowa.wikis.pages.tags.*;
public class Xoh_include_mgr {
private Io_url[] main_urls; private int main_urls_len;
public void Init(Xow_wiki wiki) {
// check bin dir
Io_url include_dir = wiki.Fsys_mgr().Root_dir().GenSubDir_nest("bin", "html", "include");
if (!Io_mgr.Instance.ExistsDir(include_dir)) return;
this.main_urls = Io_mgr.Instance.QueryDir_args(include_dir).FilPath_("*.js").ExecAsUrlAry();
this.main_urls_len = main_urls.length;
if (main_urls_len == 0) {
Gfo_usr_dlg_.Instance.Warn_many("", "", "no '.js' files found; dir=~{0}", include_dir.Raw());
return;
}
}
public void Write(Xow_wiki wiki, Xoa_page page) {
this.Init(wiki);
if (main_urls_len == 0) return;
// create engine and load scripts
Script_engine engine = new Script_engine__java();
for (int i = 0; i < main_urls_len; ++i) {
engine.Load_script(main_urls[i]);
}
// engine.Put_object("page", new Xoscript_page())
Xoscript_page xos_pg = new Xoscript_page();
Object xowa_script = engine.Get_object("xowa_script");
// engine.Invoke_method(xowa_script, "main", page.Url().To_str());
engine.Invoke_method(xowa_script, "main", xos_pg);
// if (String_.Has(rv, "yes")) {
// Io_url js_url = null;
// page.Html_data().Custom_head_tags().Add(Xopg_tag_itm.New_js_file(js_url));
// }
}
// public static Xoh_include_mgr Get_or_new(Xow_wiki wiki) {
// wiki.Addon_mgr().Itms__get_or_null();
// }
}

@ -15,9 +15,8 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.addons.htmls.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; package gplx.xowa.addons.htmls.scripts; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*;
public class Xoscript_page_doc { public class Xoscript_env {
public void Test() { public Xoscript_env(Io_url root_dir) {this.root_dir = root_dir;}
// Tfds.Write("hi"); public Io_url Root_dir() {return root_dir;} private final Io_url root_dir;
}
} }

@ -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.addons.htmls.scripts; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*;
import gplx.langs.htmls.scripts.*;
import gplx.xowa.wikis.pages.tags.*;
import gplx.xowa.addons.htmls.scripts.apis.*;
public class Xoscript_mgr {
private Io_url root_dir;
private Io_url[] script_urls; private int script_urls_len;
public void Init(Xow_wiki wiki) {
// check script_dir
this.root_dir = wiki.Fsys_mgr().Root_dir().GenSubDir_nest("bin", "html", "script");
if (!Io_mgr.Instance.ExistsDir(root_dir)) return;
// xowa.boot.js
// find script urls
this.script_urls = Io_mgr.Instance.QueryDir_args(root_dir).FilPath_("*.js").ExecAsUrlAry();
this.script_urls_len = script_urls.length;
if (script_urls_len == 0) {
Gfo_usr_dlg_.Instance.Warn_many("", "", "xoscript:no '.js' files found; dir=~{0}", root_dir.Raw());
return;
}
}
public void Write(Bry_bfr rv, Xow_wiki wiki, Xoa_page page) {
// init
this.Init(wiki);
if (script_urls_len == 0) return;
// create engine and load scripts
Gfh_script_engine engine = new Gfh_script_engine__java();
for (int i = 0; i < script_urls_len; ++i) {
engine.Load_script(script_urls[i]);
}
// call script
Object xowa_script = engine.Get_object("xowa_script");
Xoscript_env env = new Xoscript_env(root_dir);
Xoscript_page spg = new Xoscript_page(rv, env, new Xoscript_url(page.Wiki().Domain_str(), String_.new_u8(page.Url().Page_bry())));
engine.Invoke_method(xowa_script, "main", spg);
// overwrite html
if (spg.Doc().Dirty()) {
rv.Clear();
rv.Add_str_u8(spg.Doc().Html());
}
}
}

@ -0,0 +1,43 @@
/*
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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_doc {
private final Bry_bfr bfr;
public Xoscript_doc(Bry_bfr bfr, Xoscript_page page) {
this.bfr = bfr;
this.page = page;
this.head = new Xoscript_doc_head(this);
this.tail = new Xoscript_doc_tail(this);
}
public Xoscript_page Page() {return page;} private final Xoscript_page page;
public Xoscript_doc_head Head() {return head;} private final Xoscript_doc_head head;
public Xoscript_doc_tail Tail() {return tail;} private final Xoscript_doc_tail tail;
public String Html() {
if (html == null) {
html = bfr.To_str();
}
return html;
} private String html;
public void Html_(String s) {
html = s;
html_dirty = true;
}
public boolean Dirty() {
return html_dirty;
} private boolean html_dirty;
}

@ -0,0 +1,32 @@
/*
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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_doc_head {
private final Xoscript_doc doc;
public Xoscript_doc_head(Xoscript_doc doc) {this.doc = doc;}
public void Add_js_file(String file, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.HEAD.TOP-->" : "<!--XOWA.SCRIPT.HEAD.BOT-->";
if (String_.Has_at_bgn(file, "./")) file = String_.Replace(file, "./", doc.Page().Env().Root_dir().To_http_file_str());
String elem = "<script src=\"" + file + "\" type=\"text/javascript\"></script>\n";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
public void Add_elem(String elem, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.HEAD.TOP-->" : "<!--XOWA.SCRIPT.HEAD.BOT-->";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
}

@ -0,0 +1,32 @@
/*
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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_doc_tail {
private final Xoscript_doc doc;
public Xoscript_doc_tail(Xoscript_doc doc) {this.doc = doc;}
public void Add_js_file(String file, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.TAIL-->" : "<!--XOWA.SCRIPT.TAIL-->";
if (String_.Has_at_bgn(file, "./")) file = String_.Replace(file, "./", doc.Page().Env().Root_dir().To_http_file_str());
String elem = "<script src=\"" + file + "\" type=\"text/javascript\"></script>\n";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
public void Add_elem(String elem, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.TAIL-->" : "<!--XOWA.SCRIPT.TAIL-->";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
}

@ -0,0 +1,28 @@
/*
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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_page {
public Xoscript_page(Bry_bfr rv, Xoscript_env env, Xoscript_url url) {
this.env = env;
this.url = url;
this.doc = new Xoscript_doc(rv, this);
}
public Xoscript_env Env() {return env;} private final Xoscript_env env;
public Xoscript_url Url() {return url;} private final Xoscript_url url;
public Xoscript_doc Doc() {return doc;} private final Xoscript_doc doc;
}

@ -15,10 +15,9 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License 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/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package gplx.xowa.addons.htmls.includes; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; package gplx.xowa.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_page { public class Xoscript_url {
private Xoscript_page_doc doc = new Xoscript_page_doc(); public Xoscript_url(String wiki_name, String page_name) {this.wiki_name = wiki_name; this.page_name = page_name;}
public Xoscript_page_doc Doc() { public String Wiki_name() {return wiki_name;} private final String wiki_name;
return doc; public String Page_name() {return page_name;} private final String page_name;
}
} }

@ -160,12 +160,12 @@ public class Xog_tab_itm implements Gfo_invk {
else { else {
wkr.Page().Tab_data().Tab().Page_(page); // NOTE: must set tab's page to current page, so that switching to it will update url bar; EX:pt.b:A"MANUAL_DE_PROCEDURI_.Sectiunea:""CONTABILITATE_SI_MANAGEMENT_FINANCIAR""" DATE:2015-09-17 wkr.Page().Tab_data().Tab().Page_(page); // NOTE: must set tab's page to current page, so that switching to it will update url bar; EX:pt.b:A"MANUAL_DE_PROCEDURI_.Sectiunea:""CONTABILITATE_SI_MANAGEMENT_FINANCIAR""" DATE:2015-09-17
if (page.Redirect_trail().Itms__len() > 0) if (page.Redirect_trail().Itms__len() > 0)
usr_dlg.Prog_many("", "", "could not find: ~{0} (redirected from ~{1})", String_.new_u8(page.Url().Page_bry()), page.Redirect_trail().Itms__get_at_0th_or_null()); usr_dlg.Prog_many("", "", "could not find page in wiki: ~{0} (redirected from ~{1})", String_.new_u8(page.Url().Page_bry()), page.Redirect_trail().Itms__get_at_0th_or_null());
else { else {
if (ttl.Ns().Id_is_file()) if (ttl.Ns().Id_is_file())
usr_dlg.Prog_one("", "", "commons.wikimedia.org must be installed in order to view the file. See [[App/Wiki_types/Commons]]: ~{0}", String_.new_u8(url.Raw()));// HOME usr_dlg.Prog_one("", "", "commons.wikimedia.org must be installed in order to view the file. See [[App/Wiki_types/Commons]]: ~{0}", String_.new_u8(url.Raw()));// HOME
else else
usr_dlg.Prog_one("", "", "could not find: ~{0}", String_.new_u8(url.Raw())); usr_dlg.Prog_one("", "", "could not find page in wiki: ~{0}", String_.new_u8(url.Raw()));
} }
} }
app.Log_wtr().Queue_enabled_(false); app.Log_wtr().Queue_enabled_(false);

@ -62,7 +62,7 @@ public class Xoh_page_wtr_wkr {
wdata_lang_wtr.Page_(null); wdata_lang_wtr.Page_(null);
if (page_mode == Xopg_page_.Tid_read) { // EXPERIMENTAL if (page_mode == Xopg_page_.Tid_read) { // EXPERIMENTAL
new gplx.xowa.addons.htmls.includes.Xoh_include_mgr().Write(wiki, page); new gplx.xowa.addons.htmls.scripts.Xoscript_mgr().Write(rv, wiki, page);
} }
} }
} }

@ -30,5 +30,5 @@ public class Xopg_page_heading implements Bfr_arg {
if (html_data.Xtn_pgbnr() != null) return; // pgbnr exists; don't add title if (html_data.Xtn_pgbnr() != null) return; // pgbnr exists; don't add title
fmtr.Bld_many(bfr, display_title); fmtr.Bld_many(bfr, display_title);
} }
private final Bry_fmt fmtr = Bry_fmt.New(Bry_.New_u8_nl_apos("<h1 id='firstHeading' class='firstHeading'><span>~{page_title}</span></h1>"), "page_title"); private final Bry_fmt fmtr = Bry_fmt.New(Bry_.New_u8_nl_apos("<h1 id='firstHeading' class='firstHeading'>~{page_title}</h1>"), "page_title"); // <span>~{page_title}</span>
} }

Loading…
Cancel
Save