# File lib/text/format.rb, line 550
550:   def format_one_paragraph(text = nil)
551:     text ||= @text
552:     text = text[0] if text.kind_of?(Array)
553: 
554:       # Convert the provided paragraph to a list of words.
555:     words = text.split(SPACES_RE).reverse.reject { |ww| ww.nil? or ww.empty? }
556: 
557:     text = []
558: 
559:       # Find the maximum line width and the initial indent string.
560:       # TODO 20050114 - allow the left and right margins to be specified as
561:       # strings. If they are strings, then we need to use the sizes of the
562:       # strings. Also: allow the indent string to be set manually and
563:       # indicate whether the indent string will have a following space.
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:         # Increase the width of the new line if there's a sentence
590:         # terminator and we are applying extra_space.
591:       new_line_size += 1 if extra_space
592: 
593:         # Will the word fit onto the current line? If so, simply append it
594:         # to the end of the line.
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:           # Forcibly wrap the line if nonbreaking spaces are turned on and
608:           # there is a condition where words must be wrapped. If we have
609:           # returned more than one word, readjust the word list.
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:           # Check to see if the line needs to be hyphenated. If a word has a
622:           # hyphen in it (e.g., "fixed-width"), then we can ALWAYS wrap at
623:           # that hyphenation, even if #hard_margins is not turned on. More
624:           # elaborate forms of hyphenation will only be performed if
625:           # #hard_margins is turned on. If we have returned more than one
626:           # word, readjust the word list.
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)#if @hard_margins
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