9 #include <boost/algorithm/string.hpp> 10 #include <boost/filesystem/operations.hpp> 11 #include <boost/filesystem/convenience.hpp> 13 #include <Wt/WApplication> 18 namespace fs = boost::filesystem;
21 : fileNameRole_(fileNameRole),
22 contentRole_(contentRole),
23 filePathRole_(filePathRole),
37 || (!fp.empty() && !fs::is_directory(fp))) {
52 strcpy(spool,
"/tmp/wtXXXXXX");
54 int i = mkstemp(spool);
57 char spool[2 * L_tmpnam];
60 return std::string(spool);
65 if (boost::iends_with(fileName,
".h")
66 || boost::iends_with(fileName,
".C")
67 || boost::iends_with(fileName,
".cpp"))
69 else if (boost::iends_with(fileName,
".xml"))
71 else if (boost::iends_with(fileName,
".html"))
73 else if (boost::iends_with(fileName,
".java"))
75 else if (boost::iends_with(fileName,
".js"))
77 else if (boost::iends_with(fileName,
".css"))
85 std::size_t outputFileSize = (std::size_t)fs::file_size(fileName);
86 std::fstream file (fileName.c_str(), std::ios::in | std::ios::binary);
87 char* memblock =
new char [outputFileSize];
88 file.read(memblock, (std::streamsize)outputFileSize);
90 std::string data = std::string(memblock, outputFileSize);
109 if (!contentsData.empty())
110 content = boost::any_cast<const std::string&>(contentsData);
112 std::string fileName =
113 boost::any_cast<
const std::string&>(fileNameData);
115 std::string filePath;
116 if (!filePathData.empty())
117 filePath = boost::any_cast<const std::string&>(filePathData);
123 if (content !=
"" && content.substr(0, 100).find(
"-*- C++ -*-")
124 != std::string::npos)
127 std::string outputFileName;
130 std::string inputFileName;
132 if (!filePathData.empty())
133 inputFileName = filePath;
136 std::ofstream out(inputFileName.c_str(),
137 std::ios::out | std::ios::binary);
138 out.write(content.c_str(), (std::streamsize)content.length());
144 std::string sourceHighlightCommand =
"source-highlight ";
145 sourceHighlightCommand +=
"--src-lang=" + lang +
" ";
146 sourceHighlightCommand +=
"--out-format=xhtml ";
147 sourceHighlightCommand +=
"--input=" + inputFileName +
" ";
148 sourceHighlightCommand +=
"--output=" + outputFileName +
" ";
150 std::cerr << sourceHighlightCommand << std::endl;
151 bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
153 if (sourceHighlightOk)
159 unlink(outputFileName.c_str());
161 if (filePathData.empty())
162 unlink(inputFileName.c_str());
168 if (!boost::iends_with(fileName,
".jar")
169 && !boost::iends_with(fileName,
".war")
170 && !boost::iends_with(fileName,
".class"))
183 (int)content.length());
186 }
else if (lang !=
"") {
189 text->
setText(WString::fromUTF8(content));
194 text->
setText(WString::fromUTF8(content));
199 WApplication::instance()
200 ->doJavaScript(result->
jsRef() +
".parentNode.scrollTop = 0;");
206 static const char *imageExtensions[] = {
207 ".png",
".gif",
".jpg",
"jpeg",
".ico", 0
210 fs::path p(fileName);
211 std::string extension = fs::extension(p);
213 for (
const char **s = imageExtensions; *s != 0; ++s)
215 return extension.substr(1);
217 return std::string();
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
int fileNameRole_
The role that is currently displayed.
SourceView(int fileNameRole, int contentRole, int filePathRole)
Constructor.
bool setText(const WString &text)
bool setTextFormat(TextFormat format)
void setMimeType(const std::string &mimeType)
std::string readFileToString(const std::string &fileName)
void setData(const std::vector< unsigned char > &data)
std::string imageExtension(const std::string &fileName)
void setImageLink(const WLink &link)
Wt::WMemoryResource * imageResource_
bool setIndex(const Wt::WModelIndex &index)
Sets the model index.
std::string getLanguageFromFileExtension(const std::string &fileName)
virtual Wt::WWidget * renderView()
Returns the widget that renders the view.
Wt::WModelIndex index_
The index that is currently displayed.
std::string tempFileName()
virtual ~SourceView()
Destructor.