Skip to content

Commit 340fb0e

Browse files
committed
fine tune and add more comments
1 parent 96da512 commit 340fb0e

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

modules/markup/console/console.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,16 @@ func (Renderer) CanRender(filename string, sniffedType typesniffer.SniffedType,
5252
rs := []rune(s)
5353
cnt := 0
5454
firstErrPos := -1
55+
isCtrlSep := func(p int) bool {
56+
return p < len(rs) && (rs[p] == ';' || rs[p] == 'm')
57+
}
5558
for i, c := range rs {
5659
if c == 0 {
5760
return false
5861
}
5962
if c == '\x1b' {
60-
match, c2, c3, c4, c5 := false, false, false, false, false
61-
if i+2 < len(rs) {
62-
match = rs[i+1] == '['
63-
c2 = rs[i+2] == ';' || rs[i+2] == 'm'
64-
}
65-
if i+3 < len(rs) {
66-
c3 = rs[i+3] == ';' || rs[i+3] == 'm'
67-
}
68-
if i+4 < len(rs) {
69-
c4 = rs[i+4] == ';' || rs[i+4] == 'm'
70-
}
71-
if i+5 < len(rs) {
72-
c5 = rs[i+5] == ';' || rs[i+5] == 'm'
73-
}
74-
if match && (c2 || c3 || c4 || c5) {
63+
match := i+1 < len(rs) && rs[i+1] == '['
64+
if match && (isCtrlSep(i+2) || isCtrlSep(i+3) || isCtrlSep(i+4) || isCtrlSep(i+5)) {
7565
cnt++
7666
}
7767
}
@@ -82,7 +72,7 @@ func (Renderer) CanRender(filename string, sniffedType typesniffer.SniffedType,
8272
if firstErrPos != -1 && firstErrPos != len(rs)-1 {
8373
return false
8474
}
85-
return cnt >= 2
75+
return cnt >= 2 // only render it as console output if there are at least two escape sequences
8676
}
8777

8878
// Render renders terminal colors to HTML with all specific handling stuff.

modules/markup/console/console_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func TestRenderConsole(t *testing.T) {
2222
{"\x1b[1;2m \x1b[123m 啊", `<span class="term-fg2"> 啊</span>`},
2323
{"\x1b[1;2m \x1b[123m \xef", `<span class="term-fg2"> �</span>`},
2424
{"\x1b[1;2m \x1b[123m \xef \xef", ``},
25+
{"\x1b[12", ``},
26+
{"\x1b[1", ``},
27+
{"\x1b[FOO\x1b[", ``},
28+
{"\x1b[mFOO\x1b[m", `FOO`},
2529
}
2630

2731
var render Renderer

routers/web/repo/view_file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ func prepareFileView(ctx *context.Context, entry *git.TreeEntry) {
236236
return
237237
}
238238

239+
// TODO: in the future maybe we need more accurate flags, for example:
240+
// * IsRepresentableAsText: some files are text, some are not
241+
// * IsRenderableXxx: some files are rendered by backend "markup" engine, some are rendered by frontend (pdf, 3d)
242+
// * DefaultViewMode: when there is no "display" query parameter, which view mode should be used by default, source or rendered
243+
239244
utf8Reader := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
240245
switch {
241246
case fInfo.fileSize >= setting.UI.MaxDisplayFileSize:

web_src/js/features/file-view.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function showRenderRawFileButton(elFileView: HTMLElement, renderContainer: HTMLE
2323
const displayingRendered = Boolean(renderContainer);
2424
toggleClass(toggleButtons.querySelectorAll('.file-view-toggle-source'), 'active', !displayingRendered); // it may not exist
2525
toggleClass(toggleButtons.querySelector('.file-view-toggle-rendered'), 'active', displayingRendered);
26+
// TODO: if there is only one button, hide it?
2627
}
2728

2829
async function renderRawFileToContainer(container: HTMLElement, rawFileLink: string, mimeType: string) {

web_src/js/render/plugins/3d-viewer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ export function newRenderPlugin3DViewer(): FileRenderPlugin {
2424
// .ifc, .igs, .iges, .stp, .step are: TEXT
2525
// .stl .ply: TEXT or BINARY
2626
// .obj .off .wrl: TEXT
27-
// So we need to be able to render when the file is recognized as plaintext file by backend
27+
// So we need to be able to render when the file is recognized as plaintext file by backend.
28+
//
29+
// It needs more logic to make it overall right (render a text 3D model automatically):
30+
// we need to distinguish the ambiguous filename extensions.
31+
// For example: "*.obj, *.off, *.step" might be or not be a 3D model file.
32+
// So when it is a text file, we can't assume that "we only render it by 3D plugin",
33+
// otherwise the end users would be impossible to view its real content when the file is not a 3D model.
2834
const SUPPORTED_EXTENSIONS = [
2935
'.3dm', '.3ds', '.3mf', '.amf', '.bim', '.brep',
3036
'.dae', '.fbx', '.fcstd', '.glb', '.gltf',
@@ -41,6 +47,7 @@ export function newRenderPlugin3DViewer(): FileRenderPlugin {
4147
},
4248

4349
async render(container: HTMLElement, fileUrl: string): Promise<void> {
50+
// TODO: height and/or max-height?
4451
const OV = await import(/* webpackChunkName: "online-3d-viewer" */'online-3d-viewer');
4552
const viewer = new OV.EmbeddedViewer(container, {
4653
backgroundColor: new OV.RGBAColor(59, 68, 76, 0),

0 commit comments

Comments
 (0)