Skip to content

Instantly share code, notes, and snippets.

@brasic
Created August 19, 2022 22:06
Show Gist options
  • Save brasic/041ee43b79a45819b395b389a649cb80 to your computer and use it in GitHub Desktop.
Save brasic/041ee43b79a45819b395b389a649cb80 to your computer and use it in GitHub Desktop.
wrap long text to 72 chars
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