diff --git a/src/notebook.cc b/src/notebook.cc index 2cdc2d4..68f8614 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -359,6 +359,12 @@ std::unique_ptr Notebook::get_project() { if(get_current_page()!=-1) file_path=get_current_view()->file_path; + if(get_current_page()!=-1) { + if(get_current_view()->language->get_id()=="markdown") { + return std::unique_ptr(new ProjectMarkDown(file_path)); + } + } + return std::unique_ptr(new ProjectClang(file_path)); } @@ -378,4 +384,3 @@ bool Notebook::save_modified_dialog(int page) { return false; } } - diff --git a/src/project.cc b/src/project.cc index b14dbcd..4385b61 100644 --- a/src/project.cc +++ b/src/project.cc @@ -2,6 +2,7 @@ #include "config.h" #include "terminal.h" #include "filesystem.h" +#include std::unordered_map Project::run_arguments; std::unordered_map Project::debug_run_arguments; @@ -111,3 +112,35 @@ void ProjectClang::compile_and_run() { } }); } + +ProjectMarkDown::~ProjectMarkDown() { + if(!last_temp_path.empty()) { + boost::filesystem::remove(last_temp_path); + last_temp_path=boost::filesystem::path(); + } +} + +void ProjectMarkDown::compile_and_run() { + if(!last_temp_path.empty()) { + boost::filesystem::remove(last_temp_path); + last_temp_path=boost::filesystem::path(); + } + + std::stringstream stdin_stream, stdout_stream; + auto exit_status=Terminal::get().process(stdin_stream, stdout_stream, "markdown "+file_path.string(), this->file_path.parent_path()); + if(exit_status==0) { + boost::system::error_code ec; + auto temp_path=boost::filesystem::temp_directory_path(ec); + if(!ec) { + temp_path/=boost::filesystem::unique_path(); + temp_path+=".html"; + if(!boost::filesystem::exists(temp_path)) { + last_temp_path=temp_path; + std::ofstream file_stream(temp_path.string(), std::fstream::binary); + file_stream << stdout_stream.rdbuf(); + file_stream.close(); + Terminal::get().async_process("open "+temp_path.string()); + } + } + } +} diff --git a/src/project.h b/src/project.h index 872f0b5..eb74f85 100644 --- a/src/project.h +++ b/src/project.h @@ -47,4 +47,13 @@ public: void compile_and_run() override; }; +class ProjectMarkDown : public Project { +public: + ProjectMarkDown(const boost::filesystem::path &file_path) : Project(file_path) {} + ~ProjectMarkDown(); + + boost::filesystem::path last_temp_path; + void compile_and_run() override; +}; + #endif // JUCI_PROJECT_H_