Skip to content

Commit

Permalink
Prepare v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-gromeyer committed Oct 25, 2022
1 parent 3d492bc commit 7bab05b
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 27 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: Doxygen Action
name: Update website

on:
push:
branches-ignore:
- master
tags-ignore:
tags:
- '*'
workflow_dispatch:

jobs:
build:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tests/md4c"]
path = tests/md4c
url = https://github.com/software-made-easy/MarkdownEdit_md4c
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

[TOC]

## Note
## v1.0.0

**There are no releases yet!**
Initial release. All basics work but `blockquote` needs a rework.
64 changes: 57 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.12)
project("html2md" VERSION 0.0.1
project("html2md" VERSION 1.0.0
DESCRIPTION "A c++ library that converts HTML to Markdown."
HOMEPAGE_URL "https://github.com/software-made-easy/html2md"
LANGUAGES CXX)
Expand All @@ -9,19 +9,69 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Build type
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not specified. Release is used.")
set(CMAKE_BUILD_TYPE "Release")
else()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
endif()

# Check if it was included
get_directory_property(subproject PARENT_DIRECTORY)

# Some options
option(BUILD_EXE "Build a executable to convert html to markdown." ON)
if (subproject)
option(BUILD_EXE "Build a executable to convert html to markdown." OFF)
option(BUILD_TEST "Build tests" OFF)
else()
option(BUILD_EXE "Build a executable to convert html to markdown." ON)
option(BUILD_TEST "Build tests" ON)
endif()

set(SOURCES
src/html2md.cpp
)
set(HEADER
include/html2md.h
)

add_library(html2md src/html2md.cpp include/html2md.h)
target_include_directories(html2md PUBLIC include src)
add_library(html2md SHARED ${SOURCES})
set_target_properties(html2md PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
PUBLIC_HEADER ${HEADER}
)
target_include_directories(html2md PRIVATE include)

if (subproject OR BUILD_EXE)
add_library(html2md-static STATIC ${HEADER} ${SOURCES})
target_include_directories(html2md-static PUBLIC include)
endif()

if(BUILD_EXE)
add_executable(html2md-exe cli/main.cpp)
target_link_libraries(html2md-exe html2md)
target_link_libraries(html2md-exe html2md-static)
set_target_properties(html2md-exe PROPERTIES OUTPUT_NAME "html2md")
target_compile_definitions(html2md-exe PUBLIC VERSION="${PROJECT_VERSION}")
endif()

if(BUILD_TEST)
add_subdirectory(tests)
endif()

if(UNIX AND BUID_EXE)
if(UNIX)
include(GNUInstallDirs)

install(TARGETS html2md-exe)
install(TARGETS html2md
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/html2md
)

configure_file(html2md.pc.in html2md.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/html2md.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

if (BUILD_EXE)
install(TARGETS html2md-exe)
endif()
endif()
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [What does it do](#what-does-it-do)
- [Use this library](#use-this-library)
- [Supported Tags](#supported-tags)
- [Requirements](#requirements)
- [License](#license)


Expand Down Expand Up @@ -80,6 +81,12 @@ The following table lists the supported HTML tags:
| `ul` | Unordered list | |


## Requirements

1. A compiler with **c++17** support like *g++>=9*

That's all!

## License

html2md is licensed under [The MIT License (MIT)](https://opensource.org/licenses/MIT)
20 changes: 13 additions & 7 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

using namespace std;

constexpr const char *description = " [Options]\n\n"
"Simple and fast HTML to Markdown converter with table support.\n\n"
"Options:\n"
" -h, --help\tDisplays this help information.\n"
" -o, --output\tSets the output file.\n"
" -i, --input\tSets the input file or text.\n"
" -p, --print\tPrint Markdown(overrides -o).\n";
constexpr const char *description =
" [Options]\n\n"
"Simple and fast HTML to Markdown converter with table support.\n\n"
"Options:\n"
" -h, --help\tDisplays this help information.\n"
" -v, --version\tPrint html2md's version.\n"
" -o, --output\tSets the output file.\n"
" -i, --input\tSets the input file or text.\n"
" -p, --print\tPrint Markdown(overrides -o).\n";


int main(int argc, const char* argv[])
Expand All @@ -32,6 +34,10 @@ int main(int argc, const char* argv[])
cout << argv[0] << description;
return 0;
}
else if (item == "-v" || item == "--version") {
cout << "Version " << VERSION << endl;
return 0;
}
else if (item == "-p" || item == "--print") {
print = true;
continue;
Expand Down
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ The following table lists the supported HTML tags:
| `u` | Underlined | Uses HTML. |
| `ul` | Unordered list | |

## Requirements

1. A compiler with **c++17** support like *g++>=9*

That's all!

## License

Expand Down
12 changes: 12 additions & 0 deletions html2md.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@/html2md/

Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@

Requires:
Libs: -L${libdir} -lhtml2md
Cflags: -I${includedir}
11 changes: 11 additions & 0 deletions html2mdConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (CMAKE_VERSION VERSION_LESS 3.12)
message(FATAL_ERROR "html2md requires at least CMake version 3.12")
endif()

if (CMAKE_CXX_STANDARD VERSION_LESS 17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

set(html2md_NOT_FOUND_MESSAGE "")
set(html2md_FOUND TRUE)
12 changes: 7 additions & 5 deletions include/html2md.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static inline std::string Repeat(const std::string &str, size_t amount) {
* ```
*
* \todo Adding the possibility of customization
* \todo Rework `blockquote`s
*/
class Converter {
public:
Expand Down Expand Up @@ -240,7 +241,7 @@ class Converter {

std::string html_;

uint16_t offset_lt_;
uint16_t offset_lt_ = 0;
std::string current_tag_;
std::string prev_tag_;

Expand Down Expand Up @@ -820,12 +821,13 @@ class Converter {
* \brief Static wrapper around the Converter class
* \param html The HTML passed to Converter
* \return Returns the by Converter generated Markdown
*
* \todo Add the possibility to check if everything is ok
*/
inline std::string Convert(std::string &html) {
inline std::string Convert(std::string &html, bool *ok = nullptr) {
Converter c(html);
return c.Convert2Md();
const auto &md = c.Convert2Md();
if (ok)
*ok = c.ok();
return md;
}

} // namespace html2md
Expand Down
2 changes: 1 addition & 1 deletion src/html2md.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static std::vector<std::string> Split(std::string const &str, char delimiter) {
std::stringstream iss(str);

for (std::string token; getline(iss, token, delimiter);)
result.push_back(move(token));
result.push_back(token);

return result;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
project("tests" LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(MD4C_FILES
${CMAKE_CURRENT_LIST_DIR}/md4c/src/entity.c
${CMAKE_CURRENT_LIST_DIR}/md4c/src/entity.h
${CMAKE_CURRENT_LIST_DIR}/md4c/src/md4c-html.c
${CMAKE_CURRENT_LIST_DIR}/md4c/src/md4c-html.h
${CMAKE_CURRENT_LIST_DIR}/md4c/src/md4c.c
${CMAKE_CURRENT_LIST_DIR}/md4c/src/md4c.h
)

add_library(md4c-html STATIC ${MD4C_FILES})
target_include_directories(md4c-html PUBLIC ${CMAKE_CURRENT_LIST_DIR}/md4c/src)

set(TEST_SOURCES
main.cpp
)

add_executable(test ${TEST_SOURCES})
target_link_libraries(test md4c-html html2md-static)
target_compile_definitions(test PUBLIC DIR="${CMAKE_CURRENT_LIST_DIR}")

add_custom_command(TARGET test POST_BUILD
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Running tests"
)
7 changes: 7 additions & 0 deletions tests/lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- foo
- - bar

1. foo
2. bar

- > foo
102 changes: 102 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>

#include "html2md.h"
#include "md4c-html.h"


using namespace std;
namespace fs = std::filesystem;

short test_index = 0;

namespace markdown {
void captureHtmlFragment(const MD_CHAR* data, const MD_SIZE data_size, void* userData) {
auto *str = static_cast<string*>(userData);

str->append(data, data_size);
}

string toHTML(const string &md) {
string html;

md_html(md.c_str(), md.size(), &captureHtmlFragment, &html,
MD_DIALECT_GITHUB, MD_HTML_FLAG_SKIP_UTF8_BOM);

return html;
};

string fromHTML(string &html) {
return html2md::Convert(html);
}
}

namespace file {
string readAll(const string &name) {
ifstream in(name);
stringstream buffer;
buffer << in.rdbuf();
return buffer.str();
};
}

void log(const string &file, const string &html, const string &htmlOut) {
cerr << "Task " << fs::path(file).filename() << " failed:\nOriginal HTML:\n" << html << "\nHTML generated from generated Markdown:\n" << htmlOut << '\n';
}

void running(const string &file) {
cout << "Running test " << fs::path(file).filename() << "... ";
}

void passed(int number, const char *description = nullptr) {
cout << "\x1B[32mPassed!\033[0m\n";
}

void error(int number, const char *description = nullptr) {
cout << "\x1B[31mFailed :(\033[0m\n";
}

void runTest(const string& file) {
const string md = file::readAll(file);

++test_index;

short curr = test_index;

running(file);

string html = markdown::toHTML(md);

string convertedMd = markdown::fromHTML(html);

string testHTML = markdown::toHTML(convertedMd);

if (html == testHTML)
passed(curr);
else {
error(curr);
log(file, html, testHTML);
}
}

int main(int argc, char ** argv){
vector<string> files;

string ext(".md");
for (const auto &p : fs::recursive_directory_iterator(DIR))
{
if (p.path().extension() == ext && p.path().parent_path() == DIR)
files.emplace_back(p.path().c_str());
}

// Redirect errors to error.log
freopen("./error.log", "w", stderr);

for (auto &str : files)
runTest(str);

return 0;
}
1 change: 1 addition & 0 deletions tests/md4c
Submodule md4c added at b64f5e
2 changes: 1 addition & 1 deletion tests/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
| Syntax | Description | Test Text |
| :- | :-: | ---: |
| Header | Title | Here's this |
| Paragraph | Text | And more |
| Paragraph | Text | And more |

0 comments on commit 7bab05b

Please sign in to comment.