eeff8a6ad18a56709ebf7560e61625771e42d95f
[conkeror.git] / modules / string.js
blobeeff8a6ad18a56709ebf7560e61625771e42d95f
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 /**
11  * trim_whitespace removes whitespace from the beginning and end of the
12  * given string.
13  */
14 function trim_whitespace (str) {
15     return (new String(str)).replace(/^\s+|\s+$/g, "");
19 function shell_quote (str) {
20     var s = str.replace("\"", "\\\"", "g");
21     s = s.replace("$", "\$", "g");
22     return s;
26 /**
27  * Like perl's quotemeta. Backslash all non-alphanumerics.
28  */
29 function quotemeta (str) {
30     return str.replace(/([^a-zA-Z0-9])/g, "\\$1");
34 /**
35  * Given a list of choices (strings), return a regexp which matches any
36  * of them
37  */
38 function choice_regexp (choices) {
39     return ("(?:" + choices.map(quotemeta).join("|") + ")");
43 /**
44  * get_shortdoc_string, given a docstring, returns the portion of the
45  * docstring up to the first newline, or the whole docstring.
46  */
47 function get_shortdoc_string (doc) {
48     var shortdoc = null;
49     if (doc != null) {
50         var idx = doc.indexOf("\n");
51         if (idx >= 0)
52             shortdoc = doc.substring(0,idx);
53         else
54             shortdoc = doc;
55     }
56     return shortdoc;
60 /**
61  * string_format takes a format-string containing %X style format codes,
62  * and an object mapping the code-letters to replacement text.  It
63  * returns a string with the formatting codes replaced by the replacement
64  * text.
65  */
66 function string_format (spec, substitutions) {
67     return spec.replace(/%(.)/g, function (a, b) substitutions[b]);
71 /**
72  * html_escape replaces characters which are special in html with character
73  * entities, safe for inserting as text into an html document.
74  */
75 function html_escape (str) {
76     return str.replace(/&/g, '&')
77         .replace(/</g, '&lt;')
78         .replace(/>/g, '&gt;')
79         .replace('"', '&quot;', 'g');
83 /**
84  * get_spaces returns a string of n spaces.
85  */
86 function get_spaces (n) {
87     var x = "";
88     while (x.length < n) x += " ";
89     return x;
93 /**
94  * word_wrap wraps str to line_length.
95  */
96 function word_wrap (str, line_length, line_prefix_first, line_prefix) {
97     if (line_prefix === undefined)
98         line_prefix = line_prefix_first;
99     else if (line_prefix.length < line_prefix_first.length) {
100         line_prefix += get_spaces(line_prefix_first.length - line_prefix.length);
101     }
103     line_length -= line_prefix_first.length;
105     if (line_length < 1)
106         line_length = 1;
108     let cur_prefix = line_prefix_first;
110     var out = "";
111     while (line_length < str.length) {
112         let i = str.lastIndexOf(" ", line_length);
113         if (i == -1)
114             i = str.indexOf(" ", line_length);
115         if (i == -1) {
116             out += cur_prefix + str + "\n";
117             str = "";
118         }
119         else  {
120             out += cur_prefix + str.substr(0, i) + "\n";
121             while (i < str.length && str.charAt(i) ==  " ")
122                 ++i;
123             str = str.substr(i);
124         }
125         cur_prefix = line_prefix;
126     }
127     if (str.length > 0)
128         out += cur_prefix + str + "\n";
129     return out;
134  * or_string joins an array of strings on commas, except for the last
135  * pair, which it joins with the word "or".
136  */
137 function or_string (options) {
138     return options.slice(0,options.length-1)
139         .join(", ") + " or " + options[options.length - 1];
144  * build_url_regexp builds a regular expression to match URLs for a given
145  * web site.
147  * Both the $domain and $path arguments can be either regexps, in
148  * which case they will be matched as is, or strings, in which case
149  * they will be matched literally.
151  * $tlds specifies a list of valid top-level-domains to match, and
152  * defaults to .com. Useful for when e.g. foo.org and foo.com are the
153  * same.
155  * If $allow_www is true, www.domain.tld will also be allowed.
156  */
157 define_keywords("$domain", "$path", "$tlds", "$allow_www");
158 function build_url_regexp () {
159     function regexp_to_string (obj) {
160         if (typeof obj == "object" && "source" in obj)
161             return obj.source;
162         return quotemeta(obj);
163     }
165     keywords(arguments, $path = "", $tlds = ["com"], $allow_www = false);
166     var domain = regexp_to_string(arguments.$domain);
167     if (arguments.$allow_www) {
168         domain = "(?:www\.)?" + domain;
169     }
170     var path = regexp_to_string(arguments.$path);
171     var tlds = arguments.$tlds;
172     var regexp = "^https?://" + domain + "\\." + choice_regexp(tlds) + "/" + path;
173     return new RegExp(regexp);
177 provide("string");