Skip to content

Commit

Permalink
✅python语言高亮支持
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Nov 5, 2021
1 parent 6795d72 commit 533166d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
25 changes: 19 additions & 6 deletions src/main/java/com/mewhz/paste/controller/CodeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CodeController {

@RequestMapping("/code")
@ResponseBody
public String code(@RequestParam String id){
public String code(@RequestParam String id) {
String htmlFront = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
Expand All @@ -30,14 +30,18 @@ public String code(@RequestParam String id){
String text = null;
String htmlLast = " </code>\n" +
" </pre>\n" +
" <script src=\"js/prism.js\"></script>\n" +
" <script src=\"js/prism-c.min.js\"></script>\n" +
" <script src=\"js/prism-cpp.min.js\"></script>\n" +
" <script src=\"js/prism-java.min.js\"></script>\n" +
"</body>\n" +
" <script src=\"js/prism.js\"></script>\n";

String htmlEnd = "</body>\n" +
"</html>";
StringBuilder html = new StringBuilder(htmlFront);

String cHtml = "<script src=\"js/prism-c.min.js\"></script>\n" +
"<script src=\"js/prism-cpp.min.js\"></script>\n";
String javaHtml = "<script src=\"js/prism-java.min.js\"></script>\n";
String pythonHtml = "<script src=\"js/prism-python.min.js\"></script>\n";


CodeSQL codesql = new CodeSQL();
List<Entity> list = codesql.findCode(id);

Expand All @@ -50,6 +54,15 @@ public String code(@RequestParam String id){
text = text.replaceAll(" ", "&nbsp;");

html.append(type).append("\">").append(text).append(htmlLast);
if ("cpp".equals(type)){
html.append(cHtml);
}
else if ("java".equals(type)){
html.append(javaHtml);
}
else if ("python".equals(type)){
html.append(pythonHtml);
}

return html.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public String code(@RequestParam String text, @RequestParam String type, @Reques
System.out.println(code);
CodeSQL codeSql = new CodeSQL();
codeSql.insertCode(code);
codeSql.insertIdentifyingCode(identifyingCode);
if (!"".equals(identifying)){
codeSql.insertIdentifyingCode(identifyingCode);
}
return "redirect:/code?id=" + code.getDate().getTime();
}
}
10 changes: 6 additions & 4 deletions src/main/resources/static/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
</head>
<body>
<pre>
<code class="language-cpp">
<code class="language-python">
print("Hello World")
</code>
</pre>
<script src="js/prism.js"></script>
<script src="js/prism-c.min.js"></script>
<script src="js/prism-cpp.min.js"></script>
<!-- <script src="js/prism-c.min.js"></script>-->
<!-- <script src="js/prism-cpp.min.js"></script>-->
<script src="js/prism-java.min.js"></script>
<script src="js/prism-python-min.js"></script>
</body>
</html>
</html>
5 changes: 3 additions & 2 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ <h1>
</div>
<div class="control-group">
<label>
<input class="form-control" name="identifying" type="text" placeholder="识别码"/>
<input class="form-control" name="identifying" type="text" placeholder="识别码(非必填)"/>
</label>
<label>
<select class="form-control" name="type">
<option value="cpp">cpp</option>
<option value="cpp">c/c++</option>
<option value="java">java</option>
<option value="python">python</option>
</select>
</label>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/static/js/prism-python.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 533166d

Please sign in to comment.