Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/gn/standard_out.cc

Issue 1024783002: Add a --markdown flag to GN to be able to output help in markdown. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn_all_help
Patch Set: fix bold/italic formatting Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/gn/standard_out.h" 5 #include "tools/gn/standard_out.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 11 matching lines...) Expand all
22 namespace { 22 namespace {
23 23
24 bool initialized = false; 24 bool initialized = false;
25 25
26 #if defined(OS_WIN) 26 #if defined(OS_WIN)
27 HANDLE hstdout; 27 HANDLE hstdout;
28 WORD default_attributes; 28 WORD default_attributes;
29 #endif 29 #endif
30 bool is_console = false; 30 bool is_console = false;
31 31
32 bool is_markdown = false;
33
32 void EnsureInitialized() { 34 void EnsureInitialized() {
33 if (initialized) 35 if (initialized)
34 return; 36 return;
35 initialized = true; 37 initialized = true;
36 38
37 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); 39 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
40 if (cmdline->HasSwitch(switches::kMarkdown)) {
41 // output help in Markdown's syntax, not color-highlighted.
42 is_markdown = true;
43 }
44
38 if (cmdline->HasSwitch(switches::kNoColor)) { 45 if (cmdline->HasSwitch(switches::kNoColor)) {
39 // Force color off. 46 // Force color off.
40 is_console = false; 47 is_console = false;
41 return; 48 return;
42 } 49 }
43 50
44 #if defined(OS_WIN) 51 #if defined(OS_WIN)
45 // On Windows, we can't force the color on. If the output handle isn't a 52 // On Windows, we can't force the color on. If the output handle isn't a
46 // console, there's nothing we can do about it. 53 // console, there's nothing we can do about it.
47 hstdout = ::GetStdHandle(STD_OUTPUT_HANDLE); 54 hstdout = ::GetStdHandle(STD_OUTPUT_HANDLE);
48 CONSOLE_SCREEN_BUFFER_INFO info; 55 CONSOLE_SCREEN_BUFFER_INFO info;
49 is_console = !!::GetConsoleScreenBufferInfo(hstdout, &info); 56 is_console = !!::GetConsoleScreenBufferInfo(hstdout, &info);
50 default_attributes = info.wAttributes; 57 default_attributes = info.wAttributes;
51 #else 58 #else
52 if (cmdline->HasSwitch(switches::kColor)) 59 if (cmdline->HasSwitch(switches::kColor))
53 is_console = true; 60 is_console = true;
54 else 61 else
55 is_console = isatty(fileno(stdout)); 62 is_console = isatty(fileno(stdout));
56 #endif 63 #endif
57 } 64 }
58 65
59 void WriteToStdOut(const std::string& output) { 66 void WriteToStdOut(const std::string& output) {
60 size_t written_bytes = fwrite(output.data(), 1, output.size(), stdout); 67 size_t written_bytes = fwrite(output.data(), 1, output.size(), stdout);
61 DCHECK_EQ(output.size(), written_bytes); 68 DCHECK_EQ(output.size(), written_bytes);
62 } 69 }
63
64 } // namespace 70 } // namespace
65 71
66 #if defined(OS_WIN) 72 #if defined(OS_WIN)
67 73
68 void OutputString(const std::string& output, TextDecoration dec) { 74 void OutputString(const std::string& output, TextDecoration dec) {
69 EnsureInitialized(); 75 EnsureInitialized();
70 if (is_console) { 76 DWORD written = 0;
77
78 if (is_markdown) {
79 // The markdown rendering turns "dim" text to italics and any
brettw 2015/04/17 00:01:53 I think it would be better to replace this with:
Dirk Pranke 2015/04/17 00:08:15 Acknowledged.
80 // other colored text to bold.
81 if (dec == DECORATION_DIM)
82 ::WriteFile(hstdout, "*", 1), &written, nullptr);
83 else if (dec != DECORATION_NONE)
84 ::WriteFile(hstdout, "**", 2), &written, nullptr);
85 } else if (is_console) {
71 switch (dec) { 86 switch (dec) {
72 case DECORATION_NONE: 87 case DECORATION_NONE:
73 break; 88 break;
74 case DECORATION_DIM: 89 case DECORATION_DIM:
75 ::SetConsoleTextAttribute(hstdout, FOREGROUND_INTENSITY); 90 ::SetConsoleTextAttribute(hstdout, FOREGROUND_INTENSITY);
76 break; 91 break;
77 case DECORATION_RED: 92 case DECORATION_RED:
78 ::SetConsoleTextAttribute(hstdout, 93 ::SetConsoleTextAttribute(hstdout,
79 FOREGROUND_RED | FOREGROUND_INTENSITY); 94 FOREGROUND_RED | FOREGROUND_INTENSITY);
80 break; 95 break;
81 case DECORATION_GREEN: 96 case DECORATION_GREEN:
82 // Keep green non-bold. 97 // Keep green non-bold.
83 ::SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN); 98 ::SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
84 break; 99 break;
85 case DECORATION_BLUE: 100 case DECORATION_BLUE:
86 ::SetConsoleTextAttribute(hstdout, 101 ::SetConsoleTextAttribute(hstdout,
87 FOREGROUND_BLUE | FOREGROUND_INTENSITY); 102 FOREGROUND_BLUE | FOREGROUND_INTENSITY);
88 break; 103 break;
89 case DECORATION_YELLOW: 104 case DECORATION_YELLOW:
90 ::SetConsoleTextAttribute(hstdout, 105 ::SetConsoleTextAttribute(hstdout,
91 FOREGROUND_RED | FOREGROUND_GREEN); 106 FOREGROUND_RED | FOREGROUND_GREEN);
92 break; 107 break;
93 } 108 }
94 } 109 }
95 110
96 DWORD written = 0;
97 ::WriteFile(hstdout, output.c_str(), static_cast<DWORD>(output.size()), 111 ::WriteFile(hstdout, output.c_str(), static_cast<DWORD>(output.size()),
98 &written, nullptr); 112 &written, nullptr);
99 113
100 if (is_console) 114 if (is_markdown) {
115 if (dec == DECORATION_DIM)
116 ::WriteFile(hstdout, "*", 1), &written, nullptr);
117 else if (dec != DECORATION_NONE)
118 ::WriteFile(hstdout, "**", 2), &written, nullptr);
119 } else if (is_console) {
101 ::SetConsoleTextAttribute(hstdout, default_attributes); 120 ::SetConsoleTextAttribute(hstdout, default_attributes);
121 }
102 } 122 }
103 123
104 #else 124 #else
105 125
106 void OutputString(const std::string& output, TextDecoration dec) { 126 void OutputString(const std::string& output, TextDecoration dec) {
107 EnsureInitialized(); 127 EnsureInitialized();
108 if (is_console) { 128 if (is_markdown) {
129 // The markdown rendering turns "dim" text to italics and any
130 // other colored text to bold.
131 if (dec == DECORATION_DIM)
132 WriteToStdOut("*");
133 else if (dec != DECORATION_NONE)
134 WriteToStdOut("**");
135 } else if (is_console) {
109 switch (dec) { 136 switch (dec) {
110 case DECORATION_NONE: 137 case DECORATION_NONE:
111 break; 138 break;
112 case DECORATION_DIM: 139 case DECORATION_DIM:
113 WriteToStdOut("\e[2m"); 140 WriteToStdOut("\e[2m");
114 break; 141 break;
115 case DECORATION_RED: 142 case DECORATION_RED:
116 WriteToStdOut("\e[31m\e[1m"); 143 WriteToStdOut("\e[31m\e[1m");
117 break; 144 break;
118 case DECORATION_GREEN: 145 case DECORATION_GREEN:
119 WriteToStdOut("\e[32m"); 146 WriteToStdOut("\e[32m");
120 break; 147 break;
121 case DECORATION_BLUE: 148 case DECORATION_BLUE:
122 WriteToStdOut("\e[34m\e[1m"); 149 WriteToStdOut("\e[34m\e[1m");
123 break; 150 break;
124 case DECORATION_YELLOW: 151 case DECORATION_YELLOW:
125 WriteToStdOut("\e[33m\e[1m"); 152 WriteToStdOut("\e[33m\e[1m");
126 break; 153 break;
127 } 154 }
128 } 155 }
129 156
130 WriteToStdOut(output.data()); 157 WriteToStdOut(output.data());
131 158
132 if (is_console && dec != DECORATION_NONE) 159 if (is_markdown) {
160 if (dec == DECORATION_DIM)
161 WriteToStdOut("*");
162 else if (dec != DECORATION_NONE)
163 WriteToStdOut("**");
164 } else if (is_console && dec != DECORATION_NONE) {
133 WriteToStdOut("\e[0m"); 165 WriteToStdOut("\e[0m");
166 }
134 } 167 }
135 168
136 #endif 169 #endif
137 170
138 void PrintShortHelp(const std::string& line) { 171 void PrintShortHelp(const std::string& line) {
172 EnsureInitialized();
173
139 size_t colon_offset = line.find(':'); 174 size_t colon_offset = line.find(':');
140 size_t first_normal = 0; 175 size_t first_normal = 0;
141 if (colon_offset != std::string::npos) { 176 if (colon_offset != std::string::npos) {
142 OutputString(" " + line.substr(0, colon_offset), DECORATION_YELLOW); 177 OutputString(" " + line.substr(0, colon_offset), DECORATION_YELLOW);
143 first_normal = colon_offset; 178 first_normal = colon_offset;
144 } 179 }
145 180
146 // See if the colon is followed by a " [" and if so, dim the contents of [ ]. 181 // See if the colon is followed by a " [" and if so, dim the contents of [ ].
147 if (first_normal > 0 && 182 if (first_normal > 0 &&
148 line.size() > first_normal + 2 && 183 line.size() > first_normal + 2 &&
149 line[first_normal + 1] == ' ' && line[first_normal + 2] == '[') { 184 line[first_normal + 1] == ' ' && line[first_normal + 2] == '[') {
150 size_t begin_bracket = first_normal + 2; 185 size_t begin_bracket = first_normal + 2;
151 OutputString(": "); 186 OutputString(": ");
152 first_normal = line.find(']', begin_bracket); 187 first_normal = line.find(']', begin_bracket);
153 if (first_normal == std::string::npos) 188 if (first_normal == std::string::npos)
154 first_normal = line.size(); 189 first_normal = line.size();
155 else 190 else
156 first_normal++; 191 first_normal++;
157 OutputString(line.substr(begin_bracket, first_normal - begin_bracket), 192 OutputString(line.substr(begin_bracket, first_normal - begin_bracket),
158 DECORATION_DIM); 193 DECORATION_DIM);
159 } 194 }
160 195
161 OutputString(line.substr(first_normal) + "\n"); 196 OutputString(line.substr(first_normal) + "\n");
162 } 197 }
163 198
164 void PrintLongHelp(const std::string& text) { 199 void PrintLongHelp(const std::string& text) {
200 EnsureInitialized();
201
165 std::vector<std::string> lines; 202 std::vector<std::string> lines;
166 base::SplitStringDontTrim(text, '\n', &lines); 203 base::SplitStringDontTrim(text, '\n', &lines);
167 204
205 bool first_header = true;
206 bool in_body = false;
168 for (const auto& line : lines) { 207 for (const auto& line : lines) {
169 // Check for a heading line. 208 // Check for a heading line.
170 if (!line.empty() && line[0] != ' ') { 209 if (!line.empty() && line[0] != ' ') {
210 if (is_markdown) {
211 // GN's block-level formatting is converted to markdown as follows:
212 // * The first heading is treated as an H2.
213 // * Subsequent heading are treated as H3s.
214 // * Any other text is wrapped in a code block and displayed as-is.
215 //
216 // Span-level formatting (the decorations) is converted inside
217 // OutputString().
218 if (in_body) {
219 OutputString("```\n\n", DECORATION_NONE);
220 in_body = false;
221 }
222
223 if (first_header) {
224 OutputString("## ", DECORATION_NONE);
225 first_header = false;
226 } else {
227 OutputString("### ", DECORATION_NONE);
228 }
229 }
230
171 // Highlight up to the colon (if any). 231 // Highlight up to the colon (if any).
172 size_t chars_to_highlight = line.find(':'); 232 size_t chars_to_highlight = line.find(':');
173 if (chars_to_highlight == std::string::npos) 233 if (chars_to_highlight == std::string::npos)
174 chars_to_highlight = line.size(); 234 chars_to_highlight = line.size();
235
175 OutputString(line.substr(0, chars_to_highlight), DECORATION_YELLOW); 236 OutputString(line.substr(0, chars_to_highlight), DECORATION_YELLOW);
176 OutputString(line.substr(chars_to_highlight) + "\n"); 237 OutputString(line.substr(chars_to_highlight) + "\n");
177 continue; 238 continue;
239 } else if (!line.empty() && !in_body) {
240 OutputString("```\n", DECORATION_NONE);
241 in_body = true;
178 } 242 }
179 243
180 // Check for a comment. 244 // Check for a comment.
181 TextDecoration dec = DECORATION_NONE; 245 TextDecoration dec = DECORATION_NONE;
182 for (const auto& elem : line) { 246 for (const auto& elem : line) {
183 if (elem == '#') { 247 if (elem == '#') {
184 // Got a comment, draw dimmed. 248 // Got a comment, draw dimmed.
185 dec = DECORATION_DIM; 249 dec = DECORATION_DIM;
186 break; 250 break;
187 } else if (elem != ' ') { 251 } else if (elem != ' ') {
188 break; 252 break;
189 } 253 }
190 } 254 }
191 255
192 OutputString(line + "\n", dec); 256 OutputString(line + "\n", dec);
193 } 257 }
258
259 if (is_markdown && in_body) {
brettw 2015/04/17 00:01:53 No {}
Dirk Pranke 2015/04/17 00:08:16 Acknowledged.
260 OutputString("\n```\n");
261 }
194 } 262 }
195 263
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698