First of all, the principle of implementation, of course, this article mainly about my implementation, of course, there are other ways of implementation, the principle is similar.
First of all, we need to install the Kramdown interpreter, this is very good to understand, and PHP, his feature tag can achieve rendering must be interpreted by the interpreter, here Kramdown Interpreter's official website for http://kramdown.gettalong.org/ Installation.html, this is the installation interface, installed according to your own system, but he needs to install ruby dependencies. Then I forwarded the. MD document to a PHP file by forwarding the Nginx URL, and the php file called the Kramdown interpreter to parse the corresponding document.
The principle is very simple, look at the specific implementation steps,
1. Install Kramdown Interpreter
The code is as follows |
Copy Code |
sudo aptitude install ruby RubyGems sudo gem install Kramdown |
After installation, (Ruby can directly yum install), you can write a MD test file, use the Kramdown command to try.
2, the production of kramdown analytical procedures
Here I use PHP, of course, can also use other languages, as long as you can invoke the Linux system commands, of course, PHP installation sometimes will block these sensitive functions, pay attention to release.
File hair.
The code is as follows |
Copy Code |
<?php $file = @$_get[' file ']; if (!is_file ($file)) Header ("http/1.0 404 Not Found"); Header ("content-type:text/html; Charset=utf-8 "); Date_default_timezone_set (' Asia/shanghai '); $filename = STRRCHR ($file, '/'); $filename = LTrim ($filename, '/'); $odir = RTrim ($file, $filename); ?> <title><?php echo $filename;? ></title> <link href= "/github-59da74dcbe2f1d555e306461652274f8741238a64e7b1fe8cc5a286232044835.css" media= "All" rel= " Stylesheet "type=" Text/css "> <link href= "/github2-22a0054564248c6dd87336e91bca068b1ab49e28ee1062519b3a0722d29da804.css" media= "All" rel= " Stylesheet "type=" Text/css "> <body> <div class= "header header-logged-in true" role= "banner" > <div class= "Container Clearfix" > <a class= "Header-logo-invertocat" href= "data-hotkey=" G D "aria-label=" homepage "ga-data-click=" header, go Dashboard, Icon:logo "> </a>
<ul class= "Header-nav left" role= "navigation" > <li class= "Header-nav-item Explore" > <a class= "Header-nav-link" href= "/" data-ga-click= "header, go to explore, Text:explore" > site home </a> </li> <li class= "Header-nav-item" > <a class= "Header-nav-link" href=/archives/category/lovephp/"data-ga-click=" header, go to gist, text:gist "> I love php</a>. </li> <li class= "Header-nav-item" > <a class= "Header-nav-link" href=/archives/category/tj/"data-ga-click=" header, go to gist, text:gist "> Recommended articles < /a> </li> <li class= "Header-nav-item" > <a class= "Header-nav-link" href=/xiangce/"target=" _blank "header, go to blog, data-ga-click=" > Albums < /a> </li> <li class= "Header-nav-item" > <a class= "Header-nav-link" href=/flink/"data-ga-click=" header, go to blog, text:blog "> Brother chain </a> </li> <li class= "Header-nav-item" > <a class= "Header-nav-link" href= "/message/" data-ga-click= "header, go to help, text:help" > About me </a> </li> </ul> </div> </div> <div class= "Container" > <div id= "Js-repo-pjax-container" class= "repository-content context-loader-container" "> <div class= "File-wrap" > <table class= "Files" data-pjax= "" > <tbody> <?php $dir = Opendir ($odir); while (($f = Readdir ($dir))!== false) { if ('. '!= $f && ' ... '!= $f) { $href = $f; $type = ' File '; if (Is_dir ($odir. $f)) { $href = $f. '/index.md '//directory default Access Index.md $type = ' Dir '; } $time = Date (' y-m-d h:i:s ', Filemtime ($odir. $f)); ?> <tr> <TD class= "icon" > </td> <TD class= "Content" > <span class= "css-truncate css-truncate-target ><a href=" <?php echo $href;? > "class=" Js-directory-link "title=" "><?php echo $f;? ></a></span> </td> <TD class= "Message" > <span class= "Css-truncate css-truncate-target" > <?php echo $type;? > </span> </td> <TD class= "Age" > <span class= "Css-truncate css-truncate-target" ><?php echo $time;? ></span> </td> </tr> <?php } } ?> </tbody> </table> </div> <div class= "File-box" > <div class= "File" > <div class= "meta Clearfix" > <div class= "Info file-name" > <span><?php echo $filename;? ></span> </div> </div> <div id= "Readme" class= "blob instapaper_body" > <article class= "Markdown-body entry-content" itemprop= "Maincontentofpage" > <?php echo System ('/usr/bin/kramdown '. $file); ?> </article> </div> </div> </div> </div> </div> </body> |
Implemented a number of simple file traversal and style, this can be made at will, I am using the GitHub style
3, nginx configuration URL rewrite
Of course, in accordance with their own preferences, of course, can also make soket services, but after all, PHP to do the service is not very good, and with shell monitoring, so it is better to use the URL rewrite
The code is as follows |
Copy Code |
Location ~ ^ (. +\.md) (. *) { Rewrite. */md.php?file= $document _root$fastcgi_script_name last; } |
4, in addition
Finally, add a default page to the Nginx Web service
Index index.html index.htm index.php index.md default.html default.htm;
This allows you to access the Index.md file by default.
This is the implementation of their own Web site markdown file resolution, you can write the MD document