html2md  v1.6.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 (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.
 
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(std::string &html, bool *ok=nullptr)
Static wrapper around the Converter class.
Definition html2md.h:597

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 ( 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

◆ 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 208 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 165 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 189 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.

◆ 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 361 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 218 of file html2md.cpp.

◆ operator bool()

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

Returns ok().

Definition at line 246 of file html2md.h.

◆ operator==() [1/2]

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

Definition at line 239 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 237 of file html2md.h.

◆ reset()

void html2md::Converter::reset ( )

Reset the generated Markdown.

Definition at line 971 of file html2md.cpp.


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