Add subsection depth to HelpWriterContext
[alexxy/gromacs.git] / src / gromacs / onlinehelp / helpwritercontext.cpp
index b7bc9c2308e406f8ef40bae13a64221ec62b8e30..4c928e02ab955075938ce4d2c63326176eefbba6 100644 (file)
@@ -66,6 +66,9 @@ namespace
 //! \internal \addtogroup module_onlinehelp
 //! \{
 
+//! Characters used for reStructuredText title underlining.
+const char g_titleChars[] = "=-^*~+#'_.";
+
 struct t_sandr
 {
     const char *search;
@@ -465,9 +468,9 @@ class HelpWriterContext::Impl
         //! Shorthand for a list of markup/other replacements.
         typedef std::vector<ReplaceItem> ReplaceList;
 
-        //! Initializes the context with the given state.
-        explicit Impl(const StatePointer &state)
-            : state_(state)
+        //! Initializes the context with the given state and section depth.
+        Impl(const StatePointer &state, int sectionDepth)
+            : state_(state), sectionDepth_(sectionDepth)
         {
             initDefaultReplacements();
         }
@@ -500,6 +503,8 @@ class HelpWriterContext::Impl
         StatePointer            state_;
         //! List of markup/other replacements.
         ReplaceList             replacements_;
+        //! Number of subsections above this context.
+        int                     sectionDepth_;
 
     private:
         GMX_DISALLOW_ASSIGN(Impl);
@@ -672,13 +677,13 @@ void HelpWriterContext::Impl::processMarkup(const std::string &text,
  */
 
 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format)
-    : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, NULL))))
+    : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, NULL)), 0))
 {
 }
 
 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format,
                                      const HelpLinks *links)
-    : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, links))))
+    : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, links)), 0))
 {
     if (links != NULL)
     {
@@ -717,6 +722,14 @@ File &HelpWriterContext::outputFile() const
     return impl_->state_->file_;
 }
 
+void HelpWriterContext::enterSubSection(const std::string &title)
+{
+    GMX_RELEASE_ASSERT(impl_->sectionDepth_ - 1 < static_cast<int>(std::strlen(g_titleChars)),
+                       "Too deeply nested subsections");
+    writeTitle(title);
+    ++impl_->sectionDepth_;
+}
+
 std::string
 HelpWriterContext::substituteMarkupAndWrapToString(
         const TextLineWrapperSettings &settings, const std::string &text) const
@@ -737,6 +750,10 @@ HelpWriterContext::substituteMarkupAndWrapToVector(
 
 void HelpWriterContext::writeTitle(const std::string &title) const
 {
+    if (title.empty())
+    {
+        return;
+    }
     File &file = outputFile();
     switch (outputFormat())
     {
@@ -746,7 +763,8 @@ void HelpWriterContext::writeTitle(const std::string &title) const
             break;
         case eHelpOutputFormat_Rst:
             file.writeLine(title);
-            file.writeLine(std::string(title.length(), '-'));
+            file.writeLine(std::string(title.length(),
+                                       g_titleChars[impl_->sectionDepth_]));
             break;
         default:
             GMX_THROW(NotImplementedError(