string.js: whitespace
[conkeror.git] / modules / string.js
blob13cf984ea1377bbbe5666dcf9d43798d50c28f9a
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)
89         x += " ";
90     return x;
94 /**
95  * word_wrap wraps str to line_length.
96  */
97 function word_wrap (str, line_length, line_prefix_first, line_prefix) {
98     if (line_prefix === undefined)
99         line_prefix = line_prefix_first;
100     else if (line_prefix.length < line_prefix_first.length) {
101         line_prefix += get_spaces(line_prefix_first.length - line_prefix.length);
102     }
104     line_length -= line_prefix_first.length;
106     if (line_length < 1)
107         line_length = 1;
109     let cur_prefix = line_prefix_first;
111     var out = "";
112     while (line_length < str.length) {
113         let i = str.lastIndexOf(" ", line_length);
114         if (i == -1)
115             i = str.indexOf(" ", line_length);
116         if (i == -1) {
117             out += cur_prefix + str + "\n";
118             str = "";
119         }
120         else  {
121             out += cur_prefix + str.substr(0, i) + "\n";
122             while (i < str.length && str.charAt(i) ==  " ")
123                 ++i;
124             str = str.substr(i);
125         }
126         cur_prefix = line_prefix;
127     }
128     if (str.length > 0)
129         out += cur_prefix + str + "\n";
130     return out;
135  * or_string joins an array of strings on commas, except for the last
136  * pair, which it joins with the word "or".
137  */
138 function or_string (options) {
139     return options.slice(0,options.length-1)
140         .join(", ") + " or " + options[options.length - 1];
145  * build_url_regexp builds a regular expression to match URLs for a given
146  * web site.
148  * Both the $domain and $path arguments can be either regexps, in
149  * which case they will be matched as is, or strings, in which case
150  * they will be matched literally.
152  * $tlds specifies a list of valid top-level-domains to match, and
153  * defaults to .com. Useful for when e.g. foo.org and foo.com are the
154  * same.
156  * If $allow_www is true, www.domain.tld will also be allowed.
157  */
158 define_keywords("$domain", "$path", "$tlds", "$allow_www");
159 function build_url_regexp () {
160     function regexp_to_string (obj) {
161         if (typeof obj == "object" && "source" in obj)
162             return obj.source;
163         return quotemeta(obj);
164     }
166     keywords(arguments, $path = "", $tlds = ["com"], $allow_www = false);
167     var domain = regexp_to_string(arguments.$domain);
168     if (arguments.$allow_www) {
169         domain = "(?:www\.)?" + domain;
170     }
171     var path = regexp_to_string(arguments.$path);
172     var tlds = arguments.$tlds;
173     var regexp = "^https?://" + domain + "\\." + choice_regexp(tlds) + "/" + path;
174     return new RegExp(regexp);
178 provide("string");