OSDN Git Service

差分表示にjsdifflibを使うようにしてみた。
authortakezoe <takezoe@users.osdn.me>
Sun, 15 Apr 2012 16:25:42 +0000 (16:25 +0000)
committertakezoe <takezoe@users.osdn.me>
Sun, 15 Apr 2012 16:25:42 +0000 (16:25 +0000)
plugin/core/Diff.pm

index 03df6b2..08cfed0 100644 (file)
@@ -167,14 +167,45 @@ sub show_diff {
        $wiki->set_title("$page¤ÎÊѹ¹ÅÀ");
        my ($diff, $rollback) = $self->get_diff_html($wiki,$page, $from, $to);
        
-       $diff =~ s/\n/<br>/g;
-       
+       my $theme_uri = $wiki->config('theme_uri');
        my $buf = qq|
-               <ul>
-                 <li>Äɲ䵤줿Éôʬ¤Ï<ins class="diff">¤³¤Î¤è¤¦¤Ë</ins>ɽ¼¨¤µ¤ì¤Þ¤¹¡£</li>
-                 <li>ºï½ü¤µ¤ì¤¿Éôʬ¤Ï<del class="diff">¤³¤Î¤è¤¦¤Ë</del>ɽ¼¨¤µ¤ì¤Þ¤¹¡£</li>
-               </ul>
-               <div class="diff">$diff</div>
+<script type="text/javascript" src="${theme_uri}/resources/jsdifflib/difflib.js"></script>
+<script type="text/javascript" src="${theme_uri}/resources/jsdifflib/diffview.js"></script>
+<link href="${theme_uri}/resources/jsdifflib/diffview.css" type="text/css" rel="stylesheet" />
+<script type="text/javascript">
+function diffUsingJS() {\r
+    // get the baseText and newText values from the two textboxes, and split them into lines\r
+    var base   = difflib.stringAsLines(document.getElementById("baseText").value);\r
+    var newtxt = difflib.stringAsLines(document.getElementById("newText").value);\r
+\r
+    // create a SequenceMatcher instance that diffs the two sets of lines\r
+    var sm = new difflib.SequenceMatcher(base, newtxt);\r
+\r
+    // get the opcodes from the SequenceMatcher instance\r
+    // opcodes is a list of 3-tuples describing what changes should be made to the base text\r
+    // in order to yield the new text\r
+    var opcodes = sm.get_opcodes();\r
+    var diffoutputdiv = document.getElementById("diffoutputdiv")\r
+    while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);\r
+\r
+    // build the diff view and add it to the current DOM\r
+    diffoutputdiv.appendChild(diffview.buildView({\r
+        baseTextLines: base,\r
+        newTextLines: newtxt,\r
+        opcodes: opcodes,\r
+        // set the display titles for each resource\r
+        baseTextName: "Base Text",\r
+        newTextName: "New Text",\r
+        contextSize: null,\r
+        viewType: 1 // 1 or 0\r
+    }));\r
+}
+</script>
+$diff
+<div id="diffoutputdiv"/>
+<script type="text/javascript">
+  diffUsingJS();
+</script>
        |;
        
        if($wiki->can_modify_page($page) && $rollback && $wiki->get_CGI->param('diff') eq ''){
@@ -245,11 +276,6 @@ sub get_diff_html {
        } else {
                $source1 = $wiki->get_page($page);
        }
-       if($wiki->config('diff_max') ne '' && $wiki->config('diff_max') > 0){
-               if(length($source1) > $wiki->config('diff_max')){
-                       return ('¥Ú¡¼¥¸¥µ¥¤¥º¤¬Â礭¤¤¤¿¤áº¹Ê¬¤òɽ¼¨¤Ç¤­¤Þ¤»¤ó¡£', 0);
-               }
-       }
        
        my $source2 = '';
        if($to ne ''){
@@ -257,101 +283,13 @@ sub get_diff_html {
        } else {
                $source2 = $wiki->get_page($page);
        }
-       if($wiki->config('diff_max') ne '' && $wiki->config('diff_max') > 0){
-               if(length($source2) > $wiki->config('diff_max')){
-                       return ('¥Ú¡¼¥¸¥µ¥¤¥º¤¬Â礭¤¤¤¿¤áº¹Ê¬¤òɽ¼¨¤Ç¤­¤Þ¤»¤ó¡£', 0);
-               }
-       }
-       
        my $format  = $wiki->get_edit_format();
        
        $source1 = $wiki->convert_from_fswiki($source1, $format);
        $source2 = $wiki->convert_from_fswiki($source2, $format);
        
-       return (&_get_diff_html($source1, $source2), $source2 ne "");
-}
-
-#==============================================================================
-# º¹Ê¬HTML¤òÀ¸À®¤¹¤ë´Ø¿ô
-#==============================================================================
-sub _get_diff_html {
-       my $source1 = shift;
-       my $source2 = shift;
-       
-       my @lines1 = split(/\n/,$source1);
-       my @lines2 = split(/\n/,$source2);
-       my $linesrefA = \@lines2;
-       my $linesrefB = \@lines1;
-       
-       my $diff_text = "";
-       my $del_buffer = "";
-       
-       traverse_sequences($linesrefA, $linesrefB, {
-               MATCH => sub {
-                       my ($a, $b) = @_;
-                       if($del_buffer ne ''){
-                               $diff_text .= "<del class=\"diff\">".Util::escapeHTML($del_buffer)."</del>\n";
-                               $del_buffer = '';
-                       }
-                       $diff_text .= Util::escapeHTML($linesrefA->[$a])."\n";
-               },
-               DISCARD_A => sub {
-                       my ($a, $b) = @_;
-                       $del_buffer .= $linesrefA->[$a]."\n";
-               },
-               DISCARD_B => sub {
-                       my ($a, $b) = @_;
-                       if($del_buffer eq ''){
-                               $diff_text .= "<ins class=\"diff\">".Util::escapeHTML($linesrefB->[$b])."</ins>\n";
-                               
-                       } else {
-                               my @msg1 = _str_jfold($linesrefB->[$b]."\n", 1);
-                               my @msg2 = _str_jfold($del_buffer, 1);
-                               my $msgrefA = \@msg2;
-                               my $msgrefB = \@msg1;
-                               
-                               traverse_sequences($msgrefA, $msgrefB, {
-                                       MATCH => sub {
-                                               my ($a, $b) = @_;
-                                               $diff_text .= Util::escapeHTML($msgrefA->[$a]);
-                                       },
-                                       DISCARD_A => sub {
-                                               my ($a, $b) = @_;
-                                               $diff_text .= "<del class=\"diff\">".Util::escapeHTML($msgrefA->[$a])."</del><wbr>";
-                                       },
-                                       DISCARD_B => sub {
-                                               my ($a, $b) = @_;
-                                               $diff_text .= "<ins class=\"diff\">".Util::escapeHTML($msgrefB->[$b])."</ins><wbr>";
-                                       }
-                               });
-                               
-                               $del_buffer = '';
-                       }
-               }
-       });
-               
-       if($del_buffer ne ''){
-               $diff_text .= "<del class=\"diff\">".Util::escapeHTML($del_buffer)."</del>\n";
-               $del_buffer = '';
-       }
-       
-       return $diff_text;
-}
-
-#==============================================================================
-# Ê¸»úÎó¤ò»ØÄêʸ»ú¿ô¤òʬ³ä
-#==============================================================================
-sub _str_jfold {\r
-  my $str    = shift;       #»ØÄêʸ»úÎó\r
-  my $byte   = shift;       #»ØÄê¥Ð¥¤¥È\r
-  my $j      = new Jcode($str);\r
-  my @result = ();\r
-\r
-  foreach my $buff ( $j->jfold($byte) ){\r
-    push(@result, $buff);\r
-  }\r
-\r
-  return(@result);\r
+       return '<input id="newText" type="hidden" value="'.Util::escapeHTML($source1).'">'.
+              '<input id="baseText" type="hidden" value="'.Util::escapeHTML($source2).'">';
 }
 
 #==============================================================================