Merge branch release-5-1
[alexxy/gromacs.git] / src / gromacs / onlinehelp / helpwritercontext.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares gmx::HelpWriterContext.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_onlinehelp
42  */
43 #ifndef GMX_ONLINEHELP_HELPWRITERCONTEXT_H
44 #define GMX_ONLINEHELP_HELPWRITERCONTEXT_H
45
46 #include <string>
47 #include <vector>
48
49 #include "gromacs/utility/classhelpers.h"
50
51 namespace gmx
52 {
53
54 class TextLineWrapperSettings;
55 class TextOutputStream;
56 class TextWriter;
57
58 /*! \cond libapi */
59 //! \libinternal Output format for help writing.
60 enum HelpOutputFormat
61 {
62     eHelpOutputFormat_Console,  //!< Plain text directly on the console.
63     eHelpOutputFormat_Rst,      //!< reStructuredText for online manual and man pages.
64     eHelpOutputFormat_Other,    //!< Used for extensions in other modules.
65     eHelpOutputFormat_NR        //!< Used for the number of output formats.
66 };
67 //! \endcond
68
69 /*! \libinternal \brief
70  * Hyperlink data for writing out help.
71  *
72  * This class is separate from HelpWriterContext to allow constructing the list
73  * of links once and reusing them across multiple help writer contexts.
74  * This is used when exporting all the help from the wrapper binary to avoid
75  * repeatedly constructing the same data structure for each help item.
76  *
77  * While the links are in principle independent of the output format, the
78  * constructor takes the output format to be able to preformat the links,
79  * avoiding repeated processing during markup substitution.  Could be hidden
80  * behind the scenes in HelpWriterContext, but that would complicate the
81  * implementation.
82  *
83  * \ingroup module_onlinehelp
84  */
85 class HelpLinks
86 {
87     public:
88         /*! \brief
89          * Initializes an empty links collection for the given output format.
90          */
91         explicit HelpLinks(HelpOutputFormat format);
92         ~HelpLinks();
93
94         /*! \brief
95          * Adds a link.
96          *
97          * \param[in] linkName     Name of the link in input text.
98          * \param[in] targetName   Hyperlink target.
99          * \param[in] displayName  Text to show as the link.
100          *
101          * Any occurrence of \p linkName in the text passed to markup
102          * substitution methods in HelpWriterContext is made into a hyperlink
103          * to \p targetName if the markup format supports that.
104          */
105         void addLink(const std::string &linkName,
106                      const std::string &targetName,
107                      const std::string &displayName);
108
109     private:
110         class Impl;
111
112         PrivateImplPointer<Impl> impl_;
113
114         //! Allows the context to use the links.
115         friend class HelpWriterContext;
116 };
117
118 /*! \libinternal \brief
119  * Context information for writing out help.
120  *
121  * The purpose of this class is to pass information about the output format to
122  * methods that write help, and to abstract away most of the details of
123  * different output formats.
124  *
125  * The state of a context object (excluding the fact that the output file is
126  * written to) does not change after initial construction of the object.
127  * Copying creates a context objects that share state with the source.
128  *
129  * \inlibraryapi
130  * \ingroup module_onlinehelp
131  */
132 class HelpWriterContext
133 {
134     public:
135         /*! \brief
136          * Initializes a context with the given output stream and format.
137          *
138          * \throws std::bad_alloc if out of memory.
139          */
140         HelpWriterContext(TextOutputStream *stream, HelpOutputFormat format);
141         /*! \brief
142          * Initializes a context with the given output stream, format and links.
143          *
144          * \throws std::bad_alloc if out of memory.
145          *
146          * A reference to \p links is stored until the HelpWriterContext
147          * is destructed.  The caller is responsible for ensuring that the
148          * links object remains valid long enough.
149          */
150         HelpWriterContext(TextOutputStream *stream, HelpOutputFormat format,
151                           const HelpLinks *links);
152         //! Creates a copy of the context.
153         HelpWriterContext(const HelpWriterContext &other);
154         ~HelpWriterContext();
155
156         /*! \brief
157          * Adds a string replacement for markup subsitution.
158          *
159          * \param[in] search   Text to replace in input.
160          * \param[in] replace  Text that each occurrence of \p search is
161          *     replaced with.
162          * \throws std::bad_alloc if out of memory.
163          *
164          * \todo
165          * Improve semantics if the same \p search item is set multiple
166          * times.
167          */
168         void setReplacement(const std::string &search,
169                             const std::string &replace);
170
171         /*! \brief
172          * Returns the active output format.
173          *
174          * Does not throw.
175          */
176         HelpOutputFormat outputFormat() const;
177         /*! \brief
178          * Returns the raw writer for writing the help.
179          *
180          * Using this writer directly should be avoided, as it requires one to
181          * have different code for each output format.
182          * Using other methods in this class should be preferred.
183          *
184          * Does not throw.
185          */
186         TextWriter &outputFile() const;
187
188         /*! \brief
189          * Creates a subsection in the output help.
190          *
191          * \param[in] title  Title for the subsection.
192          * \throws    std::bad_alloc if out of memory.
193          * \throws    FileIOError on any I/O error.
194          *
195          * Writes \p title using writeTitle() and makes any further
196          * writeTitle() calls write headings one level deeper.
197          *
198          * Typical use for writing a subsection is to create a copy of the
199          * context for the parent section, and then call enterSubSection() on
200          * the copy.
201          * The whole subsection should be written out using the returned
202          * context before calling any further methods in the parent context.
203          *
204          * This method is only necessary if the subsection will contain further
205          * subsections.  If there is only one level of subsections, it is
206          * possible to use writeTitle() directly.
207          */
208         void enterSubSection(const std::string &title);
209
210         /*! \brief
211          * Substitutes markup used in help text and wraps lines.
212          *
213          * \param[in] settings Line wrapper settings.
214          * \param[in] text     Text to substitute.
215          * \returns   \p text with markup substituted and wrapped.
216          * \throws    std::bad_alloc if out of memory.
217          *
218          * \see TextLineWrapper::wrapToString()
219          */
220         std::string
221         substituteMarkupAndWrapToString(const TextLineWrapperSettings &settings,
222                                         const std::string             &text) const;
223         /*! \brief
224          * Substitutes markup used in help text and wraps lines.
225          *
226          * \param[in] settings Line wrapper settings.
227          * \param[in] text     Text to substitute.
228          * \returns   \p text with markup substituted and wrapped as a list of
229          *      lines.
230          * \throws    std::bad_alloc if out of memory.
231          *
232          * \see TextLineWrapper::wrapToVector()
233          */
234         std::vector<std::string>
235         substituteMarkupAndWrapToVector(const TextLineWrapperSettings &settings,
236                                         const std::string             &text) const;
237         /*! \brief
238          * Writes a title for the current help topic.
239          *
240          * \param[in] title  Title to write.
241          * \throws    std::bad_alloc if out of memory.
242          * \throws    FileIOError on any I/O error.
243          */
244         void writeTitle(const std::string &title) const;
245         /*! \brief
246          * Writes a formatted text block into the output.
247          *
248          * \param[in] text  Text to format.
249          * \throws    std::bad_alloc if out of memory.
250          * \throws    FileIOError on any I/O error.
251          *
252          * Convenience function that calls substituteMarkupAndWrapToString()
253          * and writes the result directly to the output file.
254          */
255         void writeTextBlock(const std::string &text) const;
256         /*! \brief
257          * Starts writing a list of options.
258          *
259          * Prints any necessary headers for a list of options formatted with
260          * writeOptionItem().
261          */
262         void writeOptionListStart() const;
263         /*! \brief
264          * Writes an entry for a single option into the output.
265          *
266          * \param[in] name  Name of the option.
267          * \param[in] value        Placeholder for option value.
268          * \param[in] defaultValue Default value for the option.
269          * \param[in] info         Additional (brief) info/attributes for the
270          *      option.
271          * \param[in] description  Full description of the option.
272          */
273         void writeOptionItem(
274             const std::string &name, const std::string &value,
275             const std::string &defaultValue, const std::string &info,
276             const std::string &description) const;
277         /*! \brief
278          * Finishes writing a list of options.
279          *
280          * Prints any necessary footers for a list of options formatted with
281          * writeOptionItem().
282          */
283         void writeOptionListEnd() const;
284
285     private:
286         class Impl;
287
288         /*! \brief
289          * Constructs a context object with the given implementation class.
290          *
291          * \param[in] impl  Implementation object.
292          *
293          * Does not throw.
294          */
295         explicit HelpWriterContext(Impl *impl);
296
297         PrivateImplPointer<Impl> impl_;
298
299         GMX_DISALLOW_ASSIGN(HelpWriterContext);
300 };
301
302 } // namespace gmx
303
304 #endif