Skip to content

Commit

Permalink
Better page titles
Browse files Browse the repository at this point in the history
  • Loading branch information
apardyl committed Jun 23, 2019
1 parent f171c9f commit e41de5f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,50 @@ class FilesystemController(
return pathBreadcrumb
}

private fun createTitle(path: String): String {
return path.trim('/').replace("/", " / ") + (if (path.isNotEmpty()) " - " else "") + "Mordor"
}

private fun urlEncodePath(path: String): String {
return UriUtils.encodePath(path, "UTF-8")
}

private fun previewText(entity: RepositoryFile): ModelAndView {
private fun previewText(entity: RepositoryFile, path: String): ModelAndView {
if (entity.file.length() > maxTextBytes) {
return ModelAndView("preview_too_large", mapOf(
"title" to createTitle(path),
"path" to createBreadcrumb(entity),
"download" to "/download/${entity.relativePath}"
))
}
val text = FileUtils.readFileToString(entity.file, "utf-8")
// TODO: detect encoding
return ModelAndView("preview_code", mapOf(
"title" to createTitle(path),
"text" to text,
"path" to createBreadcrumb(entity),
"download" to "/download/${entity.relativePath}"
))
}

private fun previewImage(entity: RepositoryFile): ModelAndView {
private fun previewImage(entity: RepositoryFile, path: String): ModelAndView {
if (entity.file.length() > maxImageBytes) {
return ModelAndView("preview_too_large", mapOf(
"title" to createTitle(path),
"path" to createBreadcrumb(entity),
"download" to "/download/${entity.relativePath}"
))
}
return ModelAndView("preview_image", mapOf(
"title" to createTitle(path),
"path" to createBreadcrumb(entity),
"download" to "/download/${entity.relativePath}"
))
}

private fun previewPage(entity: RepositoryFile, path: String): ModelAndView {
return ModelAndView("preview_page", mapOf(
"title" to createTitle(path),
"raw" to "/raw/$path",
"path" to createBreadcrumb(entity),
"download" to "/download/${entity.relativePath}"
Expand All @@ -111,12 +120,15 @@ class FilesystemController(
if (entry is RepositoryDirectory) "/" else "", entry.name, iconNameProvider.getIconName(entry))
}

return ModelAndView("tree", mapOf("children" to sortedChildren, "path" to createBreadcrumb(entity)))
return ModelAndView("tree", mapOf(
"title" to createTitle(path),
"children" to sortedChildren,
"path" to createBreadcrumb(entity)))
} else if (entity is RepositoryFile) {
when {
entity.isPage -> return previewPage(entity, path)
entity.mimeType.startsWith("text/") || entity.isCode -> return previewText(entity)
entity.isDisplayableImage -> return previewImage(entity)
entity.mimeType.startsWith("text/") || entity.isCode -> return previewText(entity, path)
entity.isDisplayableImage -> return previewImage(entity, path)
}
}
return ModelAndView(RedirectView(urlEncodePath("/download/${entity.relativePath}")))
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/preview_code.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en"
layout:decorate="~{layout}">
<head>
<title>Mordor</title>
<title th:text="${title}">Mordor</title>
<link rel="stylesheet"
href="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/github.min.css">
<script src="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/preview_image.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en"
layout:decorate="~{layout}">
<head>
<title>Mordor</title>
<title th:text="${title}">Mordor</title>
</head>
<body>
<div layout:fragment="content">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/preview_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en"
layout:decorate="~{layout}" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<title>Mordor</title>
<title th:text="${title}">Mordor</title>
</head>
<body>
<div layout:fragment="content">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/preview_too_large.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en"
layout:decorate="~{layout}">
<head>
<title>Mordor</title>
<title th:text="${title}">Mordor</title>
</head>
<body>
<div layout:fragment="content">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en"
layout:decorate="~{layout}">
<head>
<title>Mordor</title>
<title th:text="${title}">Mordor</title>
</head>
<body>
<div layout:fragment="content">
Expand Down

0 comments on commit e41de5f

Please sign in to comment.