2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements gmx::HelpWriterContext.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_onlinehelp
44 #include "helpwritercontext.h"
52 #include <boost/shared_ptr.hpp>
54 #include "gromacs/utility/exceptions.h"
55 #include "gromacs/utility/file.h"
56 #include "gromacs/utility/gmxassert.h"
57 #include "gromacs/utility/programcontext.h"
58 #include "gromacs/utility/stringutil.h"
66 //! \internal \addtogroup module_onlinehelp
75 /* The order of these arrays is significant. Text search and replace
76 * for each element occurs in order, so earlier changes can induce
77 * subsequent changes even though the original text might not appear
78 * to invoke the latter changes.
79 * TODO: Get rid of this behavior. It makes it very difficult to manage
80 * replacements coming from multiple sources (e.g., hyperlinks).*/
82 //! List of replacements for console output.
83 const t_sandr sandrTty[] = {
100 { "[INT]", "integral" },
101 { "[FROM]", " from " },
110 { "[SQRT]", "sqrt(" },
124 { "[COSH]", "cosh(" },
126 { "[SINH]", "sinh(" },
128 { "[TANH]", "tanh(" },
136 //! List of replacements for reStructuredText output.
137 const t_sandr sandrRst[] = {
146 { "[CHEVRON]", "<" },
147 { "[chevron]", ">" },
150 { "[INT]", "integral" },
151 { "[FROM]", " from " },
160 { "[SQRT]", "sqrt(" },
174 { "[COSH]", "cosh(" },
176 { "[SINH]", "sinh(" },
178 { "[TANH]", "tanh(" },
181 // [BR] is fundamentally incompatible with rst
188 * Replaces all entries from a list of replacements.
190 std::string repall(const std::string &s, int nsr, const t_sandr sa[])
192 std::string result(s);
193 for (int i = 0; i < nsr; ++i)
195 result = replaceAll(result, sa[i].search, sa[i].replace);
201 * Replaces all entries from a list of replacements.
203 template <size_t nsr>
204 std::string repall(const std::string &s, const t_sandr (&sa)[nsr])
206 return repall(s, nsr, sa);
210 * Custom output interface for HelpWriterContext::Impl::processMarkup().
212 * Provides an interface that is used to implement different types of output
213 * from HelpWriterContext::Impl::processMarkup().
215 class WrapperInterface
218 virtual ~WrapperInterface() {}
221 * Provides the wrapping settings.
223 * HelpWriterContext::Impl::processMarkup() may provide some default
224 * values for the settings if they are not set; this is the reason the
225 * return value is not const.
227 virtual TextLineWrapperSettings &settings() = 0;
228 //! Appends the given string to output.
229 virtual void wrap(const std::string &text) = 0;
233 * Wraps markup output into a single string.
235 class WrapperToString : public WrapperInterface
238 //! Creates a wrapper with the given settings.
239 explicit WrapperToString(const TextLineWrapperSettings &settings)
244 virtual TextLineWrapperSettings &settings()
246 return wrapper_.settings();
248 virtual void wrap(const std::string &text)
250 result_.append(wrapper_.wrapToString(text));
252 //! Returns the result string.
253 const std::string &result() const { return result_; }
256 TextLineWrapper wrapper_;
261 * Wraps markup output into a vector of string (one line per element).
263 class WrapperToVector : public WrapperInterface
266 //! Creates a wrapper with the given settings.
267 explicit WrapperToVector(const TextLineWrapperSettings &settings)
272 virtual TextLineWrapperSettings &settings()
274 return wrapper_.settings();
276 virtual void wrap(const std::string &text)
278 const std::vector<std::string> &lines = wrapper_.wrapToVector(text);
279 result_.insert(result_.end(), lines.begin(), lines.end());
281 //! Returns a vector with the output lines.
282 const std::vector<std::string> &result() const { return result_; }
285 TextLineWrapper wrapper_;
286 std::vector<std::string> result_;
290 * Makes the string uppercase.
292 * \param[in] text Input text.
293 * \returns \p text with all characters transformed to uppercase.
294 * \throws std::bad_alloc if out of memory.
296 std::string toUpperCase(const std::string &text)
298 std::string result(text);
299 std::transform(result.begin(), result.end(), result.begin(), toupper);
307 /********************************************************************
312 * Private implementation class for HelpLinks.
314 * \ingroup module_onlinehelp
316 class HelpLinks::Impl
321 LinkItem(const std::string &linkName,
322 const std::string &replacement)
323 : linkName(linkName), replacement(replacement)
326 std::string linkName;
327 std::string replacement;
330 //! Shorthand for a list of links.
331 typedef std::vector<LinkItem> LinkList;
333 //! Initializes empty links with the given format.
334 explicit Impl(HelpOutputFormat format) : format_(format)
340 //! Output format for which the links are formatted.
341 HelpOutputFormat format_;
344 /********************************************************************
348 HelpLinks::HelpLinks(HelpOutputFormat format) : impl_(new Impl(format))
352 HelpLinks::~HelpLinks()
356 void HelpLinks::addLink(const std::string &linkName,
357 const std::string &targetName,
358 const std::string &displayName)
360 std::string replacement;
361 switch (impl_->format_)
363 case eHelpOutputFormat_Console:
364 replacement = repall(displayName, sandrTty);
366 case eHelpOutputFormat_Rst:
367 replacement = targetName;
370 GMX_RELEASE_ASSERT(false, "Output format not implemented for links");
372 impl_->links_.push_back(Impl::LinkItem(linkName, replacement));
375 /********************************************************************
376 * HelpWriterContext::Impl
380 * Private implementation class for HelpWriterContext.
382 * \ingroup module_onlinehelp
384 class HelpWriterContext::Impl
388 * Shared, non-modifiable state for context objects.
390 * Contents of this structure are shared between all context objects
391 * that are created from a common parent.
392 * This state should not be modified after construction.
394 * \ingroup module_onlinehelp
398 //! Initializes the state with the given parameters.
399 SharedState(File *file, HelpOutputFormat format,
400 const HelpLinks *links)
401 : file_(*file), format_(format), links_(links)
405 //! Output file to which the help is written.
407 //! Output format for the help output.
408 HelpOutputFormat format_;
410 const HelpLinks *links_;
415 ReplaceItem(const std::string &search,
416 const std::string &replace)
417 : search(search), replace(replace)
424 //! Smart pointer type for managing the shared state.
425 typedef boost::shared_ptr<const SharedState> StatePointer;
426 //! Shorthand for a list of markup/other replacements.
427 typedef std::vector<ReplaceItem> ReplaceList;
429 //! Initializes the context with the given state.
430 explicit Impl(const StatePointer &state)
433 initDefaultReplacements();
436 //! Initializes default replacements for the chosen output format.
437 void initDefaultReplacements();
438 //! Adds a new replacement.
439 void addReplacement(const std::string &search,
440 const std::string &replace)
442 replacements_.push_back(ReplaceItem(search, replace));
445 //! Replaces links in a given string.
446 std::string replaceLinks(const std::string &input) const;
449 * Process markup and wrap lines within a block of text.
451 * \param[in] text Text to process.
452 * \param wrapper Object used to wrap the text.
454 * The \p wrapper should take care of either writing the text to output
455 * or providing an interface for the caller to retrieve the output.
457 void processMarkup(const std::string &text,
458 WrapperInterface *wrapper) const;
460 //! Constant state shared by all child context objects.
462 //! List of markup/other replacements.
463 ReplaceList replacements_;
466 GMX_DISALLOW_ASSIGN(Impl);
469 void HelpWriterContext::Impl::initDefaultReplacements()
471 const char *program = getProgramContext().programName();
472 addReplacement("[PROGRAM]", program);
475 std::string HelpWriterContext::Impl::replaceLinks(const std::string &input) const
477 std::string result(input);
478 if (state_->links_ != NULL)
480 HelpLinks::Impl::LinkList::const_iterator link;
481 for (link = state_->links_->impl_->links_.begin();
482 link != state_->links_->impl_->links_.end(); ++link)
484 result = replaceAllWords(result, link->linkName, link->replacement);
490 void HelpWriterContext::Impl::processMarkup(const std::string &text,
491 WrapperInterface *wrapper) const
493 std::string result(text);
494 for (ReplaceList::const_iterator i = replacements_.begin();
495 i != replacements_.end(); ++i)
497 result = replaceAll(result, i->search, i->replace);
499 switch (state_->format_)
501 case eHelpOutputFormat_Console:
503 result = repall(result, sandrTty);
504 result = replaceLinks(result);
505 return wrapper->wrap(result);
507 case eHelpOutputFormat_Rst:
509 result = repall(result, sandrRst);
510 result = replaceLinks(result);
511 result = replaceAll(result, "[REF]", "");
512 result = replaceAll(result, "[ref]", "");
513 return wrapper->wrap(result);
516 GMX_THROW(InternalError("Invalid help output format"));
520 /********************************************************************
524 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format)
525 : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, NULL))))
529 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format,
530 const HelpLinks *links)
531 : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, links))))
535 GMX_RELEASE_ASSERT(links->impl_->format_ == format,
536 "Links must have the same output format as the context");
540 HelpWriterContext::HelpWriterContext(Impl *impl)
545 HelpWriterContext::HelpWriterContext(const HelpWriterContext &other)
546 : impl_(new Impl(*other.impl_))
550 HelpWriterContext::~HelpWriterContext()
554 void HelpWriterContext::setReplacement(const std::string &search,
555 const std::string &replace)
557 impl_->addReplacement(search, replace);
560 HelpOutputFormat HelpWriterContext::outputFormat() const
562 return impl_->state_->format_;
565 File &HelpWriterContext::outputFile() const
567 return impl_->state_->file_;
571 HelpWriterContext::substituteMarkupAndWrapToString(
572 const TextLineWrapperSettings &settings, const std::string &text) const
574 WrapperToString wrapper(settings);
575 impl_->processMarkup(text, &wrapper);
576 return wrapper.result();
579 std::vector<std::string>
580 HelpWriterContext::substituteMarkupAndWrapToVector(
581 const TextLineWrapperSettings &settings, const std::string &text) const
583 WrapperToVector wrapper(settings);
584 impl_->processMarkup(text, &wrapper);
585 return wrapper.result();
588 void HelpWriterContext::writeTitle(const std::string &title) const
590 File &file = outputFile();
591 switch (outputFormat())
593 case eHelpOutputFormat_Console:
594 file.writeLine(toUpperCase(title));
597 case eHelpOutputFormat_Rst:
598 file.writeLine(title);
599 file.writeLine(std::string(title.length(), '-'));
602 GMX_THROW(NotImplementedError(
603 "This output format is not implemented"));
607 void HelpWriterContext::writeTextBlock(const std::string &text) const
609 TextLineWrapperSettings settings;
610 if (outputFormat() == eHelpOutputFormat_Console)
612 settings.setLineLength(78);
614 outputFile().writeLine(substituteMarkupAndWrapToString(settings, text));
617 void HelpWriterContext::writeOptionListStart() const
621 void HelpWriterContext::writeOptionItem(const std::string &name,
622 const std::string &args,
623 const std::string &description) const
625 File &file = outputFile();
626 switch (outputFormat())
628 case eHelpOutputFormat_Console:
629 // TODO: Generalize this when there is need for it; the current,
630 // special implementation is in CommandLineHelpWriter.
631 GMX_THROW(NotImplementedError("Option item formatting for console output not implemented"));
633 case eHelpOutputFormat_Rst:
635 file.writeLine(formatString("``%s`` %s", name.c_str(), args.c_str()));
636 TextLineWrapperSettings settings;
637 settings.setIndent(4);
638 file.writeLine(substituteMarkupAndWrapToString(settings, description));
642 GMX_THROW(NotImplementedError(
643 "This output format is not implemented"));
647 void HelpWriterContext::writeOptionListEnd() const