Created
August 19, 2022 22:06
-
-
Save brasic/041ee43b79a45819b395b389a649cb80 to your computer and use it in GitHub Desktop.
wrap long text to 72 chars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def wrap(text) | |
result = [] | |
fenced = false | |
text.split(/\r?\n/).each do |line| | |
if line.start_with?("```") | |
fenced = !fenced | |
end | |
if fenced || line.size <= 72 | |
result << line | |
else | |
buf = "" | |
line.split.each do |word| | |
if (buf.size + word.size) > 71 # 72 with space | |
result << buf | |
buf = word | |
else | |
buf << " " unless buf.empty? | |
buf << word | |
end | |
end | |
result << buf unless buf.empty? | |
end | |
end | |
result.join("\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment