2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, 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[] = {
96 { "[INT]", "integral" },
97 { "[FROM]", " from " },
106 { "[SQRT]", "sqrt(" },
120 { "[COSH]", "cosh(" },
122 { "[SINH]", "sinh(" },
124 { "[TANH]", "tanh(" },
128 /* [UL], [LI], [ul] cannot be implemented properly with the current
134 //! List of replacements for man page output.
135 const t_sandr sandrMan[] = {
144 { "[CHEVRON]", "<" },
145 { "[chevron]", ">" },
148 { "[INT]", "integral" },
149 { "[FROM]", " from " },
158 { "[SQRT]", "sqrt(" },
172 { "[COSH]", "cosh(" },
174 { "[SINH]", "sinh(" },
176 { "[TANH]", "tanh(" },
180 // The following three work only in the specific context in which they are
195 //! List of replacements for HTML output.
196 const t_sandr sandrHtml[] = {
207 { "[CHEVRON]", "<" },
208 { "[chevron]", ">" },
211 { "[INT]", "integral" },
212 { "[FROM]", " from " },
221 { "[SQRT]", "sqrt(" },
235 { "[COSH]", "cosh(" },
237 { "[SINH]", "sinh(" },
239 { "[TANH]", "tanh(" },
251 * Replaces all entries from a list of replacements.
253 std::string repall(const std::string &s, int nsr, const t_sandr sa[])
255 std::string result(s);
256 for (int i = 0; i < nsr; ++i)
258 result = replaceAll(result, sa[i].search, sa[i].replace);
264 * Replaces all entries from a list of replacements.
266 template <size_t nsr>
267 std::string repall(const std::string &s, const t_sandr (&sa)[nsr])
269 return repall(s, nsr, sa);
273 * Custom output interface for HelpWriterContext::Impl::processMarkup().
275 * Provides an interface that is used to implement different types of output
276 * from HelpWriterContext::Impl::processMarkup().
278 class WrapperInterface
281 virtual ~WrapperInterface() {}
284 * Provides the wrapping settings.
286 * HelpWriterContext::Impl::processMarkup() may provide some default
287 * values for the settings if they are not set; this is the reason the
288 * return value is not const.
290 virtual TextLineWrapperSettings &settings() = 0;
291 //! Appends the given string to output.
292 virtual void wrap(const std::string &text) = 0;
296 * Wraps markup output into a single string.
298 class WrapperToString : public WrapperInterface
301 //! Creates a wrapper with the given settings.
302 explicit WrapperToString(const TextLineWrapperSettings &settings)
307 virtual TextLineWrapperSettings &settings()
309 return wrapper_.settings();
311 virtual void wrap(const std::string &text)
313 result_.append(wrapper_.wrapToString(text));
315 //! Returns the result string.
316 const std::string &result() const { return result_; }
319 TextLineWrapper wrapper_;
324 * Wraps markup output into a vector of string (one line per element).
326 class WrapperToVector : public WrapperInterface
329 //! Creates a wrapper with the given settings.
330 explicit WrapperToVector(const TextLineWrapperSettings &settings)
335 virtual TextLineWrapperSettings &settings()
337 return wrapper_.settings();
339 virtual void wrap(const std::string &text)
341 const std::vector<std::string> &lines = wrapper_.wrapToVector(text);
342 result_.insert(result_.end(), lines.begin(), lines.end());
344 //! Returns a vector with the output lines.
345 const std::vector<std::string> &result() const { return result_; }
348 TextLineWrapper wrapper_;
349 std::vector<std::string> result_;
353 * Makes the string uppercase.
355 * \param[in] text Input text.
356 * \returns \p text with all characters transformed to uppercase.
357 * \throws std::bad_alloc if out of memory.
359 std::string toUpperCase(const std::string &text)
361 std::string result(text);
362 std::transform(result.begin(), result.end(), result.begin(), toupper);
370 /********************************************************************
375 * Private implementation class for HelpLinks.
377 * \ingroup module_onlinehelp
379 class HelpLinks::Impl
384 LinkItem(const std::string &linkName,
385 const std::string &replacement)
386 : linkName(linkName), replacement(replacement)
389 std::string linkName;
390 std::string replacement;
393 //! Shorthand for a list of links.
394 typedef std::vector<LinkItem> LinkList;
396 //! Initializes empty links with the given format.
397 explicit Impl(HelpOutputFormat format) : format_(format)
403 //! Output format for which the links are formatted.
404 HelpOutputFormat format_;
407 /********************************************************************
411 HelpLinks::HelpLinks(HelpOutputFormat format) : impl_(new Impl(format))
415 HelpLinks::~HelpLinks()
419 void HelpLinks::addLink(const std::string &linkName,
420 const std::string &targetName,
421 const std::string &displayName)
423 std::string replacement;
424 switch (impl_->format_)
426 case eHelpOutputFormat_Console:
427 replacement = repall(displayName, sandrTty);
429 case eHelpOutputFormat_Man:
430 replacement = repall(displayName, sandrMan);
432 case eHelpOutputFormat_Html:
433 replacement = formatString(
434 "<a href=\"%s.html\">%s</a>", targetName.c_str(),
435 repall(displayName, sandrHtml).c_str());
438 GMX_RELEASE_ASSERT(false, "Output format not implemented for links");
440 impl_->links_.push_back(Impl::LinkItem(linkName, replacement));
443 /********************************************************************
444 * HelpWriterContext::Impl
448 * Private implementation class for HelpWriterContext.
450 * \ingroup module_onlinehelp
452 class HelpWriterContext::Impl
456 * Shared, non-modifiable state for context objects.
458 * Contents of this structure are shared between all context objects
459 * that are created from a common parent.
460 * This state should not be modified after construction.
462 * \ingroup module_onlinehelp
466 //! Initializes the state with the given parameters.
467 SharedState(File *file, HelpOutputFormat format,
468 const HelpLinks *links)
469 : file_(*file), format_(format), links_(links)
473 //! Output file to which the help is written.
475 //! Output format for the help output.
476 HelpOutputFormat format_;
478 const HelpLinks *links_;
483 ReplaceItem(const std::string &search,
484 const std::string &replace)
485 : search(search), replace(replace)
492 //! Smart pointer type for managing the shared state.
493 typedef boost::shared_ptr<const SharedState> StatePointer;
494 //! Shorthand for a list of markup/other replacements.
495 typedef std::vector<ReplaceItem> ReplaceList;
497 //! Initializes the context with the given state.
498 explicit Impl(const StatePointer &state)
501 initDefaultReplacements();
504 //! Initializes default replacements for the chosen output format.
505 void initDefaultReplacements();
506 //! Adds a new replacement.
507 void addReplacement(const std::string &search,
508 const std::string &replace)
510 replacements_.push_back(ReplaceItem(search, replace));
513 //! Replaces links in a given string.
514 std::string replaceLinks(const std::string &input) const;
517 * Process markup and wrap lines within a block of text.
519 * \param[in] text Text to process.
520 * \param wrapper Object used to wrap the text.
522 * The \p wrapper should take care of either writing the text to output
523 * or providing an interface for the caller to retrieve the output.
525 void processMarkup(const std::string &text,
526 WrapperInterface *wrapper) const;
528 //! Constant state shared by all child context objects.
530 //! List of markup/other replacements.
531 ReplaceList replacements_;
534 GMX_DISALLOW_ASSIGN(Impl);
537 void HelpWriterContext::Impl::initDefaultReplacements()
539 const char *program = getProgramContext().programName();
540 addReplacement("[PROGRAM]", program);
543 std::string HelpWriterContext::Impl::replaceLinks(const std::string &input) const
545 std::string result(input);
546 if (state_->links_ != NULL)
548 HelpLinks::Impl::LinkList::const_iterator link;
549 for (link = state_->links_->impl_->links_.begin();
550 link != state_->links_->impl_->links_.end(); ++link)
552 result = replaceAllWords(result, link->linkName, link->replacement);
558 void HelpWriterContext::Impl::processMarkup(const std::string &text,
559 WrapperInterface *wrapper) const
561 std::string result(text);
562 for (ReplaceList::const_iterator i = replacements_.begin();
563 i != replacements_.end(); ++i)
565 result = replaceAll(result, i->search, i->replace);
567 switch (state_->format_)
569 case eHelpOutputFormat_Console:
571 result = repall(result, sandrTty);
572 result = replaceLinks(result);
573 return wrapper->wrap(result);
575 case eHelpOutputFormat_Man:
577 // Needs to be done first to avoid '-' -> '\-' messing up the links.
578 result = replaceLinks(result);
579 result = repall(result, sandrMan);
580 return wrapper->wrap(result);
582 case eHelpOutputFormat_Html:
584 result = repall(result, sandrHtml);
585 result = replaceLinks(result);
586 return wrapper->wrap(result);
589 GMX_THROW(InternalError("Invalid help output format"));
593 /********************************************************************
597 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format)
598 : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, NULL))))
602 HelpWriterContext::HelpWriterContext(File *file, HelpOutputFormat format,
603 const HelpLinks *links)
604 : impl_(new Impl(Impl::StatePointer(new Impl::SharedState(file, format, links))))
608 GMX_RELEASE_ASSERT(links->impl_->format_ == format,
609 "Links must have the same output format as the context");
613 HelpWriterContext::HelpWriterContext(Impl *impl)
618 HelpWriterContext::HelpWriterContext(const HelpWriterContext &other)
619 : impl_(new Impl(*other.impl_))
623 HelpWriterContext::~HelpWriterContext()
627 void HelpWriterContext::setReplacement(const std::string &search,
628 const std::string &replace)
630 impl_->addReplacement(search, replace);
633 HelpOutputFormat HelpWriterContext::outputFormat() const
635 return impl_->state_->format_;
638 File &HelpWriterContext::outputFile() const
640 return impl_->state_->file_;
644 HelpWriterContext::substituteMarkupAndWrapToString(
645 const TextLineWrapperSettings &settings, const std::string &text) const
647 WrapperToString wrapper(settings);
648 impl_->processMarkup(text, &wrapper);
649 return wrapper.result();
652 std::vector<std::string>
653 HelpWriterContext::substituteMarkupAndWrapToVector(
654 const TextLineWrapperSettings &settings, const std::string &text) const
656 WrapperToVector wrapper(settings);
657 impl_->processMarkup(text, &wrapper);
658 return wrapper.result();
661 void HelpWriterContext::writeTitle(const std::string &title) const
663 File &file = outputFile();
664 switch (outputFormat())
666 case eHelpOutputFormat_Console:
667 file.writeLine(toUpperCase(title));
670 case eHelpOutputFormat_Man:
671 file.writeLine(formatString(".SH %s", toUpperCase(title).c_str()));
673 case eHelpOutputFormat_Html:
674 file.writeLine(formatString("<H3>%s</H3>", title.c_str()));
677 GMX_THROW(NotImplementedError(
678 "This output format is not implemented"));
682 void HelpWriterContext::writeTextBlock(const std::string &text) const
684 TextLineWrapperSettings settings;
685 if (outputFormat() == eHelpOutputFormat_Console)
687 settings.setLineLength(78);
689 outputFile().writeLine(substituteMarkupAndWrapToString(settings, text));
692 void HelpWriterContext::writeOptionListStart() const
694 if (outputFormat() == eHelpOutputFormat_Html)
696 outputFile().writeLine("<dl>");
700 void HelpWriterContext::writeOptionItem(const std::string &name,
701 const std::string &args,
702 const std::string &description) const
704 File &file = outputFile();
705 switch (outputFormat())
707 case eHelpOutputFormat_Console:
708 // TODO: Generalize this when there is need for it; the current,
709 // special implementation is in CommandLineHelpWriter.
710 GMX_THROW(NotImplementedError("Option item formatting for console output not implemented"));
712 case eHelpOutputFormat_Man:
713 file.writeLine(formatString(".BI \"\\%s\" \" %s\"", name.c_str(), args.c_str()));
714 file.writeString(" ");
715 writeTextBlock(description);
718 case eHelpOutputFormat_Html:
720 std::string substArgs =
721 substituteMarkupAndWrapToString(TextLineWrapperSettings(), args);
722 file.writeLine(formatString("<dt><b><tt>%s</tt></b> %s</dt>", name.c_str(),
724 file.writeLine("<dd>");
725 writeTextBlock(description);
726 file.writeLine("</dd>");
730 GMX_THROW(NotImplementedError(
731 "This output format is not implemented"));
735 void HelpWriterContext::writeOptionListEnd() const
737 if (outputFormat() == eHelpOutputFormat_Html)
739 outputFile().writeLine("</dl>");