2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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
42 #include "helpwritercontext.h"
50 #include <boost/shared_ptr.hpp>
52 #include "gromacs/onlinehelp/helpformat.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/file.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/programinfo.h"
57 #include "gromacs/utility/stringutil.h"
65 //! \addtogroup module_onlinehelp
74 /* The order of these arrays is significant. Text search and replace
75 * for each element occurs in order, so earlier changes can induce
76 * subsequent changes even though the original text might not appear
77 * to invoke the latter changes. */
79 //! List of replacements for console output.
80 const t_sandr sandrTty[] = {
93 { "[INT]", "integral" },
94 { "[FROM]", " from " },
103 { "[SQRT]", "sqrt(" },
117 { "[COSH]", "cosh(" },
119 { "[SINH]", "sinh(" },
121 { "[TANH]", "tanh(" },
129 //! List of replacements for man page output.
130 const t_sandr sandrMan[] = {
139 { "[CHEVRON]", "<" },
140 { "[chevron]", ">" },
143 { "[INT]", "integral" },
144 { "[FROM]", " from " },
153 { "[SQRT]", "sqrt(" },
167 { "[COSH]", "cosh(" },
169 { "[SINH]", "sinh(" },
171 { "[TANH]", "tanh(" },
185 //! List of replacements for HTML output.
186 const t_sandr sandrHtml[] = {
197 { "[CHEVRON]", "<" },
198 { "[chevron]", ">" },
201 { "[INT]", "integral" },
202 { "[FROM]", " from " },
211 { "[SQRT]", "sqrt(" },
225 { "[COSH]", "cosh(" },
227 { "[SINH]", "sinh(" },
229 { "[TANH]", "tanh(" },
238 * Replaces all entries from a list of replacements.
240 std::string repall(const std::string &s, int nsr, const t_sandr sa[])
242 std::string result(s);
243 for (int i = 0; i < nsr; ++i)
245 result = replaceAll(result, sa[i].search, sa[i].replace);
251 * Replaces all entries from a list of replacements.
253 template <size_t nsr>
254 std::string repall(const std::string &s, const t_sandr (&sa)[nsr])
256 return repall(s, nsr, sa);
260 * Custom output interface for HelpWriterContext::Impl::processMarkup().
262 * Provides an interface that is used to implement different types of output
263 * from HelpWriterContext::Impl::processMarkup().
265 class WrapperInterface
268 virtual ~WrapperInterface() {}
271 * Provides the wrapping settings.
273 * HelpWriterContext::Impl::processMarkup() may provide some default
274 * values for the settings if they are not set; this is the reason the
275 * return value is not const.
277 virtual TextLineWrapperSettings &settings() = 0;
278 //! Appends the given string to output.
279 virtual void wrap(const std::string &text) = 0;
283 * Wraps markup output into a single string.
285 class WrapperToString : public WrapperInterface
288 //! Creates a wrapper with the given settings.
289 explicit WrapperToString(const TextLineWrapperSettings &settings)
294 virtual TextLineWrapperSettings &settings()
296 return wrapper_.settings();
298 virtual void wrap(const std::string &text)
300 result_.append(wrapper_.wrapToString(text));
302 //! Returns the result string.
303 const std::string &result() const { return result_; }
306 TextLineWrapper wrapper_;
311 * Wraps markup output into a vector of string (one line per element).
313 class WrapperToVector : public WrapperInterface
316 //! Creates a wrapper with the given settings.
317 explicit WrapperToVector(const TextLineWrapperSettings &settings)
322 virtual TextLineWrapperSettings &settings()
324 return wrapper_.settings();
326 virtual void wrap(const std::string &text)
328 const std::vector<std::string> &lines = wrapper_.wrapToVector(text);
329 result_.insert(result_.end(), lines.begin(), lines.end());
331 //! Returns a vector with the output lines.
332 const std::vector<std::string> &result() const { return result_; }
335 TextLineWrapper wrapper_;
336 std::vector<std::string> result_;
340 * Make the string uppercase.
342 * \param[in] text Input text.
343 * \returns \p text with all characters transformed to uppercase.
344 * \throws std::bad_alloc if out of memory.
346 std::string toUpperCase(const std::string &text)
348 std::string result(text);
349 transform(result.begin(), result.end(), result.begin(), toupper);
357 /********************************************************************
362 * Private implementation class for HelpLinks.
364 * \ingroup module_onlinehelp
366 class HelpLinks::Impl
371 LinkItem(const std::string &linkName,
372 const std::string &targetName)
373 : linkName(linkName), targetName(targetName)
376 std::string linkName;
377 std::string targetName;
380 //! Shorthand for a list of links.
381 typedef std::vector<LinkItem> LinkList;
387 /********************************************************************
391 HelpLinks::HelpLinks() : impl_(new Impl)
395 HelpLinks::~HelpLinks()
399 void HelpLinks::addLink(const std::string &linkName,
400 const std::string &targetName)
402 impl_->links_.push_back(Impl::LinkItem(linkName, targetName));
405 /********************************************************************
406 * HelpWriterContext::Impl
410 * Private implementation class for HelpWriterContext.
412 * \ingroup module_onlinehelp
414 class HelpWriterContext::Impl
418 * Shared, non-modifiable state for context objects.
420 * Contents of this structure are shared between all context objects
421 * that are created from a common parent.
422 * This state should not be modified after construction.
424 * \ingroup module_onlinehelp
428 //! Initializes the state with the given parameters.
429 SharedState(File *file, HelpOutputFormat format,
430 const HelpLinks *links)
431 : file_(*file), format_(format), links_(links)
435 //! Output file to which the help is written.
437 //! Output format for the help output.
438 HelpOutputFormat format_;
440 const HelpLinks *links_;
443 //! Smart pointer type for managing the shared state.
444 typedef boost::shared_ptr<const SharedState> StatePointer;
446 //! Initializes the context with the given state.
447 explicit Impl(const StatePointer &state)
453 * Process markup and wrap lines within a block of text.
455 * \param[in] text Text to process.
456 * \param wrapper Object used to wrap the text.
458 * The \p wrapper should take care of either writing the text to output
459 * or providing an interface for the caller to retrieve the output.
461 void processMarkup(const std::string &text,
462 WrapperInterface *wrapper) const;
464 //! Constant state shared by all child context objects.
468 GMX_DISALLOW_ASSIGN(Impl);
471 void HelpWriterContext::Impl::processMarkup(const std::string &text,
472 WrapperInterface *wrapper) const
474 const char *program = ProgramInfo::getInstance().programName().c_str();
475 std::string result(text);
476 result = replaceAll(result, "[PROGRAM]", program);
477 switch (state_->format_)
479 case eHelpOutputFormat_Console:
481 result = repall(result, sandrTty);
482 if (wrapper->settings().lineLength() == 0)
484 wrapper->settings().setLineLength(78);
486 return wrapper->wrap(result);
488 case eHelpOutputFormat_Man:
490 result = repall(result, sandrMan);
491 return wrapper->wrap(result);
493 case eHelpOutputFormat_Html:
495 result = repall(result, sandrHtml);
496 if (state_->links_ != NULL)
498 HelpLinks::Impl::LinkList::const_iterator link;
499 for (link = state_->links_->impl_->links_.begin();
500 link != state_->links_->impl_->links_.end(); ++link)
502 std::string replacement
503 = formatString("<a href=\"%s.html\">%s</a>",
504 link->targetName.c_str(),
505 link->linkName.c_str());
506 result = replaceAllWords(result, link->linkName, replacement);
509 return wrapper->wrap(result);
512 GMX_THROW(InternalError("Invalid help output format"));
516 /********************************************************************
520 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format)
521 : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, NULL))))
525 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format,
526 const HelpLinks *links)
527 : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, links))))
531 HelpWriterContext::HelpWriterContext(Impl *impl)
536 HelpWriterContext::HelpWriterContext(const HelpWriterContext &other)
537 : impl_(new Impl(*other.impl_))
541 HelpWriterContext::~HelpWriterContext()
545 HelpOutputFormat HelpWriterContext::outputFormat() const
547 return impl_->state_->format_;
550 File &HelpWriterContext::outputFile() const
552 return impl_->state_->file_;
556 HelpWriterContext::substituteMarkupAndWrapToString(
557 const TextLineWrapperSettings &settings, const std::string &text) const
559 WrapperToString wrapper(settings);
560 impl_->processMarkup(text, &wrapper);
561 return wrapper.result();
564 std::vector<std::string>
565 HelpWriterContext::substituteMarkupAndWrapToVector(
566 const TextLineWrapperSettings &settings, const std::string &text) const
568 WrapperToVector wrapper(settings);
569 impl_->processMarkup(text, &wrapper);
570 return wrapper.result();
573 void HelpWriterContext::writeTitle(const std::string &title) const
575 if (outputFormat() != eHelpOutputFormat_Console)
577 // TODO: Implement once the situation with Redmine issue #969 is more
579 GMX_THROW(NotImplementedError(
580 "This output format is not implemented"));
582 File &file = outputFile();
583 file.writeLine(toUpperCase(title));
587 void HelpWriterContext::writeTextBlock(const std::string &text) const
589 writeTextBlock(TextLineWrapperSettings(), text);
592 void HelpWriterContext::writeTextBlock(const TextLineWrapperSettings &settings,
593 const std::string &text) const
595 outputFile().writeLine(substituteMarkupAndWrapToString(settings, text));