|
| 1 | +package com.github.difflib.examples; |
| 2 | + |
| 3 | +import com.github.difflib.UnifiedDiffUtils; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileWriter; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +// According to the original text, an html will be generated by comparing the text. |
| 14 | +public class generateDiffHtmlTest { |
| 15 | + |
| 16 | + /** |
| 17 | + * Here's a simple example of getting a nice html page based on the original text and the contrasted text, |
| 18 | + * Read n1.txt and n2.txt of D disk, and finally generate an html file |
| 19 | + * |
| 20 | + */ |
| 21 | + @Test |
| 22 | + public static void generateOriginalAndDiffDemo(){ |
| 23 | + List<String> origLines = getFileContent("D:\\n1.txt"); |
| 24 | + List<String> revLines = getFileContent("D:\\n2.txt"); |
| 25 | + List<String> originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines); |
| 26 | + |
| 27 | + //generateDiffHtml |
| 28 | + generateDiffHtml(originalAndDiff,"D:\\diff.html"); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * get file content |
| 33 | + * |
| 34 | + * @param filePath file path |
| 35 | + */ |
| 36 | + public static List<String> getFileContent(String filePath) { |
| 37 | + //原始文件 |
| 38 | + List<String> fileContent = null; |
| 39 | + File file = new File(filePath); |
| 40 | + try { |
| 41 | + fileContent = Files.readAllLines(file.toPath()); |
| 42 | + } catch (IOException e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + return fileContent; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * The html file is generated by the difference diff between the two files, |
| 50 | + * and the detailed content of the file comparison can be seen by opening the html file |
| 51 | + * |
| 52 | + * @param diffString The comparison result obtained by calling the above diffString method |
| 53 | + * @param htmlPath Generated html path,e.g:/user/var/mbos/ent/21231/diff.html |
| 54 | + */ |
| 55 | + public static void generateDiffHtml(List<String> diffString, String htmlPath) { |
| 56 | + StringBuilder builder = new StringBuilder(); |
| 57 | + for (String line : diffString) { |
| 58 | + builder.append(line); |
| 59 | + builder.append("\n"); |
| 60 | + } |
| 61 | + String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css"; |
| 62 | + String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css"; |
| 63 | + String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"; |
| 64 | + |
| 65 | + String template = "<!DOCTYPE html>\n" + |
| 66 | + "<html lang=\"en-us\">\n" + |
| 67 | + " <head>\n" + |
| 68 | + " <meta charset=\"utf-8\" />\n" + |
| 69 | + " <link rel=\"stylesheet\" href=\"" + githubCss + "\" />\n" + |
| 70 | + " <link rel=\"stylesheet\" type=\"text/css\" href=\"" + diff2htmlCss + "\" />\n" + |
| 71 | + " <script type=\"text/javascript\" src=\"" + diff2htmlJs + "\"></script>\n" + |
| 72 | + " </head>\n" + |
| 73 | + " <script>\n" + |
| 74 | + " const diffString = `\n" + |
| 75 | + "temp\n" + |
| 76 | + "`;\n" + |
| 77 | + "\n" + |
| 78 | + "\n" + |
| 79 | + " document.addEventListener('DOMContentLoaded', function () {\n" + |
| 80 | + " var targetElement = document.getElementById('myDiffElement');\n" + |
| 81 | + " var configuration = {\n" + |
| 82 | + " drawFileList: true,\n" + |
| 83 | + " fileListToggle: true,\n" + |
| 84 | + " fileListStartVisible: true,\n" + |
| 85 | + " fileContentToggle: true,\n" + |
| 86 | + " matching: 'lines',\n" + |
| 87 | + " outputFormat: 'side-by-side',\n" + |
| 88 | + " synchronisedScroll: true,\n" + |
| 89 | + " highlight: true,\n" + |
| 90 | + " renderNothingWhenEmpty: true,\n" + |
| 91 | + " };\n" + |
| 92 | + " var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);\n" + |
| 93 | + " diff2htmlUi.draw();\n" + |
| 94 | + " diff2htmlUi.highlightCode();\n" + |
| 95 | + " });\n" + |
| 96 | + " </script>\n" + |
| 97 | + " <body>\n" + |
| 98 | + " <div id=\"myDiffElement\"></div>\n" + |
| 99 | + " </body>\n" + |
| 100 | + "</html>"; |
| 101 | + template = template.replace("temp", builder.toString()); |
| 102 | + FileWriter fileWriter = null; |
| 103 | + try { |
| 104 | + fileWriter = new FileWriter(htmlPath); |
| 105 | + BufferedWriter buf = new BufferedWriter(fileWriter); |
| 106 | + buf.write(template); |
| 107 | + buf.close(); |
| 108 | + fileWriter.close(); |
| 109 | + } catch (IOException e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
0 commit comments