Qore TextWrap Module Reference  1.0
TextWrap.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file TextWrap.qm Text wrapping and filling
3 
4 /* TextWrap.qm Copyright 2017 Qore Technologies, s.r.o.
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // minimum required Qore version
26 
27 
28 
29 }
30 
61 namespace Init {
62  init();
63 
64 };
65 
67 namespace TextWrap {
68 
69 // Hardcode the recognized whitespace characters to the US-ASCII
70 // whitespace characters. The main reason for doing this is that
71 // some Unicode spaces (like \u00a0) are non-breaking whitespaces.
72 const c_whitespace = "\t\n\013\014\r ";
73 
74 // -- Private helpers ---------------------------------------------------
75 
76 string expandtabs (string text, int tabsize = 8);
77 
78 
79 any sum (list la, *any start);
80 
81 
82 string translate (string text, hash table);
83 
84 
85 string xsprintf (string fmt, hash args);
86 
87 
88 // -- Public functionality ----------------------------------------------
89 
137 class TextWrapper {
138 
139 public:
140  public :
141  int width = 70;
142  string initial_indent = "";
143  string subsequent_indent = "";
144  bool expand_tabs = True;
145  bool replace_whitespace = True;
146  bool fix_sentence_endings = False;
147  bool break_long_words = True;
148  bool drop_whitespace = True;
149  bool break_on_hyphens = True;
150  int tabsize = 8;
151  int max_lines;
152  string placeholder = ' [...]';
153 
154 public:
155 
156  private :
157  hash whitespace_trans;
158  string wordsep_re;
159  string wordsep_simple_re;
160  string sentence_end_re;
161 
162 public:
163 
164  constructor (*hash opts);
165 
166 
167  // -- Private methods -----------------------------------------------
168  // (possibly useful for subclasses to override)
169 
171 
177 private:
178  string _munge_whitespace (string text);
179 public:
180 
181 
183 
196  list _split (string text);
197 
198 
200 
208 private:
209  _fix_sentence_endings (reference chunks);
210 public:
211 
212 
222 private:
223  _handle_long_word (reference reversed_chunks, reference cur_line, int cur_len, int width);
224 public:
225 
226 
228 
240 private:
241  list _wrap_chunks (list chunks);
242 public:
243 
244 
245 
246 private:
247  list _split_chunks (string text);
248 public:
249 
250 
251  // -- Public interface ----------------------------------------------
252 
254 
261  list wrap (string text);
262 
264 
269  string fill (string text);
270 };
271 
272 // -- Convenience interface ---------------------------------------------
273 
275 
283  list wrap (string text, int width=70, *hash opts);
284 
285 
287 
294  string fill (string text, int width=70, *hash opts);
295 
296 
298 
308  string shorten (string text, int width, *hash opts);
309 
310 
311 // -- Loosely related functionality -------------------------------------
312 
313 const c_whitespace_only_re = '^[ \t]+$';
314 const c_leading_whitespace_re = '(^[ \t]*)(?:[^ \t\n])';
315 
317 
326  string dedent (string text);
327 
328 
330 
336  string indent (string text, string prefix, *code predicate);
337 
338 
339 }; // namespace TextWrap
340 
341 // vim:et:sw=4:ts=4:sts=4:
string fill(string text, int width=70, *hash opts)
Fill a single paragraph of text, returning a new string.
const True
list wrap(string text, int width=70, *hash opts)
Wrap a single paragraph of text, returning a list of wrapped lines.
Definition: TextWrap.qm.dox.h:137
const False
list list(...)
Definition: TextWrap.qm.dox.h:61
string dedent(string text)
Remove any common leading whitespace from every line in 'text'.
string indent(string text, string prefix, *code predicate)
Adds 'prefix' to the beginning of selected lines in 'text'.
main namespace for all public symbols in the TextWrap module
Definition: TextWrap.qm.dox.h:67
string shorten(string text, int width, *hash opts)
Collapse and truncate the given text to fit in the given width.
hash hash(object obj)