LCOV - code coverage report
Current view: top level - src/tools/qt-gui/src/markdownconverter - discountmarkdownconverter.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 57 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright 2013 Christian Loose <christian.loose@hamburg.de>
       3             :  *
       4             :  * Redistribution and use in source and binary forms, with or without
       5             :  * modification, are permitted provided that the following conditions are
       6             :  * met:
       7             :  *
       8             :  * (1) Redistributions of source code must retain the above copyright
       9             :  * notice, this list of conditions and the following disclaimer.
      10             :  *
      11             :  * (2) Redistributions in binary form must reproduce the above copyright
      12             :  * notice, this list of conditions and the following disclaimer in
      13             :  * the documentation and/or other materials provided with the
      14             :  * distribution.
      15             :  *
      16             :  * (3) The name of the author may not be used to
      17             :  * endorse or promote products derived from this software without
      18             :  * specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      21             :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
      22             :  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      23             :  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
      24             :  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      25             :  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
      26             :  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      27             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
      28             :  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
      29             :  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      30             :  * POSSIBILITY OF SUCH DAMAGE.
      31             :  */
      32             : #include "discountmarkdownconverter.h"
      33             : 
      34             : extern "C" {
      35             : #ifdef Q_OS_WIN
      36             : #include <Windows.h>
      37             : #endif
      38             : #include <mkdio.h>
      39             : }
      40             : 
      41             : #include "markdowndocument.h"
      42             : #include "template/htmltemplate.h"
      43             : 
      44             : class DiscountMarkdownDocument : public MarkdownDocument
      45             : {
      46             : public:
      47           0 :         explicit DiscountMarkdownDocument (MMIOT * document_) : discountDocument (document_)
      48             :         {
      49             :         }
      50           0 :         ~DiscountMarkdownDocument ()
      51           0 :         {
      52           0 :                 mkd_cleanup (discountDocument);
      53           0 :         }
      54             : 
      55             :         MMIOT * document () const
      56             :         {
      57             :                 return discountDocument;
      58             :         }
      59             : 
      60             : private:
      61             :         MMIOT * discountDocument;
      62             : };
      63             : 
      64             : 
      65           0 : DiscountMarkdownConverter::DiscountMarkdownConverter ()
      66             : {
      67           0 : }
      68             : 
      69           0 : MarkdownDocument * DiscountMarkdownConverter::createDocument (const QString & text, ConverterOptions options)
      70             : {
      71           0 :         MMIOT * doc = nullptr;
      72             : 
      73           0 :         if (text.length () > 0)
      74             :         {
      75           0 :                 QString markdownText (text);
      76             : 
      77             :                 // text has to always end with a line break,
      78             :                 // otherwise characters are missing in HTML
      79           0 :                 if (!markdownText.endsWith ('\n'))
      80             :                 {
      81           0 :                         markdownText.append ('\n');
      82             :                 }
      83             : 
      84           0 :                 unsigned long converterOptions = translateConverterOptions (options);
      85             : 
      86           0 :                 QByteArray utf8Data = markdownText.toUtf8 ();
      87           0 :                 doc = mkd_string (utf8Data, utf8Data.length (), converterOptions);
      88             : 
      89           0 :                 mkd_compile (doc, converterOptions);
      90             :         }
      91             : 
      92           0 :         return new DiscountMarkdownDocument (doc);
      93             : }
      94             : 
      95           0 : QString DiscountMarkdownConverter::renderAsHtml (MarkdownDocument * document)
      96             : {
      97           0 :         QString html;
      98             : 
      99           0 :         if (document)
     100             :         {
     101           0 :                 DiscountMarkdownDocument * doc = dynamic_cast<DiscountMarkdownDocument *> (document);
     102             : 
     103           0 :                 if (doc && doc->document ())
     104             :                 {
     105             :                         char * out;
     106           0 :                         mkd_document (doc->document (), &out);
     107             : 
     108           0 :                         html = QString::fromUtf8 (out);
     109             :                 }
     110             :         }
     111             : 
     112           0 :         return html;
     113             : }
     114             : 
     115           0 : QString DiscountMarkdownConverter::renderAsTableOfContents (MarkdownDocument * document)
     116             : {
     117           0 :         QString toc;
     118             : 
     119           0 :         if (document)
     120             :         {
     121           0 :                 DiscountMarkdownDocument * doc = dynamic_cast<DiscountMarkdownDocument *> (document);
     122             : 
     123           0 :                 if (doc && doc->document ())
     124             :                 {
     125             :                         // generate table of contents
     126             :                         char * out;
     127           0 :                         mkd_toc (doc->document (), &out);
     128             : 
     129           0 :                         toc = QString::fromUtf8 (out);
     130             :                 }
     131             :         }
     132             : 
     133           0 :         return toc;
     134             : }
     135             : 
     136           0 : Template * DiscountMarkdownConverter::templateRenderer () const
     137             : {
     138           0 :         static HtmlTemplate htmlTemplate;
     139           0 :         return &htmlTemplate;
     140             : }
     141             : 
     142           0 : MarkdownConverter::ConverterOptions DiscountMarkdownConverter::supportedOptions () const
     143             : {
     144             :         return MarkdownConverter::AutolinkOption | MarkdownConverter::NoStrikethroughOption | MarkdownConverter::NoAlphaListOption |
     145             :                MarkdownConverter::NoDefinitionListOption | MarkdownConverter::NoSmartypantsOption | MarkdownConverter::ExtraFootnoteOption |
     146           0 :                MarkdownConverter::NoSuperscriptOption;
     147             : }
     148             : 
     149           0 : unsigned long DiscountMarkdownConverter::translateConverterOptions (ConverterOptions options) const
     150             : {
     151           0 :         unsigned long converterOptions = MKD_TOC;
     152             : 
     153             : #ifdef MKD_NOSTYLE
     154           0 :         converterOptions |= MKD_NOSTYLE;
     155             : #endif
     156             : 
     157             :         // autolink
     158           0 :         if (options.testFlag (MarkdownConverter::AutolinkOption))
     159             :         {
     160           0 :                 converterOptions |= MKD_AUTOLINK;
     161             :         }
     162             : 
     163             :         // strikethrough
     164           0 :         if (options.testFlag (MarkdownConverter::NoStrikethroughOption))
     165             :         {
     166           0 :                 converterOptions |= MKD_NOSTRIKETHROUGH;
     167             :         }
     168             : 
     169             :         // alphabetic lists
     170           0 :         if (options.testFlag (MarkdownConverter::NoAlphaListOption))
     171             :         {
     172           0 :                 converterOptions |= MKD_NOALPHALIST;
     173             :         }
     174             : 
     175             :         // definition lists
     176           0 :         if (options.testFlag (MarkdownConverter::NoDefinitionListOption))
     177             :         {
     178           0 :                 converterOptions |= MKD_NODLIST;
     179             :         }
     180             : 
     181             :         // SmartyPants
     182           0 :         if (options.testFlag (MarkdownConverter::NoSmartypantsOption))
     183             :         {
     184           0 :                 converterOptions |= MKD_NOPANTS;
     185             :         }
     186             : 
     187             :         // Footnotes
     188           0 :         if (options.testFlag (MarkdownConverter::ExtraFootnoteOption))
     189             :         {
     190           0 :                 converterOptions |= MKD_EXTRA_FOOTNOTE;
     191             :         }
     192             : 
     193             :         // Superscript
     194           0 :         if (options.testFlag (MarkdownConverter::NoSuperscriptOption))
     195             :         {
     196           0 :                 converterOptions |= MKD_NOSUPERSCRIPT;
     197             :         }
     198             : 
     199           0 :         return converterOptions;
     200             : }

Generated by: LCOV version 1.13