2 function choose_url (data, proxyPattern) {
3 //first try to prepare local_url from recipe
4 var local_url = data["md-url_recipe"] !== undefined
5 ? prepare_url(data["md-url_recipe"][0], data) : null;
7 var use_url_proxy = data["md-use_url_proxy"] !== undefined
8 ? data["md-use_url_proxy"] : "0";
10 //use the proxyPattern
11 if (proxyPattern && use_url_proxy == "1") {
13 data["md-local-url"] = [];
14 data["md-local-url"].push(local_url);
16 var ref_local_url = prepare_url(proxyPattern, data);
17 if (ref_local_url) return ref_local_url;
20 // proxyPattern failed, go for local
24 //local failed, go for resource
25 return data["md-electronic-url"] !== undefined
26 ? data["md-electronic-url"][0] : null;
29 var XRef = function (url, text) {
34 function has_recipe (data) {
36 if (data["md-url_recipe"] !== undefined) {
37 var recipe = data["md-url_recipe"][0];
38 if (typeof recipe == "string" && recipe.length>0) {
45 function getUrlFromRecipe (data) {
46 if (has_recipe(data)) {
47 return prepare_url(data["md-url_recipe"][0],data);
53 function getElectronicUrls (data) {
55 if (data["md-electronic-url"] !== undefined) {
56 for (var i=0; i<data["md-electronic-url"].length; i++) {
57 var linkUrl = data["md-electronic-url"][i];
58 var linkText = data["md-electronic-text"][i];
59 var ref = new XRef(linkUrl, (linkText.length==0 ? "Web Link" : linkText));
67 // Prepares urls from recipes with expressions in the form:
68 // ${variable-name[pattern/replacement/mode]}, [regex] is optional
69 // eg. http://sever.com?title=${md-title[\s+//]} will strip all whitespaces
70 function prepare_url(url_recipe, meta_data) {
71 if (typeof url_recipe != "string" || url_recipe.length == 0)
73 if (typeof meta_data != "object")
76 return url_recipe.replace(/\${[^}]*}/g, function (match) {
77 return get_var_value(match, meta_data);
80 return "Malformed URL recipe: " + e.message;
84 function get_var_value (expr_in, meta_data) {
86 var expr = expr_in.substring(2, expr_in.length-1)
87 if (expr == "") return "";
89 var var_name = expr.match(/^[^\[]+/)[0];
90 if (typeof meta_data[var_name] == "undefined") return "";
91 else var var_value = meta_data[var_name][0];
92 if (var_name.length < expr.length) { //possibly a regex
93 var_value = exec_sregex(
94 expr.substring(var_name.length+1, expr.length-1),
100 // exec perl-like substitution regexes in the form: pattern/replacement/mode
101 function exec_sregex (regex_str, input_str) {
102 var regex_parts = ["", "", ""];
104 for (var j=0; j<regex_str.length && i<3; j++) {
105 if (j>0 && regex_str.charAt(j) == '/' && regex_str.charAt(j-1) != '\\')
108 regex_parts[i] += regex_str.charAt(j);
110 var regex_obj = new RegExp(regex_parts[0], regex_parts[2]);
111 return input_str.replace(regex_obj, regex_parts[1]);
114 function test_url_recipe() {
115 var url_recipe = "http://www.indexdata.com/?title=${md-title[\\s+/+/g]}&author=${md-author}";
116 var meta_data = { "md-title" : ["Art of Computer Programming"], "md-author" : ["Knuth"]}
117 var final_url = prepare_url(url_recipe, meta_data);