diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 16:07:23 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 16:07:23 -0400 |
| commit | 6782cc5570e194791ca6cdd695b88897e9145564 (patch) | |
| tree | a428e9d954aca251212ec1cf15bd35e0badce630 /examples/wrap | |
| parent | 448e805293989b06e07878a4a87fdd378f7c6e02 (diff) | |
No more colons for blocks
Diffstat (limited to 'examples/wrap')
| -rw-r--r-- | examples/wrap/wrap.tm | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/examples/wrap/wrap.tm b/examples/wrap/wrap.tm index 448ab813..c539a0ea 100644 --- a/examples/wrap/wrap.tm +++ b/examples/wrap/wrap.tm @@ -13,19 +13,19 @@ HELP := " UNICODE_HYPHEN := \{hyphen} -func unwrap(text:Text, preserve_paragraphs=yes, hyphen=UNICODE_HYPHEN -> Text): - if preserve_paragraphs: +func unwrap(text:Text, preserve_paragraphs=yes, hyphen=UNICODE_HYPHEN -> Text) + if preserve_paragraphs paragraphs := text.split($/{2+ nl}/) - if paragraphs.length > 1: + if paragraphs.length > 1 return \n\n.join([unwrap(p, hyphen=hyphen, preserve_paragraphs=no) for p in paragraphs]) return text.replace($/$(hyphen)$(\n)/, "") -func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text): - if width <= 0: +func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text) + if width <= 0 fail("Width must be a positive integer, not $width") - if 2*min_split - hyphen.length > width: + if 2*min_split - hyphen.length > width fail(" Minimum word split length ($min_split) is too small for the given wrap width ($width)! @@ -35,66 +35,66 @@ func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text): lines : @[Text] = @[] line := "" - for word in text.split($/{whitespace}/): + for word in text.split($/{whitespace}/) letters := word.split() skip if letters.length == 0 - while not _can_fit_word(line, letters, width): + while not _can_fit_word(line, letters, width) line_space := width - line.length - if line != "": line_space -= 1 + if line != "" line_space -= 1 - if min_split > 0 and line_space >= min_split + hyphen.length and letters.length >= 2*min_split: + if min_split > 0 and line_space >= min_split + hyphen.length and letters.length >= 2*min_split # Split word with a hyphen: split := line_space - hyphen.length split = split _max_ min_split split = split _min_ (letters.length - min_split) - if line != "": line ++= " " + if line != "" line ++= " " line ++= ((++: letters.to(split)) or "") ++ hyphen letters = letters.from(split + 1) - else if line == "": + else if line == "" # Force split word without hyphenation: - if line != "": line ++= " " + if line != "" line ++= " " line ++= (++: letters.to(line_space)) or "" letters = letters.from(line_space + 1) - else: + else pass # Move to next line lines.insert(line) line = "" - if letters.length > 0: - if line != "": line ++= " " + if letters.length > 0 + if line != "" line ++= " " line ++= (++: letters) or "" - if line != "": + if line != "" lines.insert(line) return \n.join(lines) -func _can_fit_word(line:Text, letters:[Text], width:Int -> Bool; inline): - if line == "": +func _can_fit_word(line:Text, letters:[Text], width:Int -> Bool; inline) + if line == "" return letters.length <= width - else: + else return line.length + 1 + letters.length <= width -func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UNICODE_HYPHEN): - if files.length == 0: +func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UNICODE_HYPHEN) + if files.length == 0 files = [(/dev/stdin)] - for file in files: + for file in files text := file.read() or exit("Could not read file: $file") - if rewrap: + if rewrap text = unwrap(text) - out := if file.is_file() and inplace: + out := if file.is_file() and inplace file - else: + else (/dev/stdout) first := yes wrapped_paragraphs : @[Text] = @[] - for paragraph in text.split($/{2+ nl}/): + for paragraph in text.split($/{2+ nl}/) wrapped_paragraphs.insert( wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen) ) |
