Module: Net::Text::Reflow
- Defined in:
- lib/net/text/reflow.rb
Overview
Contains helper methods to correctly display texts with long lines.
This module expect given text to be Gemtext inspired (i.e. links prefixed with => and “‘ delimitting code blocks).
Class Method Summary collapse
- .format_body(body, length) ⇒ Object
- .parse_line(line, mono_block_open, length) ⇒ Object
- .reflow_line(line, length) ⇒ Object
- .reflow_line_prefix(line) ⇒ Object
Class Method Details
.format_body(body, length) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/net/text/reflow.rb', line 50 def self.format_body(body, length) new_body = [] mono_block_open = false body.each_line do |line| mono_block_open, content = parse_line line, mono_block_open, length new_body += content end new_body.join("\n") end |
.parse_line(line, mono_block_open, length) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/net/text/reflow.rb', line 34 def self.parse_line(line, mono_block_open, length) if line.start_with?('```') mono_block_open = !mono_block_open return [mono_block_open, [line.chomp]] end return [mono_block_open, [line.chomp]] if mono_block_open line.strip! if line.start_with?('=>') || line.length < length return [mono_block_open, [line]] end [mono_block_open, reflow_line(line, length)] end |
.reflow_line(line, length) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/net/text/reflow.rb', line 19 def self.reflow_line(line, length) output = [] prefix = reflow_line_prefix(line) limit_chars = ['-', '', ' '].freeze while line.length > length # Detect first possible cut cut_index = limit_chars.map { line[0...length].rindex(_1) || -1 }.max break if cut_index.zero? # Better do nothing for now output << line[0...cut_index] line = prefix + line[(cut_index + 1)..] end output << line end |
.reflow_line_prefix(line) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/net/text/reflow.rb', line 10 def self.reflow_line_prefix(line) m = line.match(/\A([*#>]+ )/) return '' unless m # Each quote line should begin with the quote mark return m[1] if m[1].start_with?('>') ' ' * m[1].length end |