Skip to content

Commit

Permalink
Use pygments support code highlight dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
manateelazycat committed Mar 26, 2022
1 parent 31f66ef commit b239d64
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 25 deletions.
49 changes: 43 additions & 6 deletions buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,17 @@ def get_file_mime(self, file_path):
if file_path.endswith(".vue"):
return "text-plain"
else:
return self.mime_db.mimeTypeForFile(file_info).name().replace("/", "-")
mime = self.mime_db.mimeTypeForFile(file_info).name().replace("/", "-")

if (mime.startswith("text-") or
mime == "application-json" or
mime == "application-x-yaml" or
mime == "application-javascript"):
mime = "eaf-mime-type-code-html"
elif mime == "application-x-sharedlib" or mime == "application-xmind":
mime = "eaf-mime-type-ignore"

return mime

def generate_file_icon(self, file_path):
file_mime = self.get_file_mime(file_path)
Expand Down Expand Up @@ -424,8 +434,34 @@ def update_preview(self, file):
def get_preview_file(self):
return self.preview_file

def update_preview_info(self, file, file_type, file_infos):
self.buffer_widget.eval_js('''setPreview(\"{}\", \"{}\", {});'''.format(file, file_type, file_infos))
def update_preview_info(self, file, file_type, file_mime, file_infos):
file_html_content = ""

if file_type == "file":
if file_mime == "eaf-mime-type-code-html":

file_size = os.path.getsize(file)
if file_size < 100000:
from pygments import highlight
from pygments.styles import get_all_styles
from pygments.lexers import PythonLexer, get_lexer_for_filename
from pygments.formatters import HtmlFormatter

with open(file) as f:
content = f.read()

try:
# All styles please look: https://pygments.org/styles/
style_name = "monokai" if self.theme_mode == "dark" else "stata-light"

file_html_content = highlight(content, get_lexer_for_filename(file), HtmlFormatter(full=True, style=style_name))
print(content, file_html_content)
except:
file_html_content = highlight(content, PythonLexer(), HtmlFormatter())
else:
file_mime = "eaf-mime-type-code"

self.buffer_widget.eval_js('''setPreview(\"{}\", \"{}\", \"{}\", {}, {});'''.format(file, file_type, file_mime, json.dumps(file_html_content), file_infos))

@interactive
def search_file(self):
Expand Down Expand Up @@ -1008,7 +1044,7 @@ def resize_view(self):

class FetchPreviewInfoThread(QThread):

fetch_finish = QtCore.pyqtSignal(str, str, str)
fetch_finish = QtCore.pyqtSignal(str, str, str, str)

def __init__(self, file, get_preview_file_callback, get_files_callback, get_file_mime_callback):
QThread.__init__(self)
Expand All @@ -1028,11 +1064,12 @@ def run(self):

if self.file != "":
path = Path(self.file)
mime = self.get_file_mime_callback(str(path.absolute()))

if path.is_file():
file_type = "file"
file_infos = [{
"mime": self.get_file_mime_callback(str(path.absolute())),
"mime": mime,
"size": os.path.getsize(str(path.absolute()))
}]
elif path.is_dir():
Expand All @@ -1041,7 +1078,7 @@ def run(self):
elif path.is_symlink():
file_type = "symlink"

self.fetch_finish.emit(self.file, file_type, json.dumps(file_infos))
self.fetch_finish.emit(self.file, file_type, mime, json.dumps(file_infos))

class GitCommitThread(QThread):

