diff --git a/CMakeLists.txt b/CMakeLists.txt index a11b359..1fb8744 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.3 FATAL_ERROR) -list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules) +list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) project(tawashi_top) set(TAWSHI_SOURCE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/cmake/modules/FindSourceHighlight.cmake b/cmake/modules/FindSourceHighlight.cmake new file mode 100644 index 0000000..4fd69c0 --- /dev/null +++ b/cmake/modules/FindSourceHighlight.cmake @@ -0,0 +1,40 @@ +# Copyright 2017 by Michele Santullo +# +# Try to find sourge-highlight +# Usage of this module as follows: +# +# find_package(SourceHighlight) +# +# The following variables will be defined: +# +# SourceHighlight_FOUND - system has source-highlight +# SourceHighlight_INCLUDE_DIR - the libsource-highlight include directory +# SourceHighlight_LIBRARIES - The libraries needed to use libsource-highlight + +find_path(SourceHighlight_ROOT_DIR + NAMES include/srchilite/sourcehighlight.h +) + +find_path(SourceHighlight_INCLUDE_DIR + NAMES srchilite/sourcehighlight.h + HINTS ${SourceHighlight_ROOT_DIR}/include +) + +find_library(SourceHighlight_LIBRARIES + NAMES source-highlight + HINTS ${SourceHighlight_ROOT_DIR}/lib +) + +if(SourceHighlight_INCLUDE_DIR AND SourceHighlight_LIBRARIES AND Ncurses_LIBRARY) + set(SourceHighlight_FOUND ON) +else(SourceHighlight_INCLUDE_DIR AND SourceHighlight_LIBRARIES AND Ncurses_LIBRARY) + find_library(SourceHighlight_LIBRARIES NAMES readline) + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args(SourceHighlight DEFAULT_MSG SourceHighlight_INCLUDE_DIR SourceHighlight_LIBRARIES ) + mark_as_advanced(SourceHighlight_INCLUDE_DIR SourceHighlight_LIBRARIES) +endif(SourceHighlight_INCLUDE_DIR AND SourceHighlight_LIBRARIES AND Ncurses_LIBRARY) + +mark_as_advanced( + SourceHighlight_INCLUDE_DIR + SourceHighlight_LIBRARIES +) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 34c2777..9da8cb6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ project(tawashi CXX) find_package(Boost 1.53.0 REQUIRED COMPONENTS program_options filesystem system) find_package(CURL REQUIRED) +find_package(SourceHighlight REQUIRED) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -25,11 +26,13 @@ target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${Boost_INCLUDE_DIRS} PRIVATE ${TAWASHI_SOURCE_ROOT}/lib/bette-enums PRIVATE ${CURL_INCLUDE_DIR} + PRIVATE ${SourceHighlight_INCLUDE_DIR} ) target_link_libraries(${PROJECT_NAME} PRIVATE ${Boost_LIBRARIES} PRIVATE incredis - ${CURL_LIBRARIES} + PRIVATE ${CURL_LIBRARIES} + PRIVATE ${SourceHighlight_LIBRARIES} ) target_compile_definitions(${PROJECT_NAME} PRIVATE BOOST_SPIRIT_USE_PHOENIX_V3=1 diff --git a/src/pastie_response.cpp b/src/pastie_response.cpp index a78ffc5..81c0bfd 100644 --- a/src/pastie_response.cpp +++ b/src/pastie_response.cpp @@ -18,14 +18,34 @@ #include "pastie_response.hpp" #include "incredis/incredis.hpp" #include +#include +#include +#include namespace tawashi { PastieResponse::PastieResponse (redis::IncRedis& parRedis) : - Response(Response::ContentType, "text/plain"), - m_redis(parRedis) + Response(Response::ContentType, "text/html"), + m_redis(parRedis), + m_plain_text(false) { } + void PastieResponse::on_process() { + auto env = cgi_env().query_string_split(); + if (env["m"] == "plain") { + this->change_type(Response::ContentType, "text/plain"); + m_plain_text = true; + } + else { + srchilite::LangMap lang_map("/usr/share/source-highlight", "lang.map"); + lang_map.open(); + if (not cgi_env().query_string().empty()) + m_lang_file = lang_map.getFileName(cgi_env().query_string()); + else + m_lang_file = "default.lang"; + } + } + void PastieResponse::on_send (std::ostream& parStream) { using opt_string = redis::IncRedis::opt_string; @@ -38,6 +58,18 @@ namespace tawashi { if (not pastie) { } - parStream << *pastie; + if (m_plain_text) { + parStream << *pastie; + } + else { + srchilite::SourceHighlight highlighter; + highlighter.setDataDir("/usr/share/source-highlight"); + highlighter.setGenerateEntireDoc(false); + highlighter.setGenerateLineNumbers(true); + const auto lang = m_lang_file; + std::istringstream iss(*pastie); + + highlighter.highlight(iss, parStream, lang); + } } } //namespace tawashi diff --git a/src/pastie_response.hpp b/src/pastie_response.hpp index c9ce3ee..566d396 100644 --- a/src/pastie_response.hpp +++ b/src/pastie_response.hpp @@ -30,8 +30,11 @@ namespace tawashi { PastieResponse (redis::IncRedis& parRedis); private: + virtual void on_process() override; virtual void on_send (std::ostream& parStream) override; redis::IncRedis& m_redis; + std::string m_lang_file; + bool m_plain_text; }; } //namespace tawashi