Skip to content

A problem was found and fixed about #164 #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ private static void insertOrig(List<List<String>> diffList, List<String> result,
int end = nexMap.get("revRow") - 2;
insert(result, getOrigList(original, start, end));
}
int start = map.get("orgRow") + map.get("orgDel") - 1;
start = start == -1 ? 0 : start;
if (simb.contains("@@ -1,") && null == nexSimb && map.get("orgDel") != original.size()) {
insert(result, getOrigList(original, 0, original.size() - 1));
insert(result, getOrigList(original, start, original.size() - 1));
} else if (null == nexSimb && (map.get("orgRow") + map.get("orgDel") - 1) < original.size()) {
int start = map.get("orgRow") + map.get("orgDel") - 1;
start = start == -1 ? 0 : start;
insert(result, getOrigList(original, start, original.size() - 1));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ public void testGenerateOriginalAndDiff() {
System.out.println(originalAndDiff.stream().collect(joining("\n")));
}

@Test
public void testGenerateOriginalAndDiffFirstLineChange() {
List<String> origLines = null;
List<String> revLines = null;
try {
origLines = fileToLines(TestConstants.MOCK_FOLDER + "issue_170_original.txt");
revLines = fileToLines(TestConstants.MOCK_FOLDER + "issue_170_revised.txt");
} catch (IOException e) {
fail(e.getMessage());
}

List<String> originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);
System.out.println(originalAndDiff.stream().collect(joining("\n")));
}

public static List<String> fileToLines(String filename) throws FileNotFoundException, IOException {
List<String> lines = new ArrayList<>();
String line = "";
Expand Down
95 changes: 95 additions & 0 deletions java-diff-utils/src/test/resources/mocks/issue_170_original.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//According to the original text, an html will be generated by comparing the text
public class generateDiffHtmlTest {
/**
* Here's a simple example of getting a nice html page based on the original text and the contrasted text,Read n1.txt and n2.txt of D disk, and finally generate an html file
*
*/
@Test
public static void generateOriginalAndDiffDemo(){
List<String> origLines = getFileContent("D:\\n1.txt");
List<String> revLines =getFileContent("D:\\n2.txt");
List<String> originalAndDiff =UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);
//System.out.println(originalAndDiff.size());
generateDiffHtml(originalAndDiff,"D:\\diff.html");
}

/**
* get file content
* @param filePath file path
*/
public static List<String> getFileContent(String filePath) {
//origin
List<String> fileContent =null;
File file = new File(filePath);
try {
fileContent = Files.readAllLines(file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}

/**
* The html file is generated by the difference diff between the two files, and the detailed content of the file comparison can be seen by opening the html file
*
*/
public static void generateDiffHtml(List<String> diffString, String htmlPath) {
StringBuilder builder = new StringBuilder();
for (String line : diffString) {
builder.append(line);
builder.append("\n");
}
String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css";
String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css";
String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js";

String template = "<!DOCTYPE html>\n" +
"<html lang=\"en-us\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\" />\n" +
" <link rel=\"stylesheet\" href=\"" + githubCss + "\" />\n" +
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" + diff2htmlCss + "\" />\n" +
" <script type=\"text/javascript\" src=\"" + diff2htmlJs + "\"></script>\n" +
" </head>\n" +
" <script>\n" +
" const diffString = `\n" +
"temp\n" +
"`;\n" +
"\n" +
"\n" +
" document.addEventListener('DOMContentLoaded', function () {\n" +
" var targetElement = document.getElementById('myDiffElement');\n" +
" var configuration = {\n" +
" drawFileList: true,\n" +
" fileListToggle: true,\n" +
" fileListStartVisible: true,\n" +
" fileContentToggle: true,\n" +
" matching: 'lines',\n" +
" outputFormat: 'side-by-side',\n" +
" synchronisedScroll: true,\n" +
" highlight: true,\n" +
" renderNothingWhenEmpty: true,\n" +
" };\n" +
" var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);\n" +
" diff2htmlUi.draw();\n" +
" diff2htmlUi.highlightCode();\n" +
" });\n" +
" </script>\n" +
" <body>\n" +
" <div id=\"myDiffElement\"></div>\n" +
" </body>\n" +
"</html>";
template = template.replace("temp", builder.toString());
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(htmlPath);
BufferedWriter buf = new BufferedWriter(fileWriter);
buf.write(template);
buf.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
114 changes: 114 additions & 0 deletions java-diff-utils/src/test/resources/mocks/issue_170_revised.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.github.difflib.examples;

import com.github.difflib.UnifiedDiffUtils;
import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

// According to the original text, an html will be generated by comparing the text.
public class generateDiffHtmlTest {

/**
* Here's a simple example of getting a nice html page based on the original text and the contrasted text,
* Read n1.txt and n2.txt of D disk, and finally generate an html file
*
*/
@Test
public static void generateOriginalAndDiffDemo(){
List<String> origLines = getFileContent("D:\\n1.txt");
List<String> revLines = getFileContent("D:\\n2.txt");
List<String> originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);

//generateDiffHtml
generateDiffHtml(originalAndDiff,"D:\\diff.html");
}

/**
* get file content
*
* @param filePath file path
*/
public static List<String> getFileContent(String filePath) {
//原始文件
List<String> fileContent = null;
File file = new File(filePath);
try {
fileContent = Files.readAllLines(file.toPath());
} catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}

/**
* The html file is generated by the difference diff between the two files,
* and the detailed content of the file comparison can be seen by opening the html file
*
* @param diffString The comparison result obtained by calling the above diffString method
* @param htmlPath Generated html path,e.g:/user/var/mbos/ent/21231/diff.html
*/
public static void generateDiffHtml(List<String> diffString, String htmlPath) {
StringBuilder builder = new StringBuilder();
for (String line : diffString) {
builder.append(line);
builder.append("\n");
}
String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css";
String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css";
String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js";

String template = "<!DOCTYPE html>\n" +
"<html lang=\"en-us\">\n" +
" <head>\n" +
" <meta charset=\"utf-8\" />\n" +
" <link rel=\"stylesheet\" href=\"" + githubCss + "\" />\n" +
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" + diff2htmlCss + "\" />\n" +
" <script type=\"text/javascript\" src=\"" + diff2htmlJs + "\"></script>\n" +
" </head>\n" +
" <script>\n" +
" const diffString = `\n" +
"temp\n" +
"`;\n" +
"\n" +
"\n" +
" document.addEventListener('DOMContentLoaded', function () {\n" +
" var targetElement = document.getElementById('myDiffElement');\n" +
" var configuration = {\n" +
" drawFileList: true,\n" +
" fileListToggle: true,\n" +
" fileListStartVisible: true,\n" +
" fileContentToggle: true,\n" +
" matching: 'lines',\n" +
" outputFormat: 'side-by-side',\n" +
" synchronisedScroll: true,\n" +
" highlight: true,\n" +
" renderNothingWhenEmpty: true,\n" +
" };\n" +
" var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);\n" +
" diff2htmlUi.draw();\n" +
" diff2htmlUi.highlightCode();\n" +
" });\n" +
" </script>\n" +
" <body>\n" +
" <div id=\"myDiffElement\"></div>\n" +
" </body>\n" +
"</html>";
template = template.replace("temp", builder.toString());
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(htmlPath);
BufferedWriter buf = new BufferedWriter(fileWriter);
buf.write(template);
buf.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}