Skip to content

Commit

Permalink
-added CMake helper functions located in hypertextcpp.cmake;
Browse files Browse the repository at this point in the history
-added unnamed namespace usage to shared lib renderer;
-cleaned up codebase;
-updated readme;
-set version to v1.2.0;
  • Loading branch information
kamchatka-volcano committed Oct 5, 2024
1 parent 8e36030 commit 465fc96
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 171 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.18)
project(hypertextcpp VERSION 1.1.0 DESCRIPTION "hypertextcpp")
project(hypertextcpp VERSION 1.2.0 DESCRIPTION "hypertextcpp")

include(external/seal_lake)

Expand Down
104 changes: 45 additions & 59 deletions README.md

Large diffs are not rendered by default.

36 changes: 0 additions & 36 deletions build_shared_template.cmake

This file was deleted.

Binary file modified doc/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 3 additions & 6 deletions examples/ex_01/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.18)
project(ex_01)

add_custom_command(
OUTPUT todolist.h
COMMAND hypertextcpp ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp -o ${CMAKE_CURRENT_SOURCE_DIR}/todolist.h
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp
VERBATIM
)
include(../../hypertextcpp.cmake)

hypertextcpp_GenerateHeader(NAME todolist)

set(SRC
todolist_printer.cpp
Expand Down
9 changes: 3 additions & 6 deletions examples/ex_02/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.18)
project(ex_02)

add_custom_command(
OUTPUT todolist.h
COMMAND hypertextcpp ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp -o ${CMAKE_CURRENT_SOURCE_DIR}/todolist.h
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp
VERBATIM
)
include(../../hypertextcpp.cmake)

hypertextcpp_GenerateHeader(NAME todolist)

set(SRC
todolist_printer.cpp
Expand Down
9 changes: 3 additions & 6 deletions examples/ex_03/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.18)
project(ex_03)

add_custom_command(
OUTPUT todolist.h
COMMAND hypertextcpp ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp -o ${CMAKE_CURRENT_SOURCE_DIR}/todolist.h
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp
VERBATIM
)
include(../../hypertextcpp.cmake)

hypertextcpp_GenerateHeader(NAME todolist)

set(SRC
todolist_printer.cpp
Expand Down
9 changes: 3 additions & 6 deletions examples/ex_04/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.18)
project(ex_04)

add_custom_command(
OUTPUT todolist.h
COMMAND hypertextcpp ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp -o ${CMAKE_CURRENT_SOURCE_DIR}/todolist.h --class-name TodoList
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/todolist.htcpp
VERBATIM
)
include(../../hypertextcpp.cmake)

hypertextcpp_GenerateHeader(NAME todolist CLASS_NAME TodoList)

