9 #include <boost/algorithm/string.hpp>
11 #include <boost/version.hpp>
12 #if BOOST_VERSION < 108500
13 #include <boost/filesystem/convenience.hpp>
15 #include <boost/filesystem/directory.hpp>
17 #include <boost/filesystem/operations.hpp>
19 #include <Wt/WApplication.h>
21 #include <Wt/WImage.h>
24 namespace fs = boost::filesystem;
29 : fileNameRole_(fileNameRole),
30 contentRole_(contentRole),
31 filePathRole_(filePathRole),
45 || (!fp.empty() && !fs::is_directory(fp))) {
60 strcpy(spool,
"/tmp/wtXXXXXX");
62 int i = mkstemp(spool);
65 char spool[2 * L_tmpnam];
68 return std::string(spool);
73 if (boost::iends_with(fileName,
".h")
74 || boost::iends_with(fileName,
".C")
75 || boost::iends_with(fileName,
".cpp"))
77 else if (boost::iends_with(fileName,
".xml"))
79 else if (boost::iends_with(fileName,
".html"))
81 else if (boost::iends_with(fileName,
".java"))
83 else if (boost::iends_with(fileName,
".js"))
85 else if (boost::iends_with(fileName,
".css"))
93 std::size_t outputFileSize = (std::size_t)fs::file_size(fileName);
94 std::fstream file (fileName.c_str(), std::ios::in | std::ios::binary);
95 char* memblock =
new char [outputFileSize];
96 file.read(memblock, (std::streamsize)outputFileSize);
98 std::string data = std::string(memblock, outputFileSize);
107 auto result = std::make_unique<WText>();
108 result->setInline(
false);
117 if (cpp17::any_has_value(contentsData))
120 std::string fileName =
123 std::string filePath;
124 if (cpp17::any_has_value(filePathData))
131 if (content !=
"" && content.substr(0, 100).find(
"-*- C++ -*-")
132 != std::string::npos)
135 std::string outputFileName;
138 std::string inputFileName;
140 if (cpp17::any_has_value(filePathData))
141 inputFileName = filePath;
144 std::ofstream out(inputFileName.c_str(),
145 std::ios::out | std::ios::binary);
146 out.write(content.c_str(), (std::streamsize)content.length());
152 std::string sourceHighlightCommand =
"source-highlight ";
153 sourceHighlightCommand +=
"--src-lang=" + lang +
" ";
154 sourceHighlightCommand +=
"--out-format=xhtml ";
155 sourceHighlightCommand +=
"--input=" + inputFileName +
" ";
156 sourceHighlightCommand +=
"--output=" + outputFileName +
" ";
158 std::cerr << sourceHighlightCommand << std::endl;
159 bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
161 if (sourceHighlightOk)
167 unlink(outputFileName.c_str());
169 if (!cpp17::any_has_value(filePathData))
170 unlink(inputFileName.c_str());
176 if (!boost::iends_with(fileName,
".jar")
177 && !boost::iends_with(fileName,
".war")
178 && !boost::iends_with(fileName,
".class"))
181 std::unique_ptr<WWidget> result;
184 std::unique_ptr<WImage> image(std::make_unique<WImage>());
188 (
int)content.length());
190 result = std::move(image);
191 }
else if (lang !=
"") {
192 auto text = std::make_unique<WText>();
193 text->setTextFormat(TextFormat::UnsafeXHTML);
194 text->setText(content);
195 result = std::move(text);
197 auto text = std::make_unique<WText>();
198 text->setTextFormat(TextFormat::Plain);
199 text->setText(content);
200 result = std::move(text);
203 result->setInline(
false);
204 WApplication::instance()
205 ->doJavaScript(result->jsRef() +
".parentNode.scrollTop = 0;");
211 static const char *imageExtensions[] = {
212 ".png",
".gif",
".jpg",
"jpeg",
".ico", 0
215 fs::path p(fileName);
216 std::string extension = p.extension().string();
218 for (
const char **s = imageExtensions; *s != 0; ++s)
220 return extension.substr(1);
222 return std::string();
std::string readFileToString(const std::string &fileName)
std::string getLanguageFromFileExtension(const std::string &fileName)
std::string tempFileName()
virtual std::unique_ptr< WWidget > renderView()
Returns the widget that renders the view.
Wt::ItemDataRole filePathRole_
WModelIndex index_
The index that is currently displayed.
std::string imageExtension(const std::string &fileName)
bool setIndex(const WModelIndex &index)
Sets the model index.
Wt::ItemDataRole fileNameRole_
The role that is currently displayed.
std::shared_ptr< WMemoryResource > imageResource_
SourceView(ItemDataRole fileNameRole, ItemDataRole contentRole, ItemDataRole filePathRole)
Constructor.
Wt::ItemDataRole contentRole_
virtual ~SourceView()
Destructor.
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
std::string toUTF8() const
WString asString(const cpp17::any &v, const WString &formatString=WString())