Simplest MarkDowm parser for publishing ASP.NET documentation and samples (snapshot 2017)
1: Module Module1
2: Const StartDir As String = "H:\Job57\AspNet-DocAndSamples-2017"
3: Dim UnicodeLeftDoubleQuotationMark As Char() = Text.Encoding.UTF8.GetChars({&HE2, &H80, &H9C})
4: Dim UnicodeRightDoubleQuotationMark As Char() = Text.Encoding.UTF8.GetChars({&HE2, &H80, &H9D})
5:
6: Sub Main()
7: Dim db1 As New tst1DataContext
8: Dim Rows = (From X In db1.ProjectTemplates2s Select X).ToList
9: For Each One In Rows
10: Dim Path1 As String = ""
11: For i = 0 To One.level - 1
12: Path1 &= "../"
13: Next
14: Dim TXT2 As String = My.Computer.FileSystem.ReadAllText(StartDir & One.url.Replace("/", "\"))
15: Dim RDR1 As New IO.StringReader(TXT2)
16: Dim Res1 As New Text.StringBuilder
17: While True
18: Dim Str1 As String = RDR1.ReadLine
19: If Str1 Is Nothing Then Exit While
20: Dim TXT1 As String = ""
21: If Str1.Contains("[!code") Then
22: Res1.AppendLine(CheckAndInsertCodeToOneLine(Str1, One.url))
23: ElseIf Str1.ToLower.Contains("[!div class=" & UnicodeLeftDoubleQuotationMark & "step-by-step" & UnicodeRightDoubleQuotationMark & "]") Then
24: Str1 = Str1.Replace("[!div class=" & UnicodeLeftDoubleQuotationMark & "step-by-step" & UnicodeRightDoubleQuotationMark & "]", "")
25: Res1.AppendLine(Str1)
26: Else
27: Res1.AppendLine(Str1)
28: End If
29: End While
30: My.Computer.FileSystem.WriteAllText("H:\Job57\AspNet-DocAndSamples-2017" & One.url.Replace("/", "\") & "l", Res1.ToString, False, System.Text.Encoding.UTF8)
31: Console.WriteLine(One.url)
32: Next
33: Console.ReadLine()
34: End Sub
35:
36: Function CheckAndInsertCodeToOneLine(Str1 As String, BasicUrl As String) As String
40: Dim Str2 As String = ""
41: Dim pos1 As Integer = Str1.IndexOf("[!code")
42: If pos1 >= 0 Then
43: Main: Dim Pos2 As Integer = Str1.ToLower.IndexOf(">main</a>]", pos1)
44: If Pos2 > pos1 Then
45: Dim Pos3 As Integer = Str1.IndexOf("<a href=""", pos1)
46: If Pos3 > pos1 And (Pos3 - pos1 - 7) >= 0 Then
47: Dim Type As String = Mid(Str1, pos1 + 8, Pos3 - pos1 - 7)
48: Dim Url As String = Mid(Str1, Pos3 + 10, Pos2 - Pos3 - 10)
49: Dim P1 As String = (StartDir & BasicUrl.Replace("/", "\")).Replace(".htm", "")
50: Dim P2 As String = P1 & "\.."
51: Dim RealUrl As String = IO.Path.Combine(P2, Url.Replace("/", "\"))
52: Dim Quest1 As Integer = RealUrl.IndexOf("?")
53: If Quest1 > 0 Then
54: RealUrl = Strings.Left(RealUrl, Quest1)
55: End If
56: Dim Quest2 As Integer = RealUrl.IndexOf("#")
57: If Quest2 > 0 Then
58: RealUrl = Strings.Left(RealUrl, Quest2)
59: End If
60: If My.Computer.FileSystem.FileExists(RealUrl) Then
61: Dim InsertingCode As String = My.Computer.FileSystem.ReadAllText(RealUrl)
62: Dim CS As New Manoli.Utils.CSharpFormat.CSharpFormat
63: CS.Alternate = False
64: CS.EmbedStyleSheet = False
65: CS.LineNumbers = True
66: Dim SQL As New Manoli.Utils.CSharpFormat.TsqlFormat
67: SQL.Alternate = False
68: SQL.EmbedStyleSheet = False
69: SQL.LineNumbers = True
70: Dim VB As New Manoli.Utils.CSharpFormat.VisualBasicFormat
71: VB.Alternate = False
72: VB.EmbedStyleSheet = False
73: VB.LineNumbers = True
74: Dim MS As New Manoli.Utils.CSharpFormat.MshFormat
75: MS.Alternate = False
76: MS.EmbedStyleSheet = False
77: MS.LineNumbers = True
78: Dim JS As New Manoli.Utils.CSharpFormat.JavaScriptFormat
79: JS.Alternate = False
80: JS.EmbedStyleSheet = False
81: JS.LineNumbers = True
82: Dim Html As New Manoli.Utils.CSharpFormat.HtmlFormat
83: Html.Alternate = False
84: Html.EmbedStyleSheet = False
85: Html.LineNumbers = True
86: Select Case Type.ToLower
87: Case "csharp"
88: Str2 = Stuff(Str1, Pos2 + 10, CS.FormatCode(InsertingCode))
89: Case "html"
90: Str2 = Stuff(Str1, Pos2 + 10, Html.FormatCode(InsertingCode))
91: Case "aspx"
92: Str2 = Stuff(Str1, Pos2 + 10, Html.FormatCode(InsertingCode))
93: Case "sql"
94: Str2 = Stuff(Str1, Pos2 + 10, SQL.FormatCode(InsertingCode))
95: Case "cshtml"
96: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode).Replace("@", "<span class=""asp"">@"))
97: Case "vb"
98: Str2 = Stuff(Str1, Pos2 + 10, VB.FormatCode(InsertingCode))
99: Case "vbhtml"
100: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode).Replace("@", "<span class=""asp"">@"))
101: Case "javascript"
102: Str2 = Stuff(Str1, Pos2 + 10, JS.FormatCode(InsertingCode))
103: Case "powershell"
104: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode))
105: Case "console"
106: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode))
107: Case "json"
108: Str2 = Stuff(Str1, Pos2 + 10, JS.FormatCode(InsertingCode))
109: Case "xml"
110: Str2 = Stuff(Str1, Pos2 + 10, Html.FormatCode(InsertingCode))
111: Case "css"
112: Str2 = Stuff(Str1, Pos2 + 10, Html.FormatCode(InsertingCode))
113: Case "text"
114: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode))
115: Case "typescript"
116: Str2 = Stuff(Str1, Pos2 + 10, JS.FormatCode(InsertingCode))
117: Case "nginx"
118: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode))
119: Case "none"
120: Str2 = Stuff(Str1, Pos2 + 10, CS.FormatCode(InsertingCode))
121: Case Else
122: Str2 = Stuff(Str1, Pos2 + 10, MS.FormatCode(InsertingCode))
123: End Select
124: Else
125: Str2 = Str1
126: End If
127: Else
128: Str2 = Str1
129: End If
130: ElseIf Str1.Contains("></a>]") Then
131: Dim Pos6 As Integer = Str1.IndexOf("></a>]")
132: Str1 = Stuff(Str1, Pos6 + 1, "Main")
133: GoTo Main
134: Else
135: Str2 = Str1
136: End If
137: Else
138: Str2 = Str1
139: End If
140: Return Str2
141: End Function
142:
143: Function Stuff(str As String, start As Integer, replacement As String) As String
144: Return str.Substring(0, start) & replacement & str.Substring(start)
145: End Function
146:
147: End Module
Comments (
)
Link to this page:
//www.vb-net.com/AspNet-DocAndSamples-2017/MarkDowmCodeParse.htm
|