9 #include <boost/algorithm/string.hpp> 10 #include <boost/filesystem/operations.hpp> 11 #include <boost/filesystem/convenience.hpp> 13 #include <Wt/WApplication.h> 15 #include <Wt/WImage.h> 18 namespace fs = boost::filesystem;
23 : fileNameRole_(fileNameRole),
24 contentRole_(contentRole),
25 filePathRole_(filePathRole),
39 || (!fp.empty() && !fs::is_directory(fp))) {
54 strcpy(spool,
"/tmp/wtXXXXXX");
56 int i = mkstemp(spool);
59 char spool[2 * L_tmpnam];
62 return std::string(spool);
67 if (boost::iends_with(fileName,
".h")
68 || boost::iends_with(fileName,
".C")
69 || boost::iends_with(fileName,
".cpp"))
71 else if (boost::iends_with(fileName,
".xml"))
73 else if (boost::iends_with(fileName,
".html"))
75 else if (boost::iends_with(fileName,
".java"))
77 else if (boost::iends_with(fileName,
".js"))
79 else if (boost::iends_with(fileName,
".css"))
87 std::size_t outputFileSize = (std::size_t)fs::file_size(fileName);
88 std::fstream file (fileName.c_str(), std::ios::in | std::ios::binary);
89 char* memblock =
new char [outputFileSize];
90 file.read(memblock, (std::streamsize)outputFileSize);
92 std::string data = std::string(memblock, outputFileSize);
101 auto result = cpp14::make_unique<WText>();
102 result->setInline(
false);
103 return std::move(result);
111 if (!contentsData.empty())
114 std::string fileName =
117 std::string filePath;
118 if (!filePathData.empty())
125 if (content !=
"" && content.substr(0, 100).find(
"-*- C++ -*-")
126 != std::string::npos)
129 std::string outputFileName;
132 std::string inputFileName;
134 if (!filePathData.empty())
135 inputFileName = filePath;
138 std::ofstream out(inputFileName.c_str(),
139 std::ios::out | std::ios::binary);
140 out.write(content.c_str(), (std::streamsize)content.length());
146 std::string sourceHighlightCommand =
"source-highlight ";
147 sourceHighlightCommand +=
"--src-lang=" + lang +
" ";
148 sourceHighlightCommand +=
"--out-format=xhtml ";
149 sourceHighlightCommand +=
"--input=" + inputFileName +
" ";
150 sourceHighlightCommand +=
"--output=" + outputFileName +
" ";
152 std::cerr << sourceHighlightCommand << std::endl;
153 bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
155 if (sourceHighlightOk)
161 unlink(outputFileName.c_str());
163 if (filePathData.empty())
164 unlink(inputFileName.c_str());
170 if (!boost::iends_with(fileName,
".jar")
171 && !boost::iends_with(fileName,
".war")
172 && !boost::iends_with(fileName,
".class"))
175 std::unique_ptr<WWidget> result;
178 std::unique_ptr<WImage> image(cpp14::make_unique<WImage>());
182 (int)content.length());
184 result = std::move(image);
185 }
else if (lang !=
"") {
186 auto text = cpp14::make_unique<WText>();
187 text->setTextFormat(TextFormat::UnsafeXHTML);
188 text->setText(content);
189 result = std::move(text);
191 auto text = cpp14::make_unique<WText>();
192 text->setTextFormat(TextFormat::Plain);
193 text->setText(content);
194 result = std::move(text);
197 result->setInline(
false);
198 WApplication::instance()
199 ->doJavaScript(result->jsRef() +
".parentNode.scrollTop = 0;");
200 return std::move(result);
205 static const char *imageExtensions[] = {
206 ".png",
".gif",
".jpg",
"jpeg",
".ico", 0
209 fs::path p(fileName);
210 std::string extension = fs::extension(p);
212 for (
const char **s = imageExtensions; *s != 0; ++s)
214 return extension.substr(1);
216 return std::string();
WModelIndex index_
The index that is currently displayed.
std::string toUTF8() const
bool setIndex(const WModelIndex &index)
Sets the model index.
std::shared_ptr< WMemoryResource > imageResource_
Wt::ItemDataRole contentRole_
WString asString(const cpp17::any &v, const WString &formatString=WString())
std::string readFileToString(const std::string &fileName)
Wt::ItemDataRole filePathRole_
std::string imageExtension(const std::string &fileName)
Wt::ItemDataRole fileNameRole_
The role that is currently displayed.
virtual std::unique_ptr< WWidget > renderView()
Returns the widget that renders the view.
std::string getLanguageFromFileExtension(const std::string &fileName)
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
std::string tempFileName()
SourceView(ItemDataRole fileNameRole, ItemDataRole contentRole, ItemDataRole filePathRole)
Constructor.
virtual ~SourceView()
Destructor.