550: def format_one_paragraph(text = nil)
551: text ||= @text
552: text = text[0] if text.kind_of?(Array)
553:
554:
555: words = text.split(SPACES_RE).reverse.reject { |ww| ww.nil? or ww.empty? }
556:
557: text = []
558:
559:
560:
561:
562:
563:
564: max_line_width = @columns - @first_indent - @left_margin - @right_margin
565: indent_str = ' ' * @first_indent
566:
567: first_line = true
568:
569: if words.empty?
570: line = []
571: line_size = 0
572: extra_space = false
573: else
574: line = [ words.pop ]
575: line_size = line[-1].size
576: extra_space = __add_extra_space?(line[-1])
577: end
578:
579: while next_word = words.pop
580: next_word.strip! unless next_word.nil?
581: new_line_size = (next_word.size + line_size) + 1
582:
583: if extra_space
584: if (line[-1] !~ __sentence_end_re)
585: extra_space = false
586: end
587: end
588:
589:
590:
591: new_line_size += 1 if extra_space
592:
593:
594:
595:
596: if new_line_size <= max_line_width
597: if line.empty?
598: line << next_word
599: else
600: if extra_space
601: line << " #{next_word}"
602: else
603: line << " #{next_word}"
604: end
605: end
606: else
607:
608:
609:
610: line, next_word = __wrap_line(line, next_word) if @nobreak
611: if next_word.kind_of?(Array)
612: if next_word.size > 1
613: words.push(*(next_word.reverse))
614: next_word = words.pop
615: else
616: next_word = next_word[0]
617: end
618: next_word.strip! unless next_word.nil?
619: end
620:
621:
622:
623:
624:
625:
626:
627: line, new_line_size, next_word = __hyphenate(line, line_size, next_word, max_line_width)
628: if next_word.kind_of?(Array)
629: if next_word.size > 1
630: words.push(*(next_word.reverse))
631: next_word = words.pop
632: else
633: next_word = next_word[0]
634: end
635: next_word.strip! unless next_word.nil?
636: end
637:
638: text << __make_line(line, indent_str, max_line_width, next_word.nil?) unless line.nil?
639:
640: if first_line
641: first_line = false
642: max_line_width = @columns - @body_indent - @left_margin - @right_margin
643: indent_str = ' ' * @body_indent
644: end
645:
646: if next_word.nil?
647: line = []
648: new_line_size = 0
649: else
650: line = [ next_word ]
651: new_line_size = next_word.size
652: end
653: end
654:
655: line_size = new_line_size
656: extra_space = __add_extra_space?(next_word) unless next_word.nil?
657: end
658:
659: loop do
660: break if line.nil? or line.empty?
661: line, line_size, ww = __hyphenate(line, line_size, ww, max_line_width)
662: text << __make_line(line, indent_str, max_line_width, ww.nil?)
663: line = ww
664: ww = nil
665: end
666:
667: if (@tag_paragraph and (not text.empty?))
668: if @tag_cur.nil? or @tag_cur.empty?
669: @tag_cur = @tag_text[0]
670: end
671:
672: fchar = /(\S)/o.match(text[0])[1]
673: white = text[0].index(fchar)
674:
675: unless @tag_cur.nil?
676: if ((white - @left_margin - 1) > @tag_cur.size) then
677: white = @tag_cur.size + @left_margin
678: text[0].gsub!(/^ {#{white}}/, "#{' ' * @left_margin}#{@tag_cur}")
679: else
680: text.unshift("#{' ' * @left_margin}#{@tag_cur}\n")
681: end
682: end
683: end
684:
685: text.join('')
686: end