diff --git a/src/kamokan_impl/highlight_functions.cpp b/src/kamokan_impl/highlight_functions.cpp index 21e2054..c8344b3 100644 --- a/src/kamokan_impl/highlight_functions.cpp +++ b/src/kamokan_impl/highlight_functions.cpp @@ -17,12 +17,70 @@ #include "highlight_functions.hpp" #include "settings_bag.hpp" +#include #include #include #include #include +#include namespace kamokan { + namespace { + std::string highlight_css_path (const SettingsBag& parSettings) { + //TODO: make sure the file exists or throw or do something + return parSettings.as("highlight_css"); + } + + std::string lang_name_to_file_path (const std::string& parLang, const std::string& parLangmapDir) { + if (parLang.empty()) + return std::string(); + + srchilite::LangMap lang_map(parLangmapDir, "lang.map"); + lang_map.open(); + std::string lang_file = lang_map.getFileName(parLang); + if (lang_file.empty()) + lang_file = "default.lang"; + + return lang_file; + } + + SplitHighlightedPastie strip_tags_from_highlighted (std::string parPastie) { + //I'm expecting some comment at the beginning, like: + // + + SplitHighlightedPastie retval; + { + const auto comment_start = parPastie.find("") - comment_start + 3; + retval.comment = parPastie.substr(comment_start, comment_len); + const std::size_t newline = (comment_len + 1 < parPastie.size() and parPastie[comment_len] == '\n' ? 1 : 0); + parPastie.erase(comment_start, comment_len + newline); + } + } + + { + const auto pre_start = parPastie.find("
");
+				if (parPastie.npos != pre_start) {
+					parPastie.erase(pre_start, 9);
+				}
+			}
+
+			{
+				const auto pre_cl_start = parPastie.find("
"); + if (parPastie.npos != pre_cl_start) { + parPastie.erase(pre_cl_start, 11); + } + } + + retval.text = std::move(parPastie); + return retval; + } + } //unnamed namespace + HighlightLangList list_highlight_langs (const SettingsBag& parSettings) { srchilite::LangMap lang_map(parSettings.as("langmap_dir"), "lang.map"); lang_map.open(); @@ -30,5 +88,27 @@ namespace kamokan { const auto lang_range = boost::make_iterator_range(lang_map.begin(), lang_map.end()); return boost::copy_range(lang_range | boost::adaptors::map_keys); } + + SplitHighlightedPastie highlight_string (std::string&& parIn, const std::string& parLang, const SettingsBag& parSettings) { + const std::string langmap_dir = parSettings.as("langmap_dir"); + srchilite::SourceHighlight highlighter; + highlighter.setDataDir(langmap_dir); + highlighter.setGenerateEntireDoc(false); + highlighter.setGenerateLineNumbers(true); +#if defined(NDEBUG) + highlighter.setOptimize(true); +#else + highlighter.setOptimize(false); +#endif + highlighter.setCanUseStdOut(false); + highlighter.setTabSpaces(4); + highlighter.setStyleCssFile(highlight_css_path(parSettings)); + highlighter.setGenerateLineNumbers(false); + + std::istringstream iss(std::move(parIn)); + std::ostringstream oss; + highlighter.highlight(iss, oss, lang_name_to_file_path(parLang, langmap_dir)); + return strip_tags_from_highlighted(oss.str()); + } } //namespace kamokan diff --git a/src/kamokan_impl/highlight_functions.hpp b/src/kamokan_impl/highlight_functions.hpp index dd41162..1eee70c 100644 --- a/src/kamokan_impl/highlight_functions.hpp +++ b/src/kamokan_impl/highlight_functions.hpp @@ -19,10 +19,18 @@ #include #include +#include +#include namespace kamokan { class SettingsBag; typedef std::vector HighlightLangList; + struct SplitHighlightedPastie { + std::string comment; + std::string text; + }; + HighlightLangList list_highlight_langs (const SettingsBag& parSettings); + SplitHighlightedPastie highlight_string (std::string&& parIn, const std::string& parLang, const SettingsBag& parSettings); } //namespace kamokan diff --git a/src/kamokan_impl/pastie_response.cpp b/src/kamokan_impl/pastie_response.cpp index 02b03dc..2a9fa2e 100644 --- a/src/kamokan_impl/pastie_response.cpp +++ b/src/kamokan_impl/pastie_response.cpp @@ -21,10 +21,8 @@ #include "escapist.hpp" #include "cgi_env.hpp" #include "spdlog.hpp" +#include "highlight_functions.hpp" #include -#include -#include -#include #include #include #include @@ -36,52 +34,6 @@ namespace kamokan { namespace { const char g_nolang_token[] = "colourless"; - struct SplitHighlightedPastie { - std::string comment; - std::string text; - }; - - std::string highlight_css_path (const SettingsBag& parSettings) { - //TODO: make sure the file exists or throw or do something - return parSettings.as("highlight_css"); - } - - SplitHighlightedPastie strip_tags_from_highlighted (std::string parPastie) { - //I'm expecting some comment at the beginning, like: - // - - SplitHighlightedPastie retval; - { - const auto comment_start = parPastie.find("") - comment_start + 3; - retval.comment = parPastie.substr(comment_start, comment_len); - const std::size_t newline = (comment_len + 1 < parPastie.size() and parPastie[comment_len] == '\n' ? 1 : 0); - parPastie.erase(comment_start, comment_len + newline); - } - } - - { - const auto pre_start = parPastie.find("
");
-				if (parPastie.npos != pre_start) {
-					parPastie.erase(pre_start, 9);
-				}
-			}
-
-			{
-				const auto pre_cl_start = parPastie.find("
"); - if (parPastie.npos != pre_cl_start) { - parPastie.erase(pre_cl_start, 11); - } - } - - retval.text = std::move(parPastie); - return retval; - } - mstch::array pastie_to_numbered_lines (boost::string_view parPastie) { using boost::string_view; using string_view_iterator = string_view::const_iterator; @@ -115,7 +67,6 @@ namespace kamokan { const Kakoune::SafePtr& parCgiEnv ) : GeneralPastieResponse(parSettings, parStreamOut, parCgiEnv), - m_langmap_dir(parSettings->as("langmap_dir")), m_plain_text(false), m_syntax_highlight(true) { @@ -132,32 +83,14 @@ namespace kamokan { m_syntax_highlight = false; } else { - srchilite::LangMap lang_map(m_langmap_dir, "lang.map"); - lang_map.open(); - m_lang_file.clear(); + m_pastie_lang.clear(); if (not query_str.empty()) - m_lang_file = lang_map.getFileName(query_str); - if (m_lang_file.empty()) - m_lang_file = "default.lang"; + m_pastie_lang = query_str; } return tawashi::make_header_type_html(); } void PastieResponse::on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) { - srchilite::SourceHighlight highlighter; - highlighter.setDataDir(settings().as("langmap_dir")); - highlighter.setGenerateEntireDoc(false); - highlighter.setGenerateLineNumbers(true); -#if defined(NDEBUG) - highlighter.setOptimize(true); -#else - highlighter.setOptimize(false); -#endif - highlighter.setCanUseStdOut(false); - highlighter.setTabSpaces(4); - highlighter.setStyleCssFile(highlight_css_path(settings())); - highlighter.setGenerateLineNumbers(false); - std::string processed_pastie; std::string highlight_comment; if (m_syntax_highlight) { @@ -169,10 +102,7 @@ namespace kamokan { } if (not m_plain_text and m_syntax_highlight) { - std::istringstream iss(std::move(processed_pastie)); - std::ostringstream oss; - highlighter.highlight(iss, oss, m_lang_file); - SplitHighlightedPastie split = strip_tags_from_highlighted(oss.str()); + SplitHighlightedPastie split = highlight_string(std::move(processed_pastie), m_pastie_lang, settings()); processed_pastie = std::move(split.text); highlight_comment = std::move(split.comment); } diff --git a/src/kamokan_impl/pastie_response.hpp b/src/kamokan_impl/pastie_response.hpp index e56068a..aa60ca4 100644 --- a/src/kamokan_impl/pastie_response.hpp +++ b/src/kamokan_impl/pastie_response.hpp @@ -39,8 +39,7 @@ namespace kamokan { virtual void on_general_mustache_prepare (std::string&& parPastie, mstch::map& parContext) override; virtual bool is_pastie_page() const override { return true; } - std::string m_lang_file; - std::string m_langmap_dir; + std::string m_pastie_lang; bool m_plain_text; bool m_syntax_highlight; };