2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2009 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
11 * trim_whitespace removes whitespace from the beginning and end of the
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");
27 * Like perl's quotemeta. Backslash all non-alphanumerics.
29 function quotemeta (str) {
30 return str.replace(/([^a-zA-Z0-9])/g, "\\$1");
35 * Given a list of choices (strings), return a regexp which matches any
38 function choice_regexp (choices) {
39 return ("(?:" + choices.map(quotemeta).join("|") + ")");
44 * get_shortdoc_string, given a docstring, returns the portion of the
45 * docstring up to the first newline, or the whole docstring.
47 function get_shortdoc_string (doc) {
50 var idx = doc.indexOf("\n");
52 shortdoc = doc.substring(0,idx);
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
66 function string_format (spec, substitutions) {
67 return spec.replace(/%(.)/g, function (a, b) substitutions[b]);
72 * html_escape replaces characters which are special in html with character
73 * entities, safe for inserting as text into an html document.
75 function html_escape (str) {
76 return str.replace(/&/g, '&')
77 .replace(/</g, '<')
78 .replace(/>/g, '>')
79 .replace('"', '"', 'g');
84 * get_spaces returns a string of n spaces.
86 function get_spaces (n) {
88 while (x.length < n) x += " ";
94 * word_wrap wraps str to line_length.
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);
103 line_length -= line_prefix_first.length;
108 let cur_prefix = line_prefix_first;
111 while (line_length < str.length) {
112 let i = str.lastIndexOf(" ", line_length);
114 i = str.indexOf(" ", line_length);
116 out += cur_prefix + str + "\n";
120 out += cur_prefix + str.substr(0, i) + "\n";
121 while (i < str.length && str.charAt(i) == " ")
125 cur_prefix = line_prefix;
128 out += cur_prefix + str + "\n";
134 * or_string joins an array of strings on commas, except for the last
135 * pair, which it joins with the word "or".
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
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
155 * If $allow_www is true, www.domain.tld will also be allowed.
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)
162 return quotemeta(obj);
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;
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);