Expand Down
10 changes: 8 additions & 2 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
],
"pip": {
"linux": [
"pypinyin"
"pypinyin",
"pygments"
],
"win32": [
"pypinyin"
"pypinyin",
"pygments"
],
"darwin": [
"pypinyin",
"pygments"
]
},
"vue_install": true
Expand Down
41 changes: 24 additions & 17 deletions src/components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@
<PreviewImage v-if="previewType == 'file' && previewMime == 'image'" :file="previewPath + '?' + Math.random().toString()"/>
<PreviewHtml v-if="previewType == 'file' && previewMime == 'html'" :file="previewPath"/>
<PreviewCode
v-if="previewType == 'file' && previewMime == 'text'"
v-if="previewType == 'file' && previewMime == 'code'"
:file="previewPath"
:size="previewSize"
:backgroundColor="backgroundColor"/>
<PreviewCodeHtml
v-if="previewType == 'file' && previewMime == 'code-html'"
:file="previewPath"
:size="previewSize"
:backgroundColor="backgroundColor"
:content="previewHtmlContent"/>
<PreviewPdf v-if="previewType == 'file' && previewMime == 'pdf'" :file="previewPath"/>
<PreviewVideo v-if="previewType == 'file' && previewMime == 'video'" :file="previewPath"/>
<PreviewAudio v-if="previewType == 'file' && previewMime == 'audio'" :file="previewPath" :barColor="foregroundColor"/>
Expand All @@ -79,6 +85,7 @@
import PreviewAudio from "./PreviewAudio.vue"
import PreviewPdf from "./PreviewPdf.vue"
import PreviewCode from "./PreviewCode.vue"
import PreviewCodeHtml from "./PreviewCodeHtml.vue"
import PreviewHtml from "./PreviewHtml.vue"
import PreviewImage from "./PreviewImage.vue"
import PreviewEmpty from "./PreviewEmpty.vue"
Expand All @@ -93,6 +100,7 @@
PreviewAudio,
PreviewPdf,
PreviewCode,
PreviewCodeHtml,
PreviewHtml,
PreviewImage,
PreviewEmpty,
Expand Down Expand Up @@ -154,6 +162,7 @@
previewType: "",
previewFiles: [],
previewMime: "",
previewHtmlContent: "",
previewSize: "",
pathSep: "",
Expand Down Expand Up @@ -531,34 +540,32 @@
this.showPreview = option;
},
setPreview(filePath, fileType, fileInfos) {
setPreview(filePath, fileType, fileMime, fileHtmlContent, fileInfos) {
this.previewPath = filePath;
this.previewType = fileType;
this.previewFiles = fileInfos;
this.previewHtmlContent = fileHtmlContent;
if (fileType == "file") {
var mime = fileInfos[0]["mime"]
console.log("***** ", filePath, mime)
console.log("***** ", filePath, fileMime)
if (mime.startsWith("image-")) {
if (fileMime.startsWith("image-")) {
this.previewMime = "image"
} else if (mime == "text-html") {
} else if (fileMime == "text-html") {
this.previewMime = "html"
} else if (mime.startsWith("text-")
|| mime == "application-json"
|| mime == "application-x-yaml") {
this.previewMime = "text"
} else if (mime == "application-pdf") {
} else if (fileMime == "eaf-mime-type-code") {
this.previewMime = "code"
} else if (fileMime == "eaf-mime-type-code-html") {
this.previewMime = "code-html"
} else if (fileMime == "application-pdf") {
this.previewMime = "pdf"
} else if (mime.startsWith("video-")) {
} else if (fileMime.startsWith("video-")) {
this.previewMime = "video"
} else if (mime.startsWith("audio-")) {
} else if (fileMime.startsWith("audio-")) {
this.previewMime = "audio"
} else if (mime == "application-wps-office.docx") {
} else if (fileMime == "application-wps-office.docx") {
this.previewMime = "office"
} else if (mime == "application-x-sharedlib" ||
mime == "application-xmind"
) {
} else if (fileMime == "eaf-mime-type-ignore") {
this.previewMime = "ignore"
}
Expand Down
77 changes: 77 additions & 0 deletions src/components/PreviewCodeHtml.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<template>
<div ref="scrollArea" class="code-box">
<div v-html="content"/>
</div>
</template>

<script>
export default {
name: 'PreviewCodeHtml',
components: {
},
props: {
file: String,
size: Number,
backgroundColor: String,
content: String
},
watch: {
file: function() {
this.$refs.scrollArea.scrollTop = 0;
}
},
mounted() {
var that = this;
this.$root.$on("previewToggle", function() {
that.scrollUp();
});
this.$root.$on("scrollUp", function() {
that.scrollUp();
});
this.$root.$on("scrollDown", function() {
that.scrollDown();
});
this.$root.$on("scrollUpLine", function() {
that.scrollUpLine();
});
this.$root.$on("scrollDownLine", function() {
that.scrollDownLine();
});
},
created() {
},
methods: {
scrollUp() {
this.$refs.scrollArea.scrollTop = this.$refs.scrollArea.scrollTop + this.$refs.scrollArea.clientHeight;
},
scrollDown() {
this.$refs.scrollArea.scrollTop = this.$refs.scrollArea.scrollTop - this.$refs.scrollArea.clientHeight;
},
scrollUpLine() {
this.$refs.scrollArea.scrollTop = this.$refs.scrollArea.scrollTop + 50;
},
scrollDownLine() {
this.$refs.scrollArea.scrollTop = this.$refs.scrollArea.scrollTop - 50;
}
}
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.code-box {
width: 100%;
height: 100%;
overflow: scroll;
padding-left: 15px;
font-size: 18px;
}
</style>

0 comments on commit b239d64

Please sign in to comment.