使用markdown語法撰寫csdn博客

在CSDN之下寫blog無疑是一件很是吃力的事情,對於很是多simple愛好者來說,能用markdown語法來書寫代碼是最優雅簡潔只是的了。本文主要介紹markdown語法和怎樣它來撰寫csdn下的blog。html


1 基本原理

撰寫csdn博客有兩種模式,源碼模式和可視化模式,當中可視化模式是咱們最常常使用的,源碼模式主要是html語法。一般模式下,咱們不會選用源碼模式,但是,咱們可以將源碼模式做爲中間輸出,來溝通markdown和可視化博客。簡單而言,咱們直接撰寫mardown文檔,經過python腳本轉化成html放到csdn的blog編輯器中,而後生成blog。因此,這裏面一個很是關鍵的地方在於撰寫將markdown轉化成html的python腳本。python


2. 功能python腳本

腳本的功能是將markdown語法轉變成html文件。代碼例如如下:git

#!/usr/bin/python
#coding utf8
import  markdown
import  sys
sys.defaultencoding=('utf8')
sys.defaultdecoding=('utf8')
filename=sys.argv[1]
markfile=open(filename)
markcontent=markfile.read();
print "markcontent type:", type(markcontent)#str ob
htmlcontent=markdown.markdown(markcontent.decode('utf8'))#
print "htmlcontent type:", type(htmlcontent)#unicode ob
htmlfilename=filename+".html"
htmlfile=open(htmlfilename, "w")
htmlcontent=htmlcontent.encode('gbk')#translate into str
htmlfile.write(htmlcontent)
htmlfile.close()
markfile.close()

功能比較簡單,不在此解釋,如下兩部分介紹markdown的詳細語法。github


3 區塊元素


3.1 段落與換行

換行需要插入一個明顯的空行來實現,也就是說,需要連續兩個換行符號。markdown

效果:編輯器

這是第一段字體

這是第二段ui

代碼:這是第一段(兩次換行)這是第二段google


3.2區塊引用

區塊引用是>來實現的,效果例如如下:code

This is a block

相應代碼:> This is a block

注意:區塊引用,並無屏蔽內部的markdown語法解析。

問題:假設一個區塊引用包括幾個段落那麼該怎樣處理呢?

答: 每個段落以前,都加上區塊引用符號,效果:

First para in a block

second para in the block.

>First para in a block

>second para in the block.


3.3標題

標題是經過#實現的,#個數的多少,表示標題的等級。

效果:

first head

second head

third head

實際代碼:

#first head
##second head
###third head

問題:區塊元素會本身主動加入換行符嗎?

答:標題元素確實會加入換行符號, 但是其它區塊元素不會加入換行符號。


3.4列表

無序列表使用*、+、-來做爲列表標記,標記後面最少跟着一個空格,效果:

  • red
  • green
  • blue

代碼例如如下:

* red
* green
* blue

注意:列表和標題同樣,會本身主動換行

答:經過縮進來引用代碼,代碼內部的markdown解析將被屏蔽。

列表項包括多個段落的時候,咱們將在第二個段落以後,每個段落開始,加上四個以上的空格或者製表符,這樣就可以實現如下的效果:

  1. first of a table

    second of a table

另外,列表中並無屏蔽markdown語法,可以在列表中放入區塊,也可以放入引用代碼塊。在列表中放入縮進的狀況例如如下:

這裏是縮進以後


3.5 code block

咱們來看看代碼塊的語法:

int main()
{
    int a;
}


3.6 切割線

切割線的語法是三個以上的","、"-"、"_",效果例如如下



4. 區段元素


4.1 連接

markdown 支持兩種連接模式,行內式和參考式:都用「 []」來引用連接文字。

行內式的連接語法與效果例如如下:

[google](http://www.google.com"the character you will see when mouse on")

google


4.2 強調

使用一對「 *」或者一對「 -」來標記需要強調的內容;使用一對兩個「*」或者「-」來對字體進行加粗。效果例如如下:outstanding,and,strong。值得注意的是,符號兩邊不能含有空格,不然,這些符號將被看成符號自己進行解析;另外,咱們相同可以使用反斜線來轉義它們.outstanding, andstrong outstanding, and strong

不少其它語法介紹可以參考這裏:markdown 語法介紹


4.3 小塊代碼

使用單個反引號來表示小塊代碼引用,效果如printf

相關文章
相關標籤/搜索