html2md  v1.7.0
Simple and fast HTML to Markdown converter
Loading...
Searching...
No Matches
html2md::Converter Class Reference

Class for converting HTML to Markdown. More...

#include <html2md.h>

Public Member Functions

 Converter (const std::string &html, struct Options *options=nullptr)
 Standard initializer, takes HTML as parameter. Also prepares everything.
 
std::string convert ()
 Convert HTML into Markdown.
 
ConverterappendToMd (char ch)
 Append a char to the Markdown.
 
ConverterappendToMd (const char *str)
 Append a char* to the Markdown.
 
ConverterappendToMd (const std::string &s)
 Append a string to the Markdown.
 
ConverterappendBlank ()
 Appends a ' ' in certain cases.
 
void addHtmlSymbolConversion (const std::string &htmlSymbol, const std::string &replacement)
 Add an HTML symbol conversion.
 
void removeHtmlSymbolConversion (const std::string &htmlSymbol)
 Remove an HTML symbol conversion.
 
void clearHtmlSymbolConversions ()
 Clear all HTML symbol conversions.
 
bool ok () const
 Checks if everything was closed properly(in the HTML).
 
void reset ()
 Reset the generated Markdown.
 
bool operator== (const Converter *c) const
 Checks if the HTML matches and the options are the same.
 
bool operator== (const Converter &c) const
 
 operator bool () const
 Returns ok().
 

Detailed Description

Class for converting HTML to Markdown.

This class converts HTML to Markdown. There is also a static wrapper for this class (see html2md::Convert).

Usage example

Option 1: Use the class:

std::string html = "<h1>example</h1>";
auto md = c.convert();
if (!c.ok()) std::cout << "There was something wrong in the HTML\n";
std::cout << md; // # example
Class for converting HTML to Markdown.
Definition html2md.h:159

Option 2: Use the static wrapper:

std::string html = "<h1>example</h1>";
auto md = html2md::Convert(html);
std::cout << md;
std::string Convert(const std::string &html, bool *ok=nullptr)
Static wrapper around the Converter class.
Definition html2md.h:634

Advanced: use Options:

std::string html = "<h1>example</h1>";
auto *options = new html2md::Options();
options->splitLines = false;
options->unorderedList = '*';
html2md::Converter c(html, options);
auto md = c.convert();
if (!c.ok()) std::cout << "There was something wrong in the HTML\n";
std::cout << md; // # example
Options for the conversion from HTML to Markdown.
Definition html2md.h:38

Definition at line 159 of file html2md.h.

Constructor & Destructor Documentation

◆ Converter()

html2md::Converter::Converter ( const std::string & html,
struct Options * options = nullptr )
inlineexplicit

Standard initializer, takes HTML as parameter. Also prepares everything.

Parameters
htmlThe HTML as std::string.
optionsOptions for the Conversation. See html2md::Options() for more.
Note
Don't pass anything else than HTML, otherwise the output will be a mess!

This is the default initializer.
You can use appendToMd() to append something to the beginning of the generated output.

Definition at line 173 of file html2md.h.

Member Function Documentation

◆ addHtmlSymbolConversion()

void html2md::Converter::addHtmlSymbolConversion ( const std::string & htmlSymbol,
const std::string & replacement )
inline

Add an HTML symbol conversion.

Parameters
htmlSymbolThe HTML symbol to convert
replacementThe replacement string
Note
This is useful for converting HTML entities to their Markdown equivalents. For example, you can add a conversion for "&nbsp;" to " " (space) or "&lt;" to "<" (less than).
This is not a standard feature of the Converter class, but it can be added to the class to allow for more flexibility in the conversion process. You can use this feature to add custom conversions for any HTML symbol that you want to convert to a specific Markdown representation.

Definition at line 232 of file html2md.h.

◆ appendBlank()

Converter * html2md::Converter::appendBlank ( )

Appends a ' ' in certain cases.

Returns
Copy of the instance with(maybe) the appended space.

This function appends ' ' if:

  • md does not end with *
  • md does not end with \n aka newline

Definition at line 239 of file html2md.cpp.

◆ appendToMd() [1/3]

Converter * html2md::Converter::appendToMd ( char ch)

Append a char to the Markdown.

Parameters
chThe char to append.
Returns
Returns a copy of the instance with the char appended.

Definition at line 197 of file html2md.cpp.

◆ appendToMd() [2/3]

Converter * html2md::Converter::appendToMd ( const char * str)

Append a char* to the Markdown.

Parameters
strThe char* to append.
Returns
Returns a copy of the instance with the char* appended.

Definition at line 221 of file html2md.cpp.

◆ appendToMd() [3/3]

Converter * html2md::Converter::appendToMd ( const std::string & s)
inline

Append a string to the Markdown.

Parameters
sThe string to append.
Returns
Returns a copy of the instance with the string appended.

Definition at line 206 of file html2md.h.

◆ clearHtmlSymbolConversions()

void html2md::Converter::clearHtmlSymbolConversions ( )
inline

Clear all HTML symbol conversions.

Note
This is useful for clearing the conversion map (it's empty afterwards).

Definition at line 251 of file html2md.h.

◆ convert()

string html2md::Converter::convert ( )
nodiscard

Convert HTML into Markdown.

Returns
Returns the converted Markdown.

This function actually converts the HTML into Markdown. It also cleans up the Markdown so you don't have to do anything.

Definition at line 393 of file html2md.cpp.

◆ ok()

bool html2md::Converter::ok ( ) const
nodiscard

Checks if everything was closed properly(in the HTML).

Returns
Returns false if there is a unclosed tag.
Note
As long as you have not called convert(), it always returns true.

Definition at line 249 of file html2md.cpp.

◆ operator bool()

html2md::Converter::operator bool ( ) const
inlineexplicit

Returns ok().

Definition at line 279 of file html2md.h.

◆ operator==() [1/2]

bool html2md::Converter::operator== ( const Converter & c) const
inline

Definition at line 272 of file html2md.h.

◆ operator==() [2/2]

bool html2md::Converter::operator== ( const Converter * c) const
inline

Checks if the HTML matches and the options are the same.

Parameters
TheConverter object to compare with
Returns
true if the HTML and options matches otherwise false

Definition at line 270 of file html2md.h.

◆ removeHtmlSymbolConversion()

void html2md::Converter::removeHtmlSymbolConversion ( const std::string & htmlSymbol)
inline

Remove an HTML symbol conversion.

Parameters
htmlSymbolThe HTML symbol to remove
Note
This is useful for removing custom conversions that you have added previously.

Definition at line 243 of file html2md.h.

◆ reset()

void html2md::Converter::reset ( )

Reset the generated Markdown.

Definition at line 1039 of file html2md.cpp.


The documentation for this class was generated from the following files: