mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-11-27 00:43:47 +00:00
Use source-highlight library for syntax colorizing.
This commit is contained in:
parent
ef9e74c473
commit
ed25d60351
5 changed files with 83 additions and 5 deletions
|
@ -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}")
|
||||
|
|
40
cmake/modules/FindSourceHighlight.cmake
Normal file
40
cmake/modules/FindSourceHighlight.cmake
Normal file
|
@ -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
|
||||
)
|
|
@ -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
|
||||
|
|
|
@ -18,14 +18,34 @@
|
|||
#include "pastie_response.hpp"
|
||||
#include "incredis/incredis.hpp"
|
||||
#include <ciso646>
|
||||
#include <srchilite/sourcehighlight.h>
|
||||
#include <srchilite/langmap.h>
|
||||
#include <sstream>
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue