-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d492bc
commit 7bab05b
Showing
16 changed files
with
262 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
- foo | ||
- - bar | ||
|
||
1. foo | ||
2. bar | ||
|
||
- > foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters