TextWrap.qm.dox.h
// -*- mode: c++; indent-tabs-mode: nil -*-
// @file TextWrap.qm Text wrapping and filling
/* TextWrap.qm Copyright 2017 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// minimum required Qore version
}
namespace Init {
init();
};
namespace TextWrap {
// Hardcode the recognized whitespace characters to the US-ASCII
// whitespace characters. The main reason for doing this is that
// some Unicode spaces (like \u00a0) are non-breaking whitespaces.
const c_whitespace = "\t\n\013\014\r ";
// -- Private helpers ---------------------------------------------------
any sum (list la, *any start);
string translate (string text, hash table);
string xsprintf (string fmt, hash args);
// -- Public helpers ----------------------------------------------------
string expandtabs (string text, int tabsize = 8);
// -- Public functionality ----------------------------------------------
class TextWrapper {
public:
public :
int width = 70;
string initial_indent = "";
string subsequent_indent = "";
bool expand_tabs = True;
bool replace_whitespace = True;
bool fix_sentence_endings = False;
bool break_long_words = True;
bool drop_whitespace = True;
bool break_on_hyphens = True;
int tabsize = 8;
int max_lines;
string placeholder = ' [...]';
public:
private :
hash whitespace_trans;
string wordsep_re;
string wordsep_simple_re;
string sentence_end_re;
public:
constructor (*hash opts);
// -- Private methods -----------------------------------------------
// (possibly useful for subclasses to override)
private:
string _munge_whitespace (string text);
public:
list _split (string text);
private:
_fix_sentence_endings (reference chunks);
public:
private:
_handle_long_word (reference reversed_chunks, reference cur_line, int cur_len, int width);
public:
private:
list _wrap_chunks (list chunks);
public:
private:
list _split_chunks (string text);
public:
// -- Public interface ----------------------------------------------
list wrap (string text);
string fill (string text);
};
// -- Convenience interface ---------------------------------------------
list wrap (string text, int width=70, *hash opts);
string fill (string text, int width=70, *hash opts);
string shorten (string text, int width, *hash opts);
// -- Loosely related functionality -------------------------------------
const c_whitespace_only_re = '^[ \t]+$';
const c_leading_whitespace_re = '(^[ \t]*)(?:[^ \t\n])';
string dedent (string text);
string indent (string text, string prefix, *code predicate);
}; // namespace TextWrap
// vim:et:sw=4:ts=4:sts=4:
TextWrap::fill
string fill(string text, int width=70, *hash opts)
Fill a single paragraph of text, returning a new string.
True
const True
TextWrap::wrap
list wrap(string text, int width=70, *hash opts)
Wrap a single paragraph of text, returning a list of wrapped lines.
TextWrap::TextWrapper
Definition: TextWrap.qm.dox.h:165
False
const False
list
list list(...)
TextWrap::expandtabs
string expandtabs(string text, int tabsize=8)
Return a copy of the string where all tab characters are expanded using spaces.
Init
Definition: TextWrap.qm.dox.h:63
TextWrap::dedent
string dedent(string text)
Remove any common leading whitespace from every line in &#39;text&#39;.
TextWrap::indent
string indent(string text, string prefix, *code predicate)
Adds &#39;prefix&#39; to the beginning of selected lines in &#39;text&#39;.
TextWrap
Main namespace for all public symbols in the TextWrap module.
Definition: TextWrap.qm.dox.h:69
TextWrap::shorten
string shorten(string text, int width, *hash opts)
Collapse and truncate the given text to fit in the given width.
hash
hash hash(object obj)