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 
63 namespace Init {
64  init();
65 
66 };
67 
69 namespace TextWrap {
70 
71 // Hardcode the recognized whitespace characters to the US-ASCII
72 // whitespace characters. The main reason for doing this is that
73 // some Unicode spaces (like \u00a0) are non-breaking whitespaces.
74 const c_whitespace = "\t\n\013\014\r ";
75 
76 // -- Private helpers ---------------------------------------------------
77 
78 any sum (list la, *any start);
79 
80 
81 string translate (string text, hash table);
82 
83 
84 string xsprintf (string fmt, hash args);
85 
86 
87 // -- Public helpers ----------------------------------------------------
88 
90 
113  string expandtabs (string text, int tabsize = 8);
114 
115 
116 // -- Public functionality ----------------------------------------------
117 
165 class TextWrapper {
166 
167 public:
168  public :
169  int width = 70;
170  string initial_indent = "";
171  string subsequent_indent = "";
172  bool expand_tabs = True;
173  bool replace_whitespace = True;
174  bool fix_sentence_endings = False;
175  bool break_long_words = True;
176  bool drop_whitespace = True;
177  bool break_on_hyphens = True;
178  int tabsize = 8;
179  int max_lines;
180  string placeholder = ' [...]';
181 
182 public:
183 
184  private :
185  hash whitespace_trans;
186  string wordsep_re;
187  string wordsep_simple_re;
188  string sentence_end_re;
189 
190 public:
191 
192  constructor (*hash opts);
193 
194 
195  // -- Private methods -----------------------------------------------
196  // (possibly useful for subclasses to override)
197 
206 private:
207  string _munge_whitespace (string text);
208 public:
209 
210 
234  list _split (string text);
235 
236 
247 private:
248  _fix_sentence_endings (reference chunks);
249 public:
250 
251 
261 private:
262  _handle_long_word (reference reversed_chunks, reference cur_line, int cur_len, int width);
263 public:
264 
265 
280 private:
281  list _wrap_chunks (list chunks);
282 public:
283 
284 
285 
286 private:
287  list _split_chunks (string text);
288 public:
289 
290 
291  // -- Public interface ----------------------------------------------
292 
294 
303  list wrap (string text);
304 
306 
313  string fill (string text);
314 };
315 
316 // -- Convenience interface ---------------------------------------------
317 
319 
327  list wrap (string text, int width=70, *hash opts);
328 
329 
331 
338  string fill (string text, int width=70, *hash opts);
339 
340 
342 
354  string shorten (string text, int width, *hash opts);
355 
356 
357 // -- Loosely related functionality -------------------------------------
358 
359 const c_whitespace_only_re = '^[ \t]+$';
360 const c_leading_whitespace_re = '(^[ \t]*)(?:[^ \t\n])';
361 
363 
372  string dedent (string text);
373 
374 
376 
382  string indent (string text, string prefix, *code predicate);
383 
384 
385 }; // namespace TextWrap
386 
387 // 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.
list wrap(string text)
Wrap a single paragraph of text, returning a list of wrapped lines.
const True
_handle_long_word(reference reversed_chunks, reference cur_line, int cur_len, int width)
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:165
string fill(string text)
Fill a single paragraph of text, returning a new string.
const False
list list(...)
list _wrap_chunks(list chunks)
string expandtabs(string text, int tabsize=8)
Return a copy of the string where all tab characters are expanded using spaces.
string _munge_whitespace(string text)
Definition: TextWrap.qm.dox.h:63
string dedent(string text)
Remove any common leading whitespace from every line in 'text'.
_fix_sentence_endings(reference chunks)
string indent(string text, string prefix, *code predicate)
Adds 'prefix' to the beginning of selected lines in 'text'.
list _split(string text)
Main namespace for all public symbols in the TextWrap module.
Definition: TextWrap.qm.dox.h:69
string shorten(string text, int width, *hash opts)
Collapse and truncate the given text to fit in the given width.
hash hash(object obj)