(2023-04-04) To wrap or not to wrap? (just my opinion) ------------------------------------------------------ DO NOT hard-wrap your text if you're serving or creating: * program code or markup documents of any sort * configuration or log files of any sort * technical documentation of any sort * generally any text content that's not specifically designed to be only read via Gopher DO hard-wrap (and optionally decorate) your text if you're: * generating Gophermap infolines * writing an article-like post or an essay * writing an information digest or an e-zine * generally creating any content to be exclusively read via Gopher by living humans in its entirety, that's not meant for copying, parsing or processing I felt the need to publish my own opinion on the topic after having read two opposite writeups: Pro-hard-wrap (by Wandering Geek): gopher://gopher.unixlore.net/0/glog/gopher-annoyances.md Anti-hard-wrap (by Magical Fish): gopher://magical.fish:70/0/phlog/thoughts-on-text-formatting Also, I have developed some algorithms to wrap soft-wrapped texts and to unwrap hard-wrapped texts. ### The Phlow algorithm ### This algorithm reflows a line to fit the target page width with no word breaks. 1. Accept the line L and target page width W as parameters. 2. Get the line length LL: LL = len(L). 3. If W is 0 or LL < W, emit L and quit. 4. Allocate a variable LWS to track the last whitespace position. Set to 0. 5. Allocate a variable CPOS to track the current relative position. Set to 0. 6. Allocate a variable BPOS to track the current base position. Set to 0. 7. Allocate an empty output string buffer OUT. 8. For every index I from 0 to LL, perform the steps 9 to 19: 9. If the CPOS value is less than W, go to step 10, otherwise go to step 13. 10. Fetch the current character C from the line L at position I. 11. If C is a whitespace character (0x20), set the LWS value to CPOS. 12. Append the value of C to the output buffer OUT. Go to step 19. 13. If LWS value is 0, set it to W. 14. Emit the value of the output buffer OUT truncated to LWS characters. 15. Empty the output buffer OUT. 16. Set BPOS to BPOS + LWS. 17. Set CPOS and LWS to 0. 18. Set I to BPOS. 19. Increment CPOS. End of iteration. 20. If the output buffer OUT is not empty, emit its value. End of algorithm. > Y U NO use fmt? Thanks for asking. First, this is an external dependency on GNU coreutils, and the main conceptual objective of Bopher-NG is to have as few external dependencies as possible. Second, fmt messes up lists, treating everything not separated by two newlines as a paragraph. The -s parameter of fmt solves this problem but look where it wraps a line if it was started with some whitespaces. I don't like tools that try to appear smarter than the user, and especially those that utterly fail to. ### The Unphlow algorithm ### This algorithm removes all hard breaks from the text. The only limitation is that input paragraphs must be separated with (visually) empty lines. Kind of what fmt does without the -s parameter. 1. Read the input text into an array A of lines, separating them by LF (line feed, 0x0a) character. 2. Allocate an empty output string buffer BUF and an empty output string array OUT. 3. For each line L in the array A, perform the steps 4 to 10: 4. Remove from the line L all leading and trailing occurrences of the following characters: whitespace (0x20), TAB (0x09), CR (carriage return, 0x0d). 5. If the line L is empty, go to step 6, otherwise go to step 10. 6. Remove all trailing occurrences of the whitespace character (0x20) from the buffer BUF. 7. Append the value of buffer BUF to the array OUT. 8. Empty the buffer BUF. 9. Append an empty string to the array OUT. End of iteration. 10. Append the value of L and a whitespace character (0x20) to the buffer BUF. End of iteration. 11. If the buffer BUF is not empty, perform the operations described in steps 6 and 7. 12. (Optional step) For each line OL in the OUT array, replace all sets of consecutive whitespace characters listed in step 4 with a single whitespace (0x20). 13. Emit the array OUT. End of algorithm. I hope my reasoning and algos finally will help solve this long-standing debate. --- Luxferre ---