我花了两天时间研究一个库或某种 PHP 函数来解析 GitHub 的 API 返回的 git 补丁,但我找不到任何可以使用的东西。
我能够从 Phabricator 中取出相当多的代码,但它有太多的依赖项。
有谁知道 php 代码会解析如下内容:
commit d87635a1001a23d3a20422fcc8f2484b6bf3ab1b
Author: Stav <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="443730042329252d286a272b29" rel="noreferrer noopener nofollow">[email protected]</a>>
Date: Thu Dec 6 23:59:16 2012 +0200
#666390 test chargify create and cancel subscription.
diff --git a/.gitignore b/.gitignore
index 796276b..b7fc8a1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
application/config/config.php
-application/config/database.php
\ No newline at end of file
+application/config/database.php
+/local_file.php
diff --git a/testing/Unit/libs/ChargifyTest.php b/testing/Unit/libs/ChargifyTest.php
new file mode 100644
index 0000000..f6ea674
--- /dev/null
+++ b/testing/Unit/libs/ChargifyTest.php
@@ -0,0 +1,38 @@
+<?php
+
+require_once dirname(__FILE__).'/../../../local_file.php';
+
像这样的 html 表:
提前谢谢你:)
最佳答案
这是我的想法。
<? foreach( $commit['files'] as $file ) { ?>
<div><h3><?=$file['filename']?></h3>
<?
$firstLine = true;
$patch = explode("@@",$file['patch']);
$lines_info = explode(",",$patch[1]);
$st_ln_num = substr($lines_info[0], 2);
$start_line['original'] = $st_ln_num;
$start_line['left'] = $st_ln_num;
$start_line['right'] = $st_ln_num;
$lines = explode("\n",$file['patch']);
?>
<div style="overflow: auto">
<table class="parseDiff" cellpadding="0" cellspacing="0">
<? foreach ($lines as $line) {
if (! $firstLine) {
$line_left = "";
$line_right = "";
$char = strlen($line) ? $line[0] : '~';
$type = "neutral";
switch ($char) {
case '-':
$line_left = $start_line['left']++;
$type = "removed";
$line = $line;
break;
case '+':
$line_right = $start_line['right']++;
$type = "added";
$line = $line;
break;
default:
$line_left = $start_line['left']++;
$line_right = $start_line['right']++;
$type = "neutral";
break;
}
?>
<tr class="line-type-<?=$type?>">
<td class="line-number line-number-left"><?=$line_left?></td>
<td class="line-number line-number-right"><?=$line_right?></td>
<td class="line-code"><pre><?=htmlspecialchars($line)?></pre></td>
</tr>
<?
} else {
?>
<tr class="line-type-first">
<td class="line-number line-number-left">···</td>
<td class="line-number line-number-right">···</td>
<td class="line-code"><pre><?=htmlspecialchars($line)?></pre></td>
</tr>
<?
$firstLine = false;
} // end if firstLine
} // end foreach
?>
</table>
</div>
</div>
<? } ?>
关于php - HTML 解析器的 Git 补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791651/