Go combat--golang using Markdown (russross/blackfriday)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Life goes on and on go Go go!!!

First, a little bit of the episode, blog about the UV amount of Go:

Today, learning to share with you is how to use the markdown syntax in Golang, and of course using a third-party library Russross/blackfriday.
Reference: Http://blog.will3942.com/creating-blog-go

Markdown

Markdown is a markup language that can be written using an ordinary text editor, which can have a certain format for normal text content with simple markup syntax.
Markdown has a series of derivative versions that extend the functionality of markdown (such as tables, footnotes, embedded HTML, and so on) that the original markdown does not yet have, allowing Markdown to be converted into more formats, such as Latex,docbook. Markdown enhanced version of the more famous have markdown Extra, Multimarkdown, Maruku and so on. These derivative versions are either tool-based, such as Pandoc, or based on web sites, such as GitHub and Wikipedia, that are basically syntactically compatible, but with some changes in syntax and rendering effects

Here is a good markdown to push a useful editing software:
The best markdown Editor for haroopad–

Russross/blackfriday

Address: Https://github.com/russross/blackfriday
Also in the Golang, the most famous bar
Blackfriday:a Markdown Processor for Go
watch:72
star:2591
fork:328

BlackFriday is a Markdown processor implemented in Go. It is paranoid on it input (so can safely feeds it user-supplied data), it is fast, it supports common extensions ( Tables, smart punctuation substitutions, etc), and it is safe for all utf-8 (Unicode) input.

A simple server

package mainimport (  "fmt"  "net/http")func handlerequest(w http.ResponseWriter, r *http.Request) {  "Hi i am SuperWang %s", r.URL.Path[1:])}func main() {  http.HandleFunc("/", handlerequest)  http.ListenAndServe(":8000"nil)}

using template
For information on how to use Html/template, please refer to the blog:
Go Language Learning html/template Pack (the path to go)

Index.html:

 ' s blog!  
package mainimport (    "html/template"    "net/http")func handlerequest(w http.ResponseWriter, r *http.Request) {    "Hello Golang World!"    t := template.New("index.html")    t, _ = t.ParseFiles("index.html")    t.Execute(w, title)}func main() {    http.HandleFunc("/", handlerequest)    http.ListenAndServe(":8000"nil)}

Read Markdown file
Index.html modified to:

 <html><body><H1>superwang ' s blog! </H1>           {{range .}}<a href="/       {{. File}}  "><h2> {{. Title}} ({{. Date}})</H2></a><p>       {{. Summary}}</P>    {{end}} </body></html>   

MD file, named TEST.MD:

post!8/9/2017thethepost!# Markdown!*it's* **golang**!

Main.go:

Package Mainimport ("Html/template"    "Io/ioutil"    "Net/http"    "Path/filepath"    "Strings"    "Github.com/russross/blackfriday") type Post struct {TitlestringDatestringSummarystringBodystringFilestring}func HandleRequest (whttp. Responsewriter, R *http. Request) {Posts: = Getposts () T: = template. New ("Index.html") T, _ = T.parsefiles ("Index.html") T.execute (W, posts)}func getposts () []post {a: = []post{}Files, _: = FilePath. Glob ("posts/*") for_, F: = RangeFiles{file: = Strings. Replace (F,"posts/","", -1)file= Strings. Replace (file,". MD","", -1) Fileread, _: = Ioutil. ReadFile (f)Lines: = Strings. Split (string(Fileread),"\ n") Title: =string(Lines[0])Date:=string(Lines[1]) Summary: =string(Lines[2]) Body: = Strings. Join (Lines[3:Len(Lines)],"\ n") BODY =string(BlackFriday. Markdowncommon ([]byte(body)))a= Append (a, Post{title,Date, summary, Body,file})    }return a}func Main () {http. Handlefunc ("/", HandleRequest)http. Listenandserve (": 8000", nil)}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.