padong4284 / padong-app

PADONG means wave in Korean. PADONG is a platform where people can come together to create an ocean of human waves.
5 stars 0 forks source link

Wiki History Compare #207

Closed VertexToEdge closed 3 years ago

VertexToEdge commented 3 years ago

https://pub.dev/packages/diff_match_patch APACHE 2.0 LICENSE

diff method will diff the old text and new text. it return what words deleted and inserted.

it is example.

import 'package:diff_match_patch/diff_match_patch.dart';
import 'dart:io';

String oldText = """
a = b
b = c
c = a
a = d
""";

String newText = """
a = b
b = d
c = a
""";

void main(List<String> arguments) {
  var diffs = diff(oldText, newText);
  for (var i in diffs) {
    if (i.operation == DIFF_DELETE) {
      if (i.text.endsWith("\n")) {  //check the deleted text is line
        stdout.write("--" + i.text.substring(0, i.text.length - 1) + "--\n"); //mark the deleted line
      } else {  //deleted words
        stdout.write("--" + i.text + "--");  // mark deleted words
      }
    } else if (i.operation == DIFF_INSERT) { // inserted text
      if (i.text.endsWith("\n")) {  //check the inserted text is line
        stdout.write("++" + i.text.substring(0, i.text.length - 1) + "++\n"); //mark the inserted line
      } else {  //deleted words
        stdout.write("++" + i.text + "++");  // mark inserted words
    } else if (i.operation == DIFF_EQUAL) {
      stdout.write(i.text);   //not modified plain text
    }
  }
}

Output:

a = b
b = --c--++d++
c = a
--a = d--
jtjun commented 3 years ago

markdown package

with custom syntax

스크린샷 2021-03-04 오후 7 20 12
jtjun commented 3 years ago
스크린샷 2021-03-04 오후 7 17 44
jtjun commented 3 years ago

Custom Syntax Example

jtjun commented 3 years ago
import 'package:charcode/charcode.dart';

import 'ast.dart';
import 'document.dart';
import 'util.dart';

// Parses preformatted code blocks between two ~~~ or ``` sequences.
//
// See the CommonMark spec: https://spec.commonmark.org/0.29/#fenced-code-blocks
class FencedCodeBlockSyntax extends BlockSyntax {
  @override
  RegExp get pattern => _codeFencePattern;

  const FencedCodeBlockSyntax();

  @override
  bool canParse(BlockParser parser) {
    final match = pattern.firstMatch(parser.current);
    if (match == null) return false;
    final codeFence = match.group(1)!;
    final infoString = match.group(2);
    // From the CommonMark spec:
    //
    // > If the info string comes after a backtick fence, it may not contain
    // > any backtick characters.
    return (codeFence.codeUnitAt(0) != $backquote ||
        !infoString!.codeUnits.contains($backquote));
  }

  @override
  List<String> parseChildLines(BlockParser parser, [String? endBlock]) {
    endBlock ??= '';
    var childLines = <String>[];
    parser.advance();
    while (!parser.isDone) {
      var match = pattern.firstMatch(parser.current);
      if (match == null || !match[1]!.startsWith(endBlock)) {
        childLines.add(parser.current);
        parser.advance();
      } else {
        parser.advance();
        break;
      }
    }
    return childLines;
  }

  @override
  Node parse(BlockParser parser) {
    // Get the syntax identifier, if there is one.
    var match = pattern.firstMatch(parser.current)!;
    var endBlock = match.group(1);
    var infoString = match.group(2)!;
    var childLines = parseChildLines(parser, endBlock);
    // The Markdown tests expect a trailing newline.
    childLines.add('');
    var text = childLines.join('\n');
    if (parser.document.encodeHtml) {
      text = escapeHtml(text);
    }
    var code = Element.text('code', text);
    // the info-string should be trimmed
    // http://spec.commonmark.org/0.22/#example-100
    infoString = infoString.trim();
    if (infoString.isNotEmpty) {
      // only use the first word in the syntax
      // http://spec.commonmark.org/0.22/#example-100
      var firstSpace = infoString.indexOf(' ');
      if (firstSpace >= 0) {
        infoString = infoString.substring(0, firstSpace);
      }
      if (parser.document.encodeHtml) {
        infoString = escapeHtmlAttribute(infoString);
      }
      code.attributes['class'] = 'language-$infoString';
    }
    var element = Element('pre', [code]);
    return element;
  }
}

CodeBlockSyntax

jtjun commented 3 years ago

Using CustomSyntax of Markdown is WRONG way.

use rich text

Styling Programmatically example

jtjun commented 3 years ago

partial styling

VertexToEdge commented 3 years ago

image Line Diffing

VertexToEdge commented 3 years ago

I didn't use diff method in diff_match_patch. only use Diff class for describing Diff action.

Todo:

  1. remove diff_match_patch dependency
  2. improve algorithm with https://neil.fraser.name/writing/diff/myers.pdf and https://neil.fraser.name/writing/diff/
jtjun commented 3 years ago

Remove diff_match_path dependency

스크린샷 2021-03-06 오전 9 26 34