X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=docs%2Fszyfrow%2Fsupport%2Ftext_prettify.html;fp=docs%2Fszyfrow%2Fsupport%2Ftext_prettify.html;h=a63330b07cce0bdc843b23af1abee1223312046a;hb=b535d9d75e69cc395e8de28c99e38564655e5ac9;hp=0000000000000000000000000000000000000000;hpb=f19a021eabb3222709b9d513839a14c01cfdfd38;p=szyfrow.git diff --git a/docs/szyfrow/support/text_prettify.html b/docs/szyfrow/support/text_prettify.html new file mode 100644 index 0000000..a63330b --- /dev/null +++ b/docs/szyfrow/support/text_prettify.html @@ -0,0 +1,293 @@ + + + + + + +szyfrow.support.text_prettify API documentation + + + + + + + + + + + +
+
+
+

Module szyfrow.support.text_prettify

+
+
+

Various functions for prettifying text, useful when cipher routines generate +strings of letters without spaces.

+
+ +Expand source code + +
"""Various functions for prettifying text, useful when cipher routines generate
+strings of letters without spaces.
+"""
+
+import string
+from szyfrow.support.segment import segment
+from szyfrow.support.utilities import cat, lcat, sanitise
+
+
+def prettify(text, width=100):
+    """Segment a text into words, then pack into lines, and combine the lines
+    into a single string for printing."""
+    return lcat(tpack(segment(text), width=width))
+
+
+def tpack(text, width=100):
+    """Pack a list of words into lines, so long as each line (including
+    intervening spaces) is no longer than _width_"""
+    lines = [text[0]]
+    for word in text[1:]:
+        if len(lines[-1]) + 1 + len(word) <= width:
+            lines[-1] += (' ' + word)
+        else:
+            lines += [word]
+    return lines
+
+
+def depunctuate_character(c):
+    """Record the punctuation of a character"""
+    if c in string.ascii_uppercase:
+        return 'UPPER'
+    elif c in string.ascii_lowercase:
+        return 'LOWER'
+    else:
+        return c
+
+
+def depunctuate(text):
+    """Record the punctuation of a string, so it can be applied to a converted
+    version of the string.
+
+    For example, 
+    punct = depunctuate(ciphertext)
+    plaintext = decipher(sanitise(ciphertext))
+    readable_plaintext = repunctuate(plaintext, punct)
+    """
+    return [depunctuate_character(c) for c in text]
+
+
+def repunctuate_character(letters, punctuation):
+    """Apply the recorded punctuation to a character. The letters must be
+    an iterator of base characters."""
+    if punctuation == 'UPPER':
+        return next(letters).upper()
+    elif punctuation == 'LOWER':
+        return next(letters).lower()
+    else:
+        return punctuation
+
+
+def repunctuate(text, punctuation):
+    """Apply the recored punctuation to a sanitised string.
+
+    For example, 
+    punct = depunctuate(ciphertext)
+    plaintext = decipher(sanitise(ciphertext))
+    readable_plaintext = repunctuate(plaintext, punct)
+    """
+    letters = iter(sanitise(text))
+    return cat(repunctuate_character(letters, p) for p in punctuation)
+
+
+
+
+
+
+
+

Functions

+
+
+def cat(iterable, /) +
+
+

Concatenate any number of strings.

+

The string whose method is called is inserted in between each given string. +The result is returned as a new string.

+

Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'

+
+
+def depunctuate(text) +
+
+

Record the punctuation of a string, so it can be applied to a converted +version of the string.

+

For example, +punct = depunctuate(ciphertext) +plaintext = decipher(sanitise(ciphertext)) +readable_plaintext = repunctuate(plaintext, punct)

+
+ +Expand source code + +
def depunctuate(text):
+    """Record the punctuation of a string, so it can be applied to a converted
+    version of the string.
+
+    For example, 
+    punct = depunctuate(ciphertext)
+    plaintext = decipher(sanitise(ciphertext))
+    readable_plaintext = repunctuate(plaintext, punct)
+    """
+    return [depunctuate_character(c) for c in text]
+
+
+
+def depunctuate_character(c) +
+
+

Record the punctuation of a character

+
+ +Expand source code + +
def depunctuate_character(c):
+    """Record the punctuation of a character"""
+    if c in string.ascii_uppercase:
+        return 'UPPER'
+    elif c in string.ascii_lowercase:
+        return 'LOWER'
+    else:
+        return c
+
+
+
+def lcat(iterable, /) +
+
+

Concatenate any number of strings.

+

The string whose method is called is inserted in between each given string. +The result is returned as a new string.

+

Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'

+
+
+def prettify(text, width=100) +
+
+

Segment a text into words, then pack into lines, and combine the lines +into a single string for printing.

+
+ +Expand source code + +
def prettify(text, width=100):
+    """Segment a text into words, then pack into lines, and combine the lines
+    into a single string for printing."""
+    return lcat(tpack(segment(text), width=width))
+
+
+
+def repunctuate(text, punctuation) +
+
+

Apply the recored punctuation to a sanitised string.

+

For example, +punct = depunctuate(ciphertext) +plaintext = decipher(sanitise(ciphertext)) +readable_plaintext = repunctuate(plaintext, punct)

+
+ +Expand source code + +
def repunctuate(text, punctuation):
+    """Apply the recored punctuation to a sanitised string.
+
+    For example, 
+    punct = depunctuate(ciphertext)
+    plaintext = decipher(sanitise(ciphertext))
+    readable_plaintext = repunctuate(plaintext, punct)
+    """
+    letters = iter(sanitise(text))
+    return cat(repunctuate_character(letters, p) for p in punctuation)
+
+
+
+def repunctuate_character(letters, punctuation) +
+
+

Apply the recorded punctuation to a character. The letters must be +an iterator of base characters.

+
+ +Expand source code + +
def repunctuate_character(letters, punctuation):
+    """Apply the recorded punctuation to a character. The letters must be
+    an iterator of base characters."""
+    if punctuation == 'UPPER':
+        return next(letters).upper()
+    elif punctuation == 'LOWER':
+        return next(letters).lower()
+    else:
+        return punctuation
+
+
+
+def tpack(text, width=100) +
+
+

Pack a list of words into lines, so long as each line (including +intervening spaces) is no longer than width

+
+ +Expand source code + +
def tpack(text, width=100):
+    """Pack a list of words into lines, so long as each line (including
+    intervening spaces) is no longer than _width_"""
+    lines = [text[0]]
+    for word in text[1:]:
+        if len(lines[-1]) + 1 + len(word) <= width:
+            lines[-1] += (' ' + word)
+        else:
+            lines += [word]
+    return lines
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file