Started on documentation
[szyfrow.git] / docs / szyfrow / support / text_prettify.html
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
6 <meta name="generator" content="pdoc 0.9.2" />
7 <title>szyfrow.support.text_prettify API documentation</title>
8 <meta name="description" content="Various functions for prettifying text, useful when cipher routines generate
9 strings of letters without spaces." />
10 <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
11 <link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
12 <link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
13 <style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
14 <style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
15 <style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
16 <script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
17 <script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
18 </head>
19 <body>
20 <main>
21 <article id="content">
22 <header>
23 <h1 class="title">Module <code>szyfrow.support.text_prettify</code></h1>
24 </header>
25 <section id="section-intro">
26 <p>Various functions for prettifying text, useful when cipher routines generate
27 strings of letters without spaces.</p>
28 <details class="source">
29 <summary>
30 <span>Expand source code</span>
31 </summary>
32 <pre><code class="python">&#34;&#34;&#34;Various functions for prettifying text, useful when cipher routines generate
33 strings of letters without spaces.
34 &#34;&#34;&#34;
35
36 import string
37 from szyfrow.support.segment import segment
38 from szyfrow.support.utilities import cat, lcat, sanitise
39
40
41 def prettify(text, width=100):
42 &#34;&#34;&#34;Segment a text into words, then pack into lines, and combine the lines
43 into a single string for printing.&#34;&#34;&#34;
44 return lcat(tpack(segment(text), width=width))
45
46
47 def tpack(text, width=100):
48 &#34;&#34;&#34;Pack a list of words into lines, so long as each line (including
49 intervening spaces) is no longer than _width_&#34;&#34;&#34;
50 lines = [text[0]]
51 for word in text[1:]:
52 if len(lines[-1]) + 1 + len(word) &lt;= width:
53 lines[-1] += (&#39; &#39; + word)
54 else:
55 lines += [word]
56 return lines
57
58
59 def depunctuate_character(c):
60 &#34;&#34;&#34;Record the punctuation of a character&#34;&#34;&#34;
61 if c in string.ascii_uppercase:
62 return &#39;UPPER&#39;
63 elif c in string.ascii_lowercase:
64 return &#39;LOWER&#39;
65 else:
66 return c
67
68
69 def depunctuate(text):
70 &#34;&#34;&#34;Record the punctuation of a string, so it can be applied to a converted
71 version of the string.
72
73 For example,
74 punct = depunctuate(ciphertext)
75 plaintext = decipher(sanitise(ciphertext))
76 readable_plaintext = repunctuate(plaintext, punct)
77 &#34;&#34;&#34;
78 return [depunctuate_character(c) for c in text]
79
80
81 def repunctuate_character(letters, punctuation):
82 &#34;&#34;&#34;Apply the recorded punctuation to a character. The letters must be
83 an iterator of base characters.&#34;&#34;&#34;
84 if punctuation == &#39;UPPER&#39;:
85 return next(letters).upper()
86 elif punctuation == &#39;LOWER&#39;:
87 return next(letters).lower()
88 else:
89 return punctuation
90
91
92 def repunctuate(text, punctuation):
93 &#34;&#34;&#34;Apply the recored punctuation to a sanitised string.
94
95 For example,
96 punct = depunctuate(ciphertext)
97 plaintext = decipher(sanitise(ciphertext))
98 readable_plaintext = repunctuate(plaintext, punct)
99 &#34;&#34;&#34;
100 letters = iter(sanitise(text))
101 return cat(repunctuate_character(letters, p) for p in punctuation)</code></pre>
102 </details>
103 </section>
104 <section>
105 </section>
106 <section>
107 </section>
108 <section>
109 <h2 class="section-title" id="header-functions">Functions</h2>
110 <dl>
111 <dt id="szyfrow.support.text_prettify.cat"><code class="name flex">
112 <span>def <span class="ident">cat</span></span>(<span>iterable, /)</span>
113 </code></dt>
114 <dd>
115 <div class="desc"><p>Concatenate any number of strings.</p>
116 <p>The string whose method is called is inserted in between each given string.
117 The result is returned as a new string.</p>
118 <p>Example: '.'.join(['ab', 'pq', 'rs']) -&gt; 'ab.pq.rs'</p></div>
119 </dd>
120 <dt id="szyfrow.support.text_prettify.depunctuate"><code class="name flex">
121 <span>def <span class="ident">depunctuate</span></span>(<span>text)</span>
122 </code></dt>
123 <dd>
124 <div class="desc"><p>Record the punctuation of a string, so it can be applied to a converted
125 version of the string.</p>
126 <p>For example,
127 punct = depunctuate(ciphertext)
128 plaintext = decipher(sanitise(ciphertext))
129 readable_plaintext = repunctuate(plaintext, punct)</p></div>
130 <details class="source">
131 <summary>
132 <span>Expand source code</span>
133 </summary>
134 <pre><code class="python">def depunctuate(text):
135 &#34;&#34;&#34;Record the punctuation of a string, so it can be applied to a converted
136 version of the string.
137
138 For example,
139 punct = depunctuate(ciphertext)
140 plaintext = decipher(sanitise(ciphertext))
141 readable_plaintext = repunctuate(plaintext, punct)
142 &#34;&#34;&#34;
143 return [depunctuate_character(c) for c in text]</code></pre>
144 </details>
145 </dd>
146 <dt id="szyfrow.support.text_prettify.depunctuate_character"><code class="name flex">
147 <span>def <span class="ident">depunctuate_character</span></span>(<span>c)</span>
148 </code></dt>
149 <dd>
150 <div class="desc"><p>Record the punctuation of a character</p></div>
151 <details class="source">
152 <summary>
153 <span>Expand source code</span>
154 </summary>
155 <pre><code class="python">def depunctuate_character(c):
156 &#34;&#34;&#34;Record the punctuation of a character&#34;&#34;&#34;
157 if c in string.ascii_uppercase:
158 return &#39;UPPER&#39;
159 elif c in string.ascii_lowercase:
160 return &#39;LOWER&#39;
161 else:
162 return c</code></pre>
163 </details>
164 </dd>
165 <dt id="szyfrow.support.text_prettify.lcat"><code class="name flex">
166 <span>def <span class="ident">lcat</span></span>(<span>iterable, /)</span>
167 </code></dt>
168 <dd>
169 <div class="desc"><p>Concatenate any number of strings.</p>
170 <p>The string whose method is called is inserted in between each given string.
171 The result is returned as a new string.</p>
172 <p>Example: '.'.join(['ab', 'pq', 'rs']) -&gt; 'ab.pq.rs'</p></div>
173 </dd>
174 <dt id="szyfrow.support.text_prettify.prettify"><code class="name flex">
175 <span>def <span class="ident">prettify</span></span>(<span>text, width=100)</span>
176 </code></dt>
177 <dd>
178 <div class="desc"><p>Segment a text into words, then pack into lines, and combine the lines
179 into a single string for printing.</p></div>
180 <details class="source">
181 <summary>
182 <span>Expand source code</span>
183 </summary>
184 <pre><code class="python">def prettify(text, width=100):
185 &#34;&#34;&#34;Segment a text into words, then pack into lines, and combine the lines
186 into a single string for printing.&#34;&#34;&#34;
187 return lcat(tpack(segment(text), width=width))</code></pre>
188 </details>
189 </dd>
190 <dt id="szyfrow.support.text_prettify.repunctuate"><code class="name flex">
191 <span>def <span class="ident">repunctuate</span></span>(<span>text, punctuation)</span>
192 </code></dt>
193 <dd>
194 <div class="desc"><p>Apply the recored punctuation to a sanitised string.</p>
195 <p>For example,
196 punct = depunctuate(ciphertext)
197 plaintext = decipher(sanitise(ciphertext))
198 readable_plaintext = repunctuate(plaintext, punct)</p></div>
199 <details class="source">
200 <summary>
201 <span>Expand source code</span>
202 </summary>
203 <pre><code class="python">def repunctuate(text, punctuation):
204 &#34;&#34;&#34;Apply the recored punctuation to a sanitised string.
205
206 For example,
207 punct = depunctuate(ciphertext)
208 plaintext = decipher(sanitise(ciphertext))
209 readable_plaintext = repunctuate(plaintext, punct)
210 &#34;&#34;&#34;
211 letters = iter(sanitise(text))
212 return cat(repunctuate_character(letters, p) for p in punctuation)</code></pre>
213 </details>
214 </dd>
215 <dt id="szyfrow.support.text_prettify.repunctuate_character"><code class="name flex">
216 <span>def <span class="ident">repunctuate_character</span></span>(<span>letters, punctuation)</span>
217 </code></dt>
218 <dd>
219 <div class="desc"><p>Apply the recorded punctuation to a character. The letters must be
220 an iterator of base characters.</p></div>
221 <details class="source">
222 <summary>
223 <span>Expand source code</span>
224 </summary>
225 <pre><code class="python">def repunctuate_character(letters, punctuation):
226 &#34;&#34;&#34;Apply the recorded punctuation to a character. The letters must be
227 an iterator of base characters.&#34;&#34;&#34;
228 if punctuation == &#39;UPPER&#39;:
229 return next(letters).upper()
230 elif punctuation == &#39;LOWER&#39;:
231 return next(letters).lower()
232 else:
233 return punctuation</code></pre>
234 </details>
235 </dd>
236 <dt id="szyfrow.support.text_prettify.tpack"><code class="name flex">
237 <span>def <span class="ident">tpack</span></span>(<span>text, width=100)</span>
238 </code></dt>
239 <dd>
240 <div class="desc"><p>Pack a list of words into lines, so long as each line (including
241 intervening spaces) is no longer than <em>width</em></p></div>
242 <details class="source">
243 <summary>
244 <span>Expand source code</span>
245 </summary>
246 <pre><code class="python">def tpack(text, width=100):
247 &#34;&#34;&#34;Pack a list of words into lines, so long as each line (including
248 intervening spaces) is no longer than _width_&#34;&#34;&#34;
249 lines = [text[0]]
250 for word in text[1:]:
251 if len(lines[-1]) + 1 + len(word) &lt;= width:
252 lines[-1] += (&#39; &#39; + word)
253 else:
254 lines += [word]
255 return lines</code></pre>
256 </details>
257 </dd>
258 </dl>
259 </section>
260 <section>
261 </section>
262 </article>
263 <nav id="sidebar">
264 <h1>Index</h1>
265 <div class="toc">
266 <ul></ul>
267 </div>
268 <ul id="index">
269 <li><h3>Super-module</h3>
270 <ul>
271 <li><code><a title="szyfrow.support" href="index.html">szyfrow.support</a></code></li>
272 </ul>
273 </li>
274 <li><h3><a href="#header-functions">Functions</a></h3>
275 <ul class="">
276 <li><code><a title="szyfrow.support.text_prettify.cat" href="#szyfrow.support.text_prettify.cat">cat</a></code></li>
277 <li><code><a title="szyfrow.support.text_prettify.depunctuate" href="#szyfrow.support.text_prettify.depunctuate">depunctuate</a></code></li>
278 <li><code><a title="szyfrow.support.text_prettify.depunctuate_character" href="#szyfrow.support.text_prettify.depunctuate_character">depunctuate_character</a></code></li>
279 <li><code><a title="szyfrow.support.text_prettify.lcat" href="#szyfrow.support.text_prettify.lcat">lcat</a></code></li>
280 <li><code><a title="szyfrow.support.text_prettify.prettify" href="#szyfrow.support.text_prettify.prettify">prettify</a></code></li>
281 <li><code><a title="szyfrow.support.text_prettify.repunctuate" href="#szyfrow.support.text_prettify.repunctuate">repunctuate</a></code></li>
282 <li><code><a title="szyfrow.support.text_prettify.repunctuate_character" href="#szyfrow.support.text_prettify.repunctuate_character">repunctuate_character</a></code></li>
283 <li><code><a title="szyfrow.support.text_prettify.tpack" href="#szyfrow.support.text_prettify.tpack">tpack</a></code></li>
284 </ul>
285 </li>
286 </ul>
287 </nav>
288 </main>
289 <footer id="footer">
290 <p>Generated by <a href="https://pdoc3.github.io/pdoc"><cite>pdoc</cite> 0.9.2</a>.</p>
291 </footer>
292 </body>
293 </html>