packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmDocumentationFormatterText.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12
13 #include "cmDocumentationFormatterText.h"
14 #include "cmDocumentationSection.h"
15
16 cmDocumentationFormatterText::cmDocumentationFormatterText()
17 :cmDocumentationFormatter()
18 ,TextWidth(77)
19 ,TextIndent("")
20 {
21 }
22
23 void cmDocumentationFormatterText
24 ::PrintSection(std::ostream& os,
25                const cmDocumentationSection &section,
26                const char* name)
27 {
28   if(name && (strcmp(name, "SingleItem")!=0))
29     {
30     os <<
31       "---------------------------------------"
32       "---------------------------------------\n";
33     os << name << "\n\n";
34     }
35
36   const std::vector<cmDocumentationEntry> &entries =
37     section.GetEntries();
38   for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
39       op != entries.end(); ++op)
40     {
41     if(op->Name.size())
42       {
43       os << "  " << op->Name << "\n";
44       this->TextIndent = "       ";
45       this->PrintFormatted(os, op->Brief.c_str());
46       if(op->Full.size())
47         {
48         os << "\n";
49         this->PrintFormatted(os, op->Full.c_str());
50         }
51       }
52     else
53       {
54       this->TextIndent = "";
55       this->PrintFormatted(os, op->Brief.c_str());
56       }
57     os << "\n";
58     }
59 }
60
61 void cmDocumentationFormatterText::PrintPreformatted(std::ostream& os,
62                                                      const char* text)
63 {
64   bool newline = true;
65   for(const char* ptr = text; *ptr; ++ptr)
66     {
67     if(newline && *ptr != '\n')
68       {
69       os << this->TextIndent;
70       newline = false;
71       }
72     os << *ptr;
73     if(*ptr == '\n')
74       {
75       newline = true;
76       }
77     }
78   os << "\n";
79 }
80
81 void cmDocumentationFormatterText::PrintParagraph(std::ostream& os,
82                                                   const char* text)
83 {
84   os << this->TextIndent;
85   this->PrintColumn(os, text);
86   os << "\n";
87 }
88
89 void cmDocumentationFormatterText::SetIndent(const char* indent)
90 {
91   this->TextIndent = indent;
92 }
93
94 void cmDocumentationFormatterText::PrintColumn(std::ostream& os,
95                                                const char* text)
96 {
97   // Print text arranged in an indented column of fixed witdh.
98   const char* l = text;
99   long column = 0;
100   bool newSentence = false;
101   bool firstLine = true;
102   int width = this->TextWidth - static_cast<int>(strlen(this->TextIndent));
103
104   // Loop until the end of the text.
105   while(*l)
106     {
107     // Parse the next word.
108     const char* r = l;
109     while(*r && (*r != '\n') && (*r != ' ')) { ++r; }
110
111     // Does it fit on this line?
112     if(r-l < (width-column-(newSentence?1:0)))
113       {
114       // Word fits on this line.
115       if(r > l)
116         {
117         if(column)
118           {
119           // Not first word on line.  Separate from the previous word
120           // by a space, or two if this is a new sentence.
121           if(newSentence)
122             {
123             os << "  ";
124             column += 2;
125             }
126           else
127             {
128             os << " ";
129             column += 1;
130             }
131           }
132         else
133           {
134           // First word on line.  Print indentation unless this is the
135           // first line.
136           os << (firstLine?"":this->TextIndent);
137           }
138
139         // Print the word.
140         os.write(l, static_cast<long>(r-l));
141         newSentence = (*(r-1) == '.');
142         }
143
144       if(*r == '\n')
145         {
146         // Text provided a newline.  Start a new line.
147         os << "\n";
148         ++r;
149         column = 0;
150         firstLine = false;
151         }
152       else
153         {
154         // No provided newline.  Continue this line.
155         column += static_cast<long>(r-l);
156         }
157       }
158     else
159       {
160       // Word does not fit on this line.  Start a new line.
161       os << "\n";
162       firstLine = false;
163       if(r > l)
164         {
165         os << this->TextIndent;
166         os.write(l, static_cast<long>(r-l));
167         column = static_cast<long>(r-l);
168         newSentence = (*(r-1) == '.');
169         }
170       else
171         {
172         column = 0;
173         }
174       }
175
176     // Move to beginning of next word.  Skip over whitespace.
177     l = r;
178     while(*l && (*l == ' ')) { ++l; }
179     }
180 }