日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

怎样在 Markdown 中使程序代码带上行号

發(fā)布時(shí)間:2025/3/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 怎样在 Markdown 中使程序代码带上行号 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在圖靈社區(qū)使用 Markdown 寫文章時(shí),如果在一段文字的每行開頭加上四個(gè)空格,或者一個(gè)制表符(Tab),這段文字就會被視為程序代碼。這樣,就會自動(dòng)識別所用的編程語言,進(jìn)行代碼染色,語法高亮顯示。但是,如果這段程序很長的話,就有兩個(gè)小問題:

  • 每行的開頭要加上空格或制表符,很麻煩。
  • 如果要顯示行號的話,就更麻煩了。
  • 因此,我用 C# 語言寫了小程序,建設(shè)一個(gè) ASP.NET 4 網(wǎng)站來解決上述兩個(gè)麻煩:

    [+]查看原圖

    在這個(gè)網(wǎng)頁中:

    • Line Count 復(fù)選框表示是否需要加上行號。
    • Prefix 中的的 SpaceTab 無線按鈕讓你選擇每行開頭是增加空格還是制表符。
    • Prefix Count 文本框讓你輸入縮進(jìn)的層次。默認(rèn)是縮進(jìn)一層 。但是如果遇到在有序列表或無序列表中的程序代碼,就需要縮進(jìn)兩層,甚至更多層了。

    這個(gè)網(wǎng)站的總體結(jié)構(gòu)如下所示:

    網(wǎng)站的配置文件 Web.config 如下所示:

    <?xml version="1.0" encoding="utf-8"?> <configuration><system.web><httpRuntime requestValidationMode="2.0" /><globalization requestEncoding="utf-8" responseEncoding="utf-8" /></system.web> </configuration>

    網(wǎng)站的 Web 頁面文件 CodeFormat.aspx 如下所示:

    <%@ Page validateRequest="false" Language="C#" inherits="Skyiv.Ben.Web.CodeFormatPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Code Format</title> </head> <body><form id="form1" runat="server"><asp:Button Onclick="Submit" Text="Submit" Runat="Server" /><span style="background-color:LightBlue;"><asp:CheckBox Id="chkLineCount" Text="Line Count" Checked="True" Runat="Server" />&nbsp;</span>&nbsp;<span style="background-color:LightBlue;">&nbsp;Prefix:<asp:RadioButton Id="rbnSpace" Text="Space" Checked="True"GroupName="Prefix" Runat="Server" /><asp:RadioButton Id="rbnTab" Text="Tab"GroupName="Prefix" Runat="Server" />&nbsp;</span>&nbsp;<span style="background-color:LightBlue;">&nbsp;Prefix Count:<asp:TextBox Runat="Server" Id="tbxLevel" Text="1" Columns="2" MaxLength="1" />&nbsp;</span><hr /><div><asp:TextBox Runat="Server" Id="tbxInput" Wrap="False"TextMode="MultiLine" Columns="80" Rows="10" /><br /><asp:TextBox Runat="Server" Id="tbxOutput" ReadOnly="True" Wrap="False"TextMode="MultiLine" BackColor="LightBlue" Columns="80" Rows="10" /></div></form> </body> </html>

    以及對應(yīng)的后臺 C# 代碼 CodeFormat.aspx.cs:

    1: using System;2: using System.IO;3: using System.Web;4: using System.Web.UI;5: using System.Web.UI.WebControls;6: using Skyiv.Utils;7: 8: namespace Skyiv.Ben.Web9: { 10: public class CodeFormatPage : Page 11: { 12: protected TextBox tbxInput; 13: protected TextBox tbxOutput; 14: protected TextBox tbxLevel; 15: protected CheckBox chkLineCount; 16: protected RadioButton rbnTab; 17: 18: protected void Page_Load(object sender, EventArgs e) 19: { 20: tbxOutput.Text = string.Format(" OS: {1} ({2}-bit){0}CLR: {3}", 21: Environment.NewLine, Environment.OSVersion, 22: Environment.Is64BitOperatingSystem ? 64 : 32, 23: Environment.Version); 24: } 25: 26: protected void Submit(object sender, EventArgs e) 27: { 28: var writer = new StringWriter(); 29: new CodeFormat(new StringReader(tbxInput.Text), 30: writer).Run(chkLineCount.Checked, rbnTab.Checked, GetLevel(tbxLevel.Text)); 31: tbxOutput.Text = writer.ToString(); 32: } 33: 34: int GetLevel(string str) 35: { 36: int n; 37: if (!int.TryParse(str, out n)) n = 1; 38: return Math.Min(5, Math.Max(0, n)); 39: } 40: } 41: }

    上述程序中:

    • 第 34 至 39 行的 GetLevel 方法讀取 Prefix Count 文本框中的縮進(jìn)層次,返回結(jié)果限制在 0 到 5 之間。
    • 第 26 至 32 行的 Submit 方法在 Web 頁面中的 Submit 按鈕被點(diǎn)擊時(shí)被調(diào)用。
    • 第 29 至 30 行調(diào)用 CodeFormat 類的 Run 方法對程序代碼進(jìn)行格式化(加行號、行首空格等)。

    下面就是 CodeFormat 類的源程序代碼 CodeFormat.cs:

    1: using System;2: using System.IO;3: using System.Collections.Generic;4: 5: namespace Skyiv.Utils6: {7: sealed class CodeFormat8: {9: TextReader reader; 10: TextWriter writer; 11: 12: public CodeFormat(TextReader reader, TextWriter writer) 13: { 14: this.reader = reader; 15: this.writer = writer; 16: } 17: 18: public void Run(bool hasCount, bool isTab, int level) 19: { 20: Write(Read(), hasCount, isTab, level); 21: } 22: 23: List<string> Read() 24: { 25: var lines = new List<string>(); 26: for (string s; (s = reader.ReadLine()) != null; ) lines.Add(s); 27: return lines; 28: } 29: 30: void Write(List<string> lines, bool hasCount, bool isTab, int level) 31: { 32: var prefix = "".PadLeft((isTab ? 1 : 4) * level, isTab ? '\t' : ' '); 33: var format = "{0}" + (hasCount ? "{1," + 34: lines.Count.ToString().Length + "}: " : "") + "{2}"; 35: var count = 0; 36: foreach (var line in lines) 37: writer.WriteLine(format, prefix, ++count, line); 38: } 39: } 40: }

    上述程序中:

    • 第 9 至 10 行的 TextReader 和 TextWriter 分別用于讀取數(shù)據(jù)和輸出格式化后的結(jié)果,這兩個(gè)類是抽象基類。
    • 在這個(gè)網(wǎng)站中,是使用 StringReader 和 StringWriter 派生類,對應(yīng)于 Web 頁面的 tbxInputtbxOutput 文本框。
    • 如果使用 StreamReader 和 StreamWriter 派生類,就可以從輸入流讀取數(shù)據(jù),寫到輸出流中。
    • 如果使用 Console.In 和 Console.Out,就可以從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù),寫到標(biāo)準(zhǔn)輸出。
    • 第 23 至 28 行的 Read 方法讀取數(shù)據(jù)到內(nèi)存的 List<string> 數(shù)據(jù)結(jié)構(gòu)中。
    • 第 30 至 38 行的 Writer 方法將內(nèi)存中的數(shù)據(jù)格式化后寫出去。
    • 第 32 行根據(jù) isTab 和 level 參數(shù)決定程序代碼數(shù)據(jù)每行的前綴。
    • 第 33 至 34 行根據(jù) hasCount 參數(shù)決定行號的內(nèi)容。
    • 第 34 行的 lines.Count.ToString().Length 是行號所占的寬度。
    • 第 36 至 37 行的循環(huán)逐行格式化數(shù)據(jù)。

    最后是 Makefile:

    CSC = dmcs DLL1 = -r:System.Web.dll../bin/CodeFormat.dll: CodeFormat.aspx.cs CodeFormat.cs$(CSC) -out:$@ -t:library $(DLL1) CodeFormat.aspx.cs CodeFormat.cs

    有了上面的源程序后,執(zhí)行 make 命令編譯整個(gè)網(wǎng)站:

    src$ make
    dmcs -out:../bin/CodeFormat.dll -t:library -r:System.Web.dll CodeFormat.aspx.cs CodeFormat.cs

    這就大功告成了。


    from: http://www.ituring.com.cn/article/35350

    總結(jié)

    以上是生活随笔為你收集整理的怎样在 Markdown 中使程序代码带上行号的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。