简单几行代码实现WordPress支持plantuml

Terwer 经验分享评论54字数 2487阅读8分17秒阅读模式

由于目前 PHP 操作 DOM 生成 HTMl 还没找到好办法,因此,我只是在内容末尾添加并生成 plantuml文章源自浅海拾贝-https://blog.terwergreen.com/simple-line-code-to-implement-wordpress-support-plantuml-2awddw.html

代码实现

代码如下:文章源自浅海拾贝-https://blog.terwergreen.com/simple-line-code-to-implement-wordpress-support-plantuml-2awddw.html

// plantuml相关函数
//function appendHTML(DOMNode $parent, $source) {
//  $tmpDoc = new DOMDocument();
//  $tmpDoc->loadHTML($source);
//  foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) {
//      $node = $parent->ownerDocument->importNode($node, true);
//      $parent->appendChild($node);
//  }
//}

function encodep($text) {
    $data = utf8_encode($text);
    $compressed = gzdeflate($data, 9);
    return encode64($compressed);
}

function encode6bit($b) {
    if ($b < 10) {
        return chr(48 + $b);
    }
    $b -= 10;
    if ($b < 26) {
        return chr(65 + $b);
    }
    $b -= 26;
    if ($b < 26) {
        return chr(97 + $b);
    }
    $b -= 26;
    if ($b == 0) {
        return '-';
    }
    if ($b == 1) {
        return '_';
    }
    return '?';
}

function append3bytes($b1, $b2, $b3) {
    $c1 = $b1 >> 2;
    $c2 = (($b1 & 0x3) << 4) | ($b2 >> 4);
    $c3 = (($b2 & 0xF) << 2) | ($b3 >> 6);
    $c4 = $b3 & 0x3F;
    $r = "";
    $r .= encode6bit($c1 & 0x3F);
    $r .= encode6bit($c2 & 0x3F);
    $r .= encode6bit($c3 & 0x3F);
    $r .= encode6bit($c4 & 0x3F);
    return $r;
}

function encode64($c) {
    $str = "";
    $len = strlen($c);
    for ($i = 0; $i < $len; $i+=3) {
        if ($i+2==$len) {
            $str .= append3bytes(ord(substr($c, $i, 1)), ord(substr($c, $i+1, 1)), 0);
        } else if ($i+1==$len) {
            $str .= append3bytes(ord(substr($c, $i, 1)), 0, 0);
        } else {
            $str .= append3bytes(ord(substr($c, $i, 1)), ord(substr($c, $i+1, 1)),
                ord(substr($c, $i+2, 1)));
        }
    }
    return $str;
}

// plantuml
add_filter( 'the_content', function ( $content ) {
    // print_r('<pre><code>' . base64_encode($content) . '</code></pre>');

    $dom = new DOMDocument;
    $dom->loadHTML( $content );
    $xpath = new DOMXPath( $dom );

    $uml_str = "";
    $idx     = 0;

    foreach ( $xpath->query( "//code[@class='plantuml']" ) as $section ) {
        $base64_uml= "SoWkIImgAStDuNBEIImkDZ1KiAdHrLM0S8oWWiOAMd0n4wYOgK8-NCmCAcQkeAS75RA02bagm5GP6d0vfEQb0Aq20000";

        // search for sub nodes inside each element
        $base64_uml = encodep($section->nodeValue);
        // var_dump( $section->nodeValue );

        $plantuml_img = "http://www.plantuml.com/plantuml/png/" . $base64_uml;
        $current_uml  = '<p>' . $idx . '.<a href="' . $plantuml_img . '" target="_blank"><img src="' . $plantuml_img . '" /></a></p>';
        $uml_str      = $uml_str . $current_uml;
        $idx ++;
    }

    if($uml_str != ""){
        $uml_str = '' . $uml_str;
    }

    return $content . $uml_str;
} );

查看效果

/mybatis-s-annotation-achieves-complex-mapping-development-z1l39xe.html文章源自浅海拾贝-https://blog.terwergreen.com/simple-line-code-to-implement-wordpress-support-plantuml-2awddw.html

@startuml

!include https://unpkg.com/plantuml-style-c4@latest/core.puml
' uncomment the following line and comment the first to use locally
'!include core.puml

left to right direction

class sys_role {
   rolename: varchar(255)
   roleDesc: varchar(255)
   id: int(11)
}
class sys_user_role {
   userid: int(11)
   roleid: int(11)
}
class user {
   username: varchar(50)
   password: varchar(50)
   birthday: varchar(50)
   id: int(11)
}

sys_user_role  -[#595959,plain]-^  sys_role      : "roleid:id"
sys_user_role  -[#595959,plain]-^  user          : "userid:id"
@enduml

这里是正文中的plantuml图表

0.简单几行代码实现WordPress支持plantuml文章源自浅海拾贝-https://blog.terwergreen.com/simple-line-code-to-implement-wordpress-support-plantuml-2awddw.html

相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: