Selaa lähdekoodia

Updated code highlighter to include info.

Added dot example.
Brucey 8 kuukautta sitten
vanhempi
commit
b0f7715114
3 muutettua tiedostoa jossa 99 lisäystä ja 12 poistoa
  1. 57 10
      markdown.mod/examples/example_03.bmx
  2. 28 0
      markdown.mod/glue.c
  3. 14 2
      markdown.mod/markdown.bmx

+ 57 - 10
markdown.mod/examples/example_03.bmx

@@ -3,6 +3,7 @@ SuperStrict
 Framework brl.standardio
 Import text.markdown
 Import text.pikchr
+Import text.graphviz
 
 Local sb:TStringBuilder = New TStringBuilder
 Local highlighter:THighlighter = New THighlighter
@@ -13,38 +14,84 @@ sb.Append("<body>")
 TMarkdown.ParseToHtml("""
 Hello !
 ```pikchr
-arrow right 200% "Markdown" "Source"
-box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
-arrow right 200% "HTML+SVG" "Output"
-arrow <-> down 70% from last box.s
-box same "Pikchr" "Formatter" "(pikchr.c)" fit
+A: ellipse thick
+line thin color gray left 70% from 2mm left of (A.w,A.n)
+line same from 2mm left of (A.w,A.s)
+text "height" at (7/8<previous.start,previous.end>,1/2<1st line,2ndline>)
+line thin color gray from previous text.n up until even with 1st line ->
+line thin color gray from previous text.s down until even with 2nd line ->
+X1: line thin color gray down 50% from 2mm below (A.w,A.s)
+X2: line thin color gray down 50% from 2mm below (A.e,A.s)
+text "width" at (1/2<X1,X2>,6/8<X1.start,X1.end>)
+line thin color gray from previous text.w left until even with X1 ->
+line thin color gray from previous text.e right until even with X2 ->
 ```
 hmm...
 ```csharp
 Console.WriteLine("Hello World!");
 ```
 World!
+```dot
+digraph U {
+  eee -> {
+  a [label="abc &aelig; z"]
+  2[label="ü"];
+  3[label="ä"];
+  4[label="ö"];
+  d [label=" well &#9679; ok"]
+
+  F [label="f: &#958;"]
+
+  B  [label= "Fichier non trouvé"]
+  p1 [label="p1: &#9816;"]
+  p2 [label="p2: &#x2658;"]
+  }
+}
+```
 """, sb,,,,highlighter)
 
 sb.Append("</body></html>")
 
-SaveString(sb.ToString(), "markdown.html")
+Local s:String = sb.ToString()
+Local buf:Byte[] = New Byte[s.length * 3]
+Local length:size_t = buf.length
+s.ToUTF8StringBuffer(buf, length)
+SaveByteArray( buf, "markdown.html" )
 
 
 Type THighlighter Extends TMDHtmlCodeHighlighter
 
-	Method Text:Int(lang:String, txt:String, output:TStringBuilder)
+	Method Text:Int(lang:String, info:String[], txt:String, output:TStringBuilder)
 
-		If lang = "pikchr" Then
+		Select lang
+		Case "pikchr"
 			Local width:Int, height:Int
 			Local out:String = Pikchr(txt, Null, EPikChrFlags.NONE, width, height)
-			
+
 			output.Append("<div style=~qmax-width:").Append(width).Append("px~q>")
 			output.Append(out)
 			output.Append("</div>")
 
 			Return True
-		End If
+		Case "dot"
+
+			Local g:TGVGraphviz = New TGVGraphviz
+			Local graph:TAGraph = TAGraph.FromString(txt)
+
+			Local engine:String = "dot"
+			Local parts:String[] = info[0].Split(" ")
+			If parts.Length > 1 Then
+				engine = parts[1]
+			End If
+
+			graph.Unflatten(4)
+
+			Local res:Int = g.Layout(graph, engine)
+			Local svg:String = g.ToSvg(graph)
+
+			output.Append(svg)
+			Return True
+		End Select
 
 		Return False
 	End Method

+ 28 - 0
markdown.mod/glue.c

@@ -216,3 +216,31 @@ MD_ATTRIBUTE bmx_md_spanimg_title(MD_SPAN_IMG_DETAIL * detail) {
 MD_ATTRIBUTE bmx_md_spanwikilink_target(MD_SPAN_WIKILINK_DETAIL * detail) {
     return detail->target;
 }
+
+BBArray * bmx_md_attribute_substostringarray(MD_ATTRIBUTE * attr) {
+
+    if (attr->size == 0) {
+        return &bbEmptyArray;
+    }
+
+    int n = 0;
+    while (attr->substr_offsets[n+1] != attr->size) {
+        n++;
+    }
+    n++;
+
+    BBArray *p = bbArrayNew1D("$", n);
+    BBString **s = (BBString**)BBARRAYDATA(p, p->dims);
+
+    for (int i = 0; i < n; i++) {
+        int start_offset = attr->substr_offsets[i];
+        int end_offset = attr->substr_offsets[i+1];
+        int substr_length = end_offset - start_offset;
+
+        const MD_CHAR *substr_start = attr->text + start_offset;
+
+        s[i] = bbStringFromUTF8Bytes((const unsigned char*)substr_start, substr_length);
+    }
+
+    return p;
+}

+ 14 - 2
markdown.mod/markdown.bmx

@@ -59,6 +59,7 @@ Type TMDHtmlCodeHighlighter Abstract
 	Field _codeblock:TStringBuilder
 	Field _output:TStringBuilder
 	Field _lang:String
+	Field _info:String[]
 
 	Method _EnterCodeBlock:Int(block:TMDBlockCode)
 		_codeblock = New TStringBuilder
@@ -66,11 +67,17 @@ Type TMDHtmlCodeHighlighter Abstract
 		If lang.size > 0 Then
 			_lang = String.FromUTF8Bytes(lang.text, lang.size)
 		End If
+		
+		local info:SMDAttribute = block.Info()
+		If info.size > 0 Then
+			_info = info.SubsToStringArray()
+		End If
+
 		Return 0
 	End Method
 
 	Method _LeaveCodeBlock:Int(block:TMDBlockCode)
-		Local processed:Int = Text(_lang, _codeblock.ToString(), _output)
+		Local processed:Int = Text(_lang, _info, _codeblock.ToString(), _output)
 		If Not processed Then
 			_output.Append("<pre><code")
 			If _lang Then
@@ -93,7 +100,7 @@ Type TMDHtmlCodeHighlighter Abstract
 	returns: #True if the code was processed, #False if the default code block rendering should be used.
 	about: If the code is processed, the output should be appended to @output.
 	End Rem
-	Method Text:Int(lang:String, text:String, output:TStringBuilder) Abstract
+	Method Text:Int(lang:String, info:String[], text:String, output:TStringBuilder) Abstract
 
 End Type
 
@@ -768,6 +775,10 @@ Struct SMDAttribute
 	Field size:UInt
 	Field substrTypes:EMDTextType Ptr
 	Field substrOffsets:UInt Ptr
+
+	Method SubsToStringArray:String[]()
+		Return bmx_md_attribute_substostringarray(Self)
+	End Method
 End Struct
 
 Private
@@ -809,4 +820,5 @@ Extern
 
 	Function bmx_md_spanwikilink_target:SMDAttribute(detail:Byte Ptr)
 
+	Function bmx_md_attribute_substostringarray:String[](attr:SMDAttribute Var)
 End Extern