52eda481e1010dc231b780e78208543f4bc6c6a5
[alexxy/gromacs.git] / src / gromacs / onlinehelp / rstparser.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2018,2019, 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 /*! \internal \file
36  * \brief
37  * Declares classes for (partial) parsing of reStructuredText.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_onlinehelp
41  */
42 #ifndef GMX_ONLINEHELP_RSTPARSER_H
43 #define GMX_ONLINEHELP_RSTPARSER_H
44
45 #include <string>
46
47 #include "gromacs/utility/classhelpers.h"
48
49 namespace gmx
50 {
51
52 class TextLineWrapperSettings;
53
54 /*! \internal
55  * \brief
56  * Iterator over reStructuredText paragraphs.
57  *
58  * After initialization, nextParagraph() needs to be called to access the first
59  * paragraph.  Subsequence paragraphs can be accessed by repeated calls to
60  * nextParagraph().  After the last paragraph, nextParagraph() returns `false`.
61  *
62  * After each call to nextParagraph(), other methods can be called to query
63  * details of the current paragraph.
64  *
65  * \ingroup module_onlinehelp
66  */
67 class RstParagraphIterator
68 {
69 public:
70     /*! \brief
71      * Initializes an iterator for given input text.
72      *
73      * Does not throw.
74      */
75     explicit RstParagraphIterator(const std::string& text);
76
77     /*! \brief
78      * Advances the iterator to the next paragraph.
79      *
80      * \returns `false` if there were no more paragraphs.
81      *
82      * Does not throw (except std::bad_alloc if std::string::compare()
83      * throws).
84      */
85     bool nextParagraph();
86
87     //! Returns the indentation for first line of this paragraph.
88     int firstLineIndent() const { return firstLineIndent_; }
89     //! Returns the indentation for subsequent lines of this paragraph.
90     int indent() const { return indent_; }
91     /*! \brief
92      * Returns the text
93      *
94      * \param[out] result  Variable to receive the paragraph text.
95      * \throws std::bad_alloc if out of memory.
96      *
97      * Indentation and internal line breaks have been stripped from the
98      * paragraph text (except for literal blocks etc.).  For literal
99      * blocks, the common indentation has been stripped and is returned in
100      * indent() instead.
101      *
102      * Leading newlines are returned to indicate necessary separation from
103      * the preceding paragraph.
104      */
105     void getParagraphText(std::string* result) const;
106
107 private:
108     enum ParagraphType
109     {
110         eParagraphType_Normal,
111         eParagraphType_Literal,
112         eParagraphType_Title
113     };
114
115     //! The text to iterate over.
116     const std::string& text_; //NOLINT(google-runtime-member-string-references)
117
118     //! Start of the current paragraph.
119     size_t begin_;
120     //! End of the current paragraph (C++-style iterator).
121     size_t end_;
122     //! Type of the current paragraph.
123     ParagraphType type_;
124     //! Number of newlines to print before the current paragraph.
125     int breakSize_;
126     //! Indentation of the first line of this paragraph.
127     int firstLineIndent_;
128     //! (Minimum) indentation of other lines in this paragraph.
129     int indent_;
130
131     //! Start of the next paragrah.
132     size_t nextBegin_;
133     //! Number of newlines to print after the current paragraph.
134     int nextBreakSize_;
135     /*! \brief
136      * Indentation of the preceding paragraph that contained `::`.
137      *
138      * If the next paragraph is not a literal block, the value is `-1`.
139      */
140     int literalIndent_;
141
142     GMX_DISALLOW_COPY_AND_ASSIGN(RstParagraphIterator);
143 };
144
145 } // namespace gmx
146
147 #endif