]> git.sesse.net Git - casparcg/blob - shell/generate_docs.cpp
* Enforce help descriptions for consumers in code.
[casparcg] / shell / generate_docs.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "included_modules.h"
23
24 #include <common/env.h>
25 #include <common/log.h>
26
27 #include <core/producer/media_info/in_memory_media_info_repository.h>
28 #include <core/help/help_repository.h>
29 #include <core/help/help_sink.h>
30 #include <core/help/util.h>
31 #include <core/producer/text/text_producer.h>
32
33 #include <protocol/amcp/amcp_command_repository.h>
34 #include <protocol/amcp/AMCPCommandsImpl.h>
35
36 #include <boost/filesystem/fstream.hpp>
37
38 #include <iostream>
39 #include <sstream>
40
41 using namespace caspar;
42
43 static const int WIDTH = 150;
44
45 class mediawiki_paragraph_builder : public core::paragraph_builder
46 {
47         std::wostringstream     out_;
48         std::wostream&          commit_to_;
49 public:
50         mediawiki_paragraph_builder(std::wostream& out)
51                 : commit_to_(out)
52         {
53         }
54
55         ~mediawiki_paragraph_builder()
56         {
57                 commit_to_ << core::wordwrap(out_.str(), WIDTH) << std::endl;
58         }
59
60         spl::shared_ptr<paragraph_builder> text(std::wstring text) override
61         {
62                 out_ << std::move(text);
63                 return shared_from_this();
64         };
65
66         spl::shared_ptr<paragraph_builder> code(std::wstring text) override
67         {
68                 out_ << L"<code>" << std::move(text) << L"</code>";
69                 return shared_from_this();
70         };
71
72         spl::shared_ptr<paragraph_builder> see(std::wstring item) override
73         {
74                 out_ << L"[[#" << item << L"|" << item << L"]]";
75                 return shared_from_this();
76         };
77
78         spl::shared_ptr<paragraph_builder> url(std::wstring url, std::wstring name) override
79         {
80                 out_ << L"[" << std::move(url) << L" " << std::move(name) << L"]";
81                 return shared_from_this();
82         };
83 };
84
85 class mediawiki_definition_list_builder : public core::definition_list_builder
86 {
87         std::wostream& out_;
88 public:
89         mediawiki_definition_list_builder(std::wostream& out)
90                 : out_(out)
91         {
92         }
93
94         ~mediawiki_definition_list_builder()
95         {
96                 out_ << std::endl;
97         }
98
99         spl::shared_ptr<definition_list_builder> item(std::wstring term, std::wstring description) override
100         {
101                 out_ << L"; <code>" << term << L"</code>\n";
102                 out_ << L": " << description << L"\n";
103
104                 return shared_from_this();
105         };
106 };
107
108 class mediawiki_help_sink : public core::help_sink
109 {
110         std::wostream& out_;
111 public:
112         mediawiki_help_sink(std::wostream& out)
113                 : out_(out)
114         {
115         }
116
117         void start_section(std::wstring title)
118         {
119                 out_ << L"=" << title << L"=\n" << std::endl;
120         }
121
122         void syntax(const std::wstring& syntax) override
123         {
124                 out_ << L"Syntax:\n";
125                 out_ << core::indent(core::wordwrap(syntax, WIDTH - 1), L" ") << std::endl;
126         }
127
128         spl::shared_ptr<core::paragraph_builder> para() override
129         {
130                 return spl::make_shared<mediawiki_paragraph_builder>(out_);
131         }
132
133         spl::shared_ptr<core::definition_list_builder> definitions() override
134         {
135                 return spl::make_shared<mediawiki_definition_list_builder>(out_);
136         }
137
138         void example(const std::wstring& code, const std::wstring& caption) override
139         {
140                 out_ << core::indent(core::wordwrap(code, WIDTH - 1), L" ");
141
142                 if (!caption.empty())
143                         out_ << core::wordwrap(L"..." + caption, WIDTH - 1);
144
145                 out_ << std::endl;
146         }
147 private:
148         void begin_item(const std::wstring& name) override 
149         {
150                 out_ << L"==" << name << L"==\n" << std::endl;
151         }
152 };
153
154 void generate_amcp_commands_help(const core::help_repository& help_repo)
155 {
156         boost::filesystem::wofstream file(L"amcp_commands_help.wiki");
157         mediawiki_help_sink sink(file);
158
159         auto print_section = [&](std::wstring title)
160         {
161                 sink.start_section(title);
162                 help_repo.help({ L"AMCP", title }, sink);
163         };
164
165         print_section(L"Basic Commands");
166         print_section(L"Data Commands");
167         print_section(L"Template Commands");
168         print_section(L"Mixer Commands");
169         print_section(L"Thumbnail Commands");
170         print_section(L"Query Commands");
171         file.flush();
172 }
173
174 void generate_producers_help(const core::help_repository& help_repo)
175 {
176         boost::filesystem::wofstream file(L"producers_help.wiki");
177         mediawiki_help_sink sink(file);
178
179         sink.start_section(L"Producers (Input Modules)");
180         help_repo.help({ L"producer" }, sink);
181
182         file.flush();
183 }
184
185 void generate_consumers_help(const core::help_repository& help_repo)
186 {
187         boost::filesystem::wofstream file(L"consumers_help.wiki");
188         mediawiki_help_sink sink(file);
189
190         sink.start_section(L"Consumers (Output Modules)");
191         help_repo.help({ L"consumer" }, sink);
192
193         file.flush();
194 }
195
196 int main(int argc, char** argv)
197 {
198         env::configure(L"casparcg.config");
199         spl::shared_ptr<core::system_info_provider_repository> system_info_provider_repo;
200         spl::shared_ptr<core::cg_producer_registry> cg_registry;
201         auto media_info_repo = core::create_in_memory_media_info_repository();
202         spl::shared_ptr<core::help_repository> help_repo;
203         auto producer_registry = spl::make_shared<core::frame_producer_registry>(help_repo);
204         auto consumer_registry = spl::make_shared<core::frame_consumer_registry>(help_repo);
205         std::promise<bool> shutdown_server_now;
206         protocol::amcp::amcp_command_repository repo(
207                         { },
208                         nullptr,
209                         media_info_repo,
210                         system_info_provider_repo,
211                         cg_registry,
212                         help_repo,
213                         producer_registry,
214                         consumer_registry,
215                         shutdown_server_now);
216
217         protocol::amcp::register_commands(repo);
218
219         core::module_dependencies dependencies(system_info_provider_repo, cg_registry, media_info_repo, producer_registry, consumer_registry);
220         initialize_modules(dependencies);
221         core::text::init(dependencies);
222
223         generate_amcp_commands_help(*help_repo);
224         generate_producers_help(*help_repo);
225         generate_consumers_help(*help_repo);
226
227         uninitialize_modules();
228         
229         return 0;
230 }