Thursday, March 12, 2009

How to combine ReST and Pygments

Many Python people use ReST for documentation their sourcecode, programs, scripts et cetera.But as the description at Using Pygments in ReST documents you need copy external/rst-directive.py to your code. It's not enough detail.

let me give you an example:
  1. Get it from the Cheese Shop.
  2. unzip the file and then open the external/rst-directive.py with your favorite text editor.
  3. edit the file under the //Lib/site-packages/docutils/parsers/rst/__init__.py
  4. add following code to the bottom of the file and save it.
  5. now you can generate the rest documnet with source code highlight.
Here is an example that I used:



# Set to True if you want inline CSS styles instead of classes
INLINESTYLES = True

from pygments.formatters import HtmlFormatter

# The default formatter
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)

# Add name -> formatter pairs for every variant you want to use
VARIANTS = {
'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),

}


from docutils import nodes
from docutils.parsers.rst import directives

from pygments import highlight
from pygments.lexers import get_lexer_by_name, TextLexer

def pygments_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
try:
lexer = get_lexer_by_name(arguments[0])
except ValueError:
# no lexer found - use the text one instead of an exception
lexer = TextLexer()
# take an arbitrary option if more than one is given
formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
parsed = highlight(u'\n'.join(content), lexer, formatter)
return [nodes.raw('', parsed, format='html')]

pygments_directive.arguments = (1, 0, 1)
pygments_directive.content = 1
pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])

directives.register_directive('sourcecode', pygments_directive)

42 comments:

未來 said...

到處盡心,即為快事;舉步踏實,便是坦途。 ..................................................

跑車 said...

成人全裸打炮打手槍打飛機巨乳巨奶女優大奶性交性愛淫蕩淫慾淫亂淫婦淫妹淫叫淫水淫女情慾情色做愛限制級波霸口交18禁貼圖寫真視訊援交露點爆乳潮吹裸體

dee said...

wonderful...................................................

家瑩 said...

Since it is the early worm that gets eaten by the bird, sleep late.............................................

倫音 said...

辛苦了!祝你愈來愈好!........................................

惠蘋 said...

你的部落格很棒,我期待更新喔........................................

鳳珠鳳珠 said...

很用心的blog,推推哦 ........................................

PhilCrispin0626清民 said...

0401影音成人影片交流愛撫淫叫色情照片性愛圖片女生如何自慰激突成人論壇美女脫胸罩走光一夜正妹視訊情人性愛論壇85cc影城性伴侶85cc色情影片打手槍情色視訊聊天室色情a片色情色情圖片限制級照片色情電話520sex免費色情網情色性愛一夜情聊天網成人聊天網視訊成人台灣性網av一夜女情色av淫女免費視訊美女挑逗情色影音聊天作愛影片辣妹清涼秀美女做愛成人自拍貼圖成人資訊色論壇台灣色情成人網站色情視訊聊天露三點打砲情色性愛貼圖av成人網成人影片

勝傑懿綺 said...

上班很累,摸個魚輕輕鬆鬆的逛部落 ........................................

怡如 said...

Very good stories~~ Thanks for ur sharing~~!! ........................................

真坤 said...

thank for share, it is very important . ̄︿ ̄

淑芳 said...

憂能傷身,保重哦!

M212aeganT_Moe1 said...

A friend to everybody is a friend to nobody..............................................

Tro0223y_Millan said...

45avdvd 本土av自拍天堂 免費a長片 性感照片xxx383 av1688情人天使娛樂網,sex 台中援交34c甜心寶貝直播貼片 hhh重鹹系列免費 寶貝視訊台中援交友留言 桃園視訊聊天室 小魔女a自拍 上班族視訊 高雄援留言視訊交友 彩虹性愛巴士 夢中情人情趣用品 mmshow tw網愛 苗栗人聊天室 寶貝視訊ok論壇 0941電愛專線 520sex-情色視訊 免費影片無碼卡通 85cc情色視訊交友 免費視訊美女影音觀賞 色瞇瞇影片網 jp成人 免費a影片線上直播 h 視訊自拍美女聊天室 性感沙灘3,性感照片 視訊交友90739,限制級,777美女dvd 洪爺sex免費看a片論壇 微風成人區情色文學 微風論壇短片區 免費成人動畫 玩美辣妹有約 1111av女優天堂 嘟嘟成人網 視訊聊天1799 成人圖片區 台灣kiss,微風成人 show live 視訊聊天 g8成人下載 yam視訊交友 a 免費影片線上直播 情色視訊交友辣妹no3 日本a片援交妹 拓網天空交友高雄視訊 6k情人網辣妹視訊 正妹視訊elove交友 免費視訊formosa car 視訊聊天qk176 成人文學

文佩齊華 said...

朋友是一面鏡子 ............................................................

宛美黃惠妹 said...

與人相處多微笑,個人獨處多沈思。 ............................................................

陳卓人 said...

生活總是起起伏伏,心情要保持快樂才好哦!! .........................................

雅馨 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。..................................................................

嘉剛 said...

人生是故事的創造與遺忘。..................................................................

峻龍 said...

讓人流連忘返,真期待新文章發表!.................................................................

王瑞 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」....................................................................

貢慧 said...

幸福不是一切,人還有責任。....................................................................

俊坤戴 said...

死亡是悲哀的,但活得不快樂更悲哀。......................................................................

淑娟淑娟 said...

成熟,就是有能力適應生活中的模糊。.................................................................

星美星美 said...

君子立恆志,小人恆立志。.................................................................

婷珊 said...

很用心的blog,推推哦 ..................................................................

懿綺懿綺 said...

閱讀您的BLOG文章,真是件快樂的事!..................................................................

蕙春蕙春 said...

寫文章需要心情~~期待你再一次的好文章............................................................

趙坤然趙坤然 said...

faith will move mountains. ..................................................

曾法幸 said...

Riches serve a wise man but command a fool.............................................................

青卉 said...

我只知道,假如我去愛人生,那人生一定也會回愛我................................................

許惠吟許惠吟 said...

單純喜歡你的部落格 留個言跟您問聲好~~...............................................................

林聿希林聿希林聿希 said...

加油!期待更新哦!..................................................................

承王蓁 said...

Lets cross the bridge when we come to it............................................................

家唐銘 said...

你不能改變容貌~~但你可以展現笑容.................................................................

建邱勳 said...

你要保守你的心,勝過保守一切,因為一生的果效是由心發出................................................

家唐銘 said...

成功的秘訣在於:當機會來臨時,你已準備妥當............................................................

梁名劉明倫裕 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

惠邱邱邱邱雯 said...

時間就是塑造生命的材料。

王辛江淑萍康 said...

人生匆匆-把握當下,支持鼓勵~事事如意~..................................................

said...

唯有穿鞋的人,才知道鞋的哪一處擠腳......................................................................

冠州 said...

you got it!i like it!!!