set(SRC
todolist_printer.cpp
Expand Down
4 changes: 2 additions & 2 deletions examples/ex_05/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ set_target_properties(${PROJECT_NAME} PROPERTIES CXX_EXTENSIONS OFF)

target_link_libraries(${PROJECT_NAME} ${CMAKE_DL_LIBS})

include(../../build_shared_template.cmake)
hypertextcpp_BuildSharedTemplate(
include(../../hypertextcpp.cmake)
hypertextcpp_BuildSharedLibrary(
NAME todolist
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
)
Expand Down
80 changes: 80 additions & 0 deletions hypertextcpp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
function(hypertextcpp_GenerateHeader)
cmake_parse_arguments(
ARG
""
"NAME"
"FILE;CLASS_NAME;OUTPUT_DIR"
${ARGN}
)
if (NOT ARG_NAME)
message(FATAL_ERROR "[hypertextcpp_GenerateHeader] Argument 'NAME' is missing")
endif()
if (ARG_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "[hypertextcpp_GenerateHeader] Unsupported argument: ${ARG_UNPARSED_ARGUMENTS}")
endif()

if (ARG_FILE)
set(TEMPLATE_FILE ${ARG_FILE})
else()
set(TEMPLATE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_NAME}.htcpp)
endif()

if (ARG_OUTPUT_DIR)
set(OUTPUT_DIR ${ARG_OUTPUT_DIR})
else()
set(OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if (ARG_CLASS_NAME)
add_custom_command(
OUTPUT ${ARG_NAME}.h
COMMAND hypertextcpp ${TEMPLATE_FILE} -o ${OUTPUT_DIR}/${ARG_NAME}.h -c ${ARG_CLASS_NAME}
DEPENDS ${TEMPLATE_FILE}
VERBATIM
)
else()
add_custom_command(
OUTPUT ${ARG_NAME}.h
COMMAND hypertextcpp ${TEMPLATE_FILE} -o ${OUTPUT_DIR}/${ARG_NAME}.h
DEPENDS ${TEMPLATE_FILE}
VERBATIM
)
endif()
endfunction()

function(hypertextcpp_BuildSharedLibrary)
cmake_parse_arguments(
ARG
""
"NAME"
"FILE;OUTPUT_DIR"
${ARGN}
)
if (NOT ARG_NAME)
message(FATAL_ERROR "[hypertextcpp_BuildSharedLibrary] Argument 'NAME' is missing")
endif()
if (ARG_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "[hypertextcpp_BuildSharedLibrary] Unsupported argument: ${ARG_UNPARSED_ARGUMENTS}")
endif()

if (ARG_FILE)
set(TEMPLATE_FILE ${ARG_FILE})
else()
set(TEMPLATE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_NAME}.htcpp)
endif()

add_custom_command(
OUTPUT ${ARG_NAME}.cpp
COMMAND hypertextcpp ${TEMPLATE_FILE} -o ${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}.cpp -s
DEPENDS ${TEMPLATE_FILE}
VERBATIM
)

add_library(${ARG_NAME} SHARED ${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}.cpp)
target_compile_features(${ARG_NAME} PUBLIC cxx_std_11)
target_compile_definitions(${ARG_NAME} PUBLIC "HYPERTEXTCPP_EXPORT")
set_target_properties(${ARG_NAME} PROPERTIES CXX_EXTENSIONS OFF)
if (ARG_OUTPUT_DIR)
set_target_properties(${ARG_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_OUTPUT_DIR})
endif()
endfunction()
17 changes: 0 additions & 17 deletions shared_template/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/codenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "errors.h"
#include "utils.h"
#include <utility>
#include <gsl/gsl>

namespace htcpp{

Expand Down
55 changes: 28 additions & 27 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "errors.h"
#include "nameutils.h"
#include "transpiler.h"
#include "shared_lib_transpiler_renderer.h"
#include "single_header_transpiler_renderer.h"
#include "transpiler.h"
#include <cmdlime/commandlinereader.h>
#include <cmdlime/config.h>
#include <filesystem>
Expand All @@ -13,44 +13,45 @@
namespace fs = std::filesystem;

namespace {
struct Cfg : public cmdlime::Config{
CMDLIME_ARG(input, fs::path) << ".htcpp file to transpile";
CMDLIME_PARAM(output, fs::path)() << "output c++ file path\n(if empty, current working directory is used)";
CMDLIME_PARAM(className, std::string)() << "generated class name";
CMDLIME_FLAG(sharedLib) << "generate result as shared library source files";
CMDLIME_FLAG(classPascalcase) << cmdlime::WithoutShortName{} << "generate class name by using .htcpp filename in PascalCase";
CMDLIME_FLAG(classSnakecase) << cmdlime::WithoutShortName{} << "generate class name by using .htcpp filename in snake_case";
CMDLIME_FLAG(classLowercase) << cmdlime::WithoutShortName{} << "generate class name by using .htcpp filename in lowercase";
struct Cfg : public cmdlime::Config {
CMDLIME_ARG(input, fs::path) << ".htcpp file to transpile";
CMDLIME_PARAM(output, fs::path)() << "output c++ file path\n(if empty, current working directory is used)";
CMDLIME_PARAM(className, std::string)() << "generated class name";
CMDLIME_FLAG(sharedLib) << "generate result as shared library source files";
CMDLIME_FLAG(classPascalcase) << cmdlime::WithoutShortName{}
<< "generate class name by using .htcpp filename in PascalCase";
CMDLIME_FLAG(classSnakecase) << cmdlime::WithoutShortName{}
<< "generate class name by using .htcpp filename in snake_case";
CMDLIME_FLAG(classLowercase) << cmdlime::WithoutShortName{}
<< "generate class name by using .htcpp filename in lowercase";
};
fs::path getOutputFilePath(const Cfg& cfg);
std::string getClassName(const Cfg& cfg);
std::unique_ptr<htcpp::ITranspilerRenderer> makeTranspilerRenderer(const Cfg& cfg);
}
} //namespace

int run(const Cfg& cfg)
{
auto result = std::string{};
auto transpilerRenderer = makeTranspilerRenderer(cfg);
auto transpiler = htcpp::Transpiler{*transpilerRenderer};
try{
result = transpiler.process(cfg.input);
auto stream = std::ofstream{getOutputFilePath(cfg)};
stream << result;
try {
result = transpiler.process(cfg.input);
auto stream = std::ofstream{getOutputFilePath(cfg)};
stream << result;
}
catch(const htcpp::Error& e)
{
std::cerr << e.what() << std::endl;
return 1;
catch (const htcpp::Error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
catch(const std::exception& e)
{
std::cerr << "Unknown critical error:" << e.what() << std::endl;
return 1;
catch (const std::exception& e) {
std::cerr << "Unknown critical error:" << e.what() << std::endl;
return 1;
}
return 0;
}

int main(int argc, char**argv)
int main(int argc, char** argv)
{
return cmdlime::CommandLineReader{"hypertextcpp"}.exec<Cfg>(argc, argv, run);
}
Expand All @@ -59,13 +60,13 @@ namespace {
fs::path getOutputFilePath(const Cfg& cfg)
{
auto path = fs::current_path();
if (!cfg.output.empty()){
if (!cfg.output.empty()) {
if (cfg.output.is_absolute())
path = cfg.output;
else
path /= cfg.output;
}
else{
else {
if (cfg.sharedLib)
path /= (cfg.input.stem().string() + ".cpp");
else
Expand All @@ -77,7 +78,7 @@ fs::path getOutputFilePath(const Cfg& cfg)
std::string getClassName(const Cfg& cfg)
{
auto result = cfg.className;
if (result.empty()){
if (result.empty()) {
result = cfg.input.stem().string();
if (cfg.classPascalcase)
result = htcpp::utils::toPascalCase(result);
Expand All @@ -97,4 +98,4 @@ std::unique_ptr<htcpp::ITranspilerRenderer> makeTranspilerRenderer(const Cfg& cf
return std::make_unique<htcpp::SingleHeaderTranspilerRenderer>(getClassName(cfg));
}

}
} //namespace
1 change: 0 additions & 1 deletion src/procedurenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "streamreader.h"
#include "utils.h"
#include "node_utils.h"
#include <gsl/gsl>

namespace htcpp{

Expand Down
2 changes: 1 addition & 1 deletion src/sectionnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "streamreader.h"
#include "textnode.h"
#include "utils.h"
#include <gsl/gsl>
#include <algorithm>

namespace htcpp{

Expand Down
7 changes: 6 additions & 1 deletion src/shared_lib_transpiler_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ std::string SharedLibTranspilerRenderer::generateCode(const std::vector<gsl::not
#define HYPERTEXTCPP_API
#endif
namespace{
struct Template;
}
namespace htcpp{
class AllowRenderTag{
friend struct ::Template;
Expand All @@ -46,6 +48,7 @@ std::string SharedLibTranspilerRenderer::generateCode(const std::vector<gsl::not
}
#define HTCPP_CONFIG(TCfg) using Cfg = TCfg;\
namespace {\
struct TemplateRenderer{\
void renderHTML(const Cfg& cfg, std::ostream& out, htcpp::AllowRenderTag) const;\
void renderHTMLPart(const std::string& name, const Cfg& cfg, std::ostream& out, htcpp::AllowRenderTag) const;\
Expand Down Expand Up @@ -84,6 +87,7 @@ std::string SharedLibTranspilerRenderer::generateCode(const std::vector<gsl::not
renderer_.renderHTMLPart(renderFuncName, cfg, stream, htcpp::AllowRenderTag{});\
}\
};\
}\
\
extern "C" \
HYPERTEXTCPP_API htcpp::ITemplate<TCfg>* makeTemplate()\
Expand All @@ -109,7 +113,7 @@ std::string SharedLibTranspilerRenderer::generateCode(const std::vector<gsl::not
result += "\n}\n";


result +=
result +="namespace {\n"
"void TemplateRenderer::renderHTML(const Cfg& cfg, std::ostream& out, htcpp::AllowRenderTag tag) const\n"
"{\n";
for (const auto& procedure : procedures)
Expand All @@ -129,6 +133,7 @@ std::string SharedLibTranspilerRenderer::generateCode(const std::vector<gsl::not
result += " htcpp::" + procedure->name() + "(cfg, out, tag);";
}
result += "\n}\n";
result += "}";

return result;
}
Expand Down
Loading

0 comments on commit 465fc96

Please sign in to comment.