[EXAMPLES]

* NEW: Embed the gb.markdown code so that the example can be tested on the
  stable version of Gambas. This is temporary!


git-svn-id: svn://localhost/gambas/trunk@6128 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
Benoît Minisini 2014-02-03 18:32:43 +00:00
parent 1d2147f99c
commit cd9babdeeb
9 changed files with 830 additions and 15 deletions

View file

@ -4,8 +4,8 @@ Title=SmallWiki
Startup=Main
UseHttpServer=1
Icon=.public/logo.png
Version=0.0.126
Component=gb.markdown
Version=3.5.90
VersionFile=1
Component=gb.web
TabSize=2
ExecPath=/home/guygle/guygle/cgi-bin/wiki.gambas

View file

@ -1,3 +1,7 @@
HTML, BODY {
height: 100%;
}
BODY{
padding: 0px;
margin: 0px;
@ -21,13 +25,20 @@ CODE {
}
PRE {
background: #F0F0F0;
display: inline-table;
padding: 4px;
margin: 0px;
}
/*PRE {
border: solid 1px #E0E0E0;
padding: 4px;
}
PRE CODE {
background: none;
}
}*/
TABLE {
border: solid #A0A0A0 1px;
@ -72,6 +83,7 @@ TABLE.example TH {
border-bottom: black solid 1px;
padding: 4px 8px;
box-shadow: 0 0 8px black;
height: 24px;
}
.title {
@ -93,7 +105,9 @@ TABLE.example TH {
.unknown {
color: red;
font-style: italic;
font-style: italic;
margin-top: 8px;
font-size: 150%;
}
.command {
@ -107,9 +121,18 @@ TABLE.example TH {
padding: 2px;
}
.edit-frame {
position: absolute;
left: 0px;
right: 0px;
top: 36px;
bottom: 0px;
padding: 8px;
}
.edit {
border: none;
width: 100%;
height: 600px;
background: none;
width: 100%;
height: 100%;
}

View file

@ -0,0 +1,21 @@
' Gambas class file
'Export
Create
Public Root As String
Public Line As Integer
Public Sub ToHTML(Markdown As String) As String
Return Markup.Convert(Markdown, Me)
End
Public Sub Link((sLink) As MarkdownLink)
End
Public Sub Command((sCommand) As String) As String[]
End

View file

@ -0,0 +1,8 @@
' Gambas class file
'Export
Public Link As String
Public Text As String
Public Title As String
Public Html As String

View file

@ -0,0 +1,762 @@
' Gambas module file
Public Struct MarkupList
sType As String
iIndent As Integer
End Struct
Private $aMarkup As String[]
Private $cLink As Collection
Private $aIndex As String[]
Private $aTable As String[]
Private $aTablePos As Integer[]
Private $hMarkdown As Markdown
Private $hLink As New MarkdownLink
Private Sub GetIndent(sLine As String) As Integer
Return Len(sLine) - Len(LTrim(sLine))
End
Public Sub Convert(sMarkup As String, hMarkdown As Markdown) As String
$hMarkdown = hMarkdown
$cLink = New Collection
$aIndex = New String[]
Return ConvertMarkup(Split(sMarkup, "\n"))
End
Private Sub ConvertMarkup(aLine As String[], Optional bDoNotSetLine As Boolean) As String
Dim iLine As Integer
Dim aResult As String[]
Dim sLine As String
Dim sText As String
Dim I As Integer
Dim bCode As Boolean
Dim iBlockQuote As Integer
Dim sCar As String
Dim bInsidePar As Boolean
Dim bIgnorePar As Boolean
Dim bAddPar As Boolean
Dim iIndent, iCurrentIndent As Integer
Dim hMarkupList As MarkupList
Dim bJustList As Boolean
Dim sLink As String
Dim iPos As Integer
Dim aList As New MarkupList[]
Dim sTable As String
Dim iIndexPos As Integer = -1
Dim aCommand As String[]
aResult = New String[]
$aMarkup = New String[]
$aTable = New String[]
$aTablePos = New Integer[]
' iLine = 0
' Do
' If iLine >= aLine.Count Then Break
' sLine = LTrim(aLine[iLine])
' If Left(sLine) = "%" Then
' If sLine Begins "%IF " Then
' Else If sLine Begins "%ELSE " Then
'
' Endif
' Endif
' Inc iLine
' Loop
For iLine = 0 To aLine.Max
sLine = LTrim(aLine[iLine])
If Not sLine Then Continue
If Left(sLine) <> "[" Then Continue
I = InStr(sLine, "]:")
If I = 0 Then Continue
If I >= 3 Then
sLink = Trim(Mid$(sLine, I + 2))
$cLink[Mid$(sLine, 2, I - 2)] = sLink
If Left(sLink) = "#" Then
iPos = InStr(sLink, " ")
If iPos Then sLink = Left(sLink, iPos - 1)
aLine[iLine] = "<a name=\"" & Html$(sLink) & "\"></a>"
Continue
Endif
Endif
aLine[iLine] = ""
Next
For iLine = 0 To aLine.Max
If Not bDoNotSetLine Then $hMarkdown.Line = iLine
sLine = aLine[iLine]
If $aMarkup.Count Then
aResult.Add(ConvertLine(sLine))
Continue
Endif
sLine = RTrim(sLine)
If Not sLine Then sLine = aLine[iLine]
' Special command
If Left(sLine, 2) = "@{" And If Right(sLine) = "}" Then
If sLine = "@{index}" Then
iIndexPos = aResult.Count
aResult.Add(sLine)
Else
aCommand = $hMarkdown.Command(Mid$(sLine, 3, -1))
If aCommand And If aCommand.Count Then
aResult.Add(ConvertMarkup(aCommand))
Endif
Endif
Continue
Endif
' Blockquote
I = 0
Do
If Left(sLine) <> ">" Then Break
sCar = Mid$(sLine, 2, 1)
If sCar <> " " And If sCar <> gb.Tab Then Break
Inc I
sLine = LTrim(Mid$(sLine, 3))
Loop
If I > iBlockQuote Then
While I > iBlockQuote
aResult.Add("<blockquote>")
Inc iBlockQuote
Wend
Else If I < iBlockQuote Then
While I < iBlockQuote
aResult.Add("</blockquote>")
Dec iBlockQuote
Wend
Endif
' Lists
If Trim(sLine) Then
iIndent = GetIndent(sLine)
GoSub CLOSE_LIST
sLine = Mid$(sLine, iCurrentIndent + 1)
' Horizontal lines
If Left(sLine) = "*" And If Right(sLine) = "*" Then
sText = Replace(sLine, " ", "")
If Len(sText) >= 3 And If sText = String$(Len(sText), "*") Then
GoSub CLOSE_PARA
aResult.Add("<hr />")
Continue
Endif
Endif
If Left(sLine) = "-" And If Right(sLine) = "-" Then
sText = Replace(sLine, " ", "")
If Len(sText) >= 3 And If sText = String$(Len(sText), "-") Then
GoSub CLOSE_PARA
aResult.Add("<hr />")
Continue
Endif
Endif
If sLine Begins "* " Or If sLine Begins "- " Then
hMarkupList = New MarkupList
hMarkupList.sType = "ul"
hMarkupList.iIndent = iIndent + 1 + GetIndent(Mid$(sLine, 2))
aList.Add(hMarkupList)
bJustList = True
If aResult.Count And If Trim(aResult[aResult.Max]) = "</ul>" Then
If Not Trim(aLine[iLine - 1]) And If iLine < aLine.Max And If GetIndent(aLine[iLine + 1]) >= hMarkupList.iIndent Then
bJustList = False
Endif
aResult.Remove(aResult.Max)
Else
GoSub CLOSE_PARA
aResult.Add(Space$(iCurrentIndent) & "<ul>")
Endif
iCurrentIndent = hMarkupList.iIndent
aResult.Add(Space$(iCurrentIndent) & "<li>")
sLine = Mid$(sLine, 3)
Else If sLine Begins "+ " Then
hMarkupList = New MarkupList
hMarkupList.sType = "ol"
hMarkupList.iIndent = iIndent + 1 + GetIndent(Mid$(sLine, 2))
aList.Add(hMarkupList)
bJustList = True
If aResult.Count And If Trim(aResult[aResult.Max]) = "</ol>" Then
If Not Trim(aLine[iLine - 1]) And If iLine < aLine.Max And If GetIndent(aLine[iLine + 1]) >= hMarkupList.iIndent Then
bJustList = False
Endif
aResult.Remove(aResult.Max)
Else
GoSub CLOSE_PARA
aResult.Add(Space$(iCurrentIndent) & "<ol>")
Endif
iCurrentIndent = hMarkupList.iIndent
aResult.Add(Space$(iCurrentIndent) & "<li>")
sLine = Mid$(sLine, 3)
Endif
Endif
' Blockquote again!
I = iBlockQuote
Do
If Left(sLine) <> ">" Then Break
sCar = Mid$(sLine, 2, 1)
If sCar <> " " And If sCar <> gb.Tab Then Break
Inc I
sLine = LTrim(Mid$(sLine, 3))
Loop
If I > iBlockQuote Then
While I > iBlockQuote
aResult.Add("<blockquote>")
Inc iBlockQuote
Wend
Else If I < iBlockQuote Then
While I < iBlockQuote
aResult.Add("</blockquote>")
Dec iBlockQuote
Wend
Endif
If sLine = "[[" Or If sLine Begins "[[ " Then
GoSub CLOSE_PARA
$aTable.Push("[[")
sLine = Trim(Mid$(sLine, 3))
$aTablePos.Push(aResult.Count)
If sLine Then
aResult.Add("<table class=\"" & sLine & "\">")
Else
aResult.Add("<table>")
Endif
aResult.Add("<tr><th>")
bIgnorePar = True
bInsidePar = True
Continue
Endif
If $aTable.Count Then
If sLine = "]]" Then
GoSub CLOSE_PARA
GoSub CLOSE_CODE
sTable = $aTable.Pop()
iPos = $aTablePos.Pop()
If sTable = "[[" Then
aResult[iPos] = Replace(aResult[iPos], "<table", "<div><div")
aResult.Remove(iPos + 1)
aResult.Add("</div></div>")
Else
aResult.Add("</td></tr>")
aResult.Add("</table>")
Endif
Continue
Else If sLine = "--" Then
GoSub CLOSE_PARA
GoSub CLOSE_CODE
sTable = $aTable[$aTable.Max]
If sTable = "[[" Then
aResult.Add("</th><th>")
Else
aResult.Add("</td><td>")
Endif
bIgnorePar = True
bInsidePar = True
Continue
Else If sLine = "==" Then
GoSub CLOSE_PARA
GoSub CLOSE_CODE
sTable = $aTable[$aTable.Max]
If aResult[aResult.Max] = "<tr><th>" Then
aResult[aResult.Max] = "<tr><td>"
Else If sTable = "[[" Then
aResult.Add("</th></tr><tr><td>")
Else
aResult.Add("</td></tr><tr><td>")
Endif
$aTable[$aTable.Max] = "=="
bIgnorePar = True
bInsidePar = True
Continue
Endif
Endif
If sLine Begins "==" And If sLine = String$(Len(sLine), "=") Then
sLine = aResult[aResult.Max]
If sLine Not Begins "<h" Then
If sLine Begins "<p>" Then
sLine = Mid$(sLine, 4)
bInsidePar = False
Endif
GoSub CLOSE_PARA
aResult[aResult.Max] = "<h1>" & sLine & "</h1>"
Endif
Continue
Endif
If sLine Begins "--" And If sLine = String$(Len(sLine), "-") Then
sLine = Trim(aResult[aResult.Max])
If sLine Then
If sLine Not Begins "<h" Then
If sLine Begins "<p>" Then
sLine = Mid$(sLine, 4)
bInsidePar = False
Endif
GoSub CLOSE_PARA
aResult[aResult.Max] = "<h2>" & sLine & "</h2>"
Endif
Continue
Endif
Endif
' Code
If sLine Begins " " Or If sLine Begins gb.Tab Then
If Left(sLine) = gb.Tab Then
sLine = Mid$(sLine, 2)
Else
sLine = Mid$(sLine, 5)
Endif
sLine = Html$(sLine)
If Not bCode Then
GoSub CLOSE_PARA
bCode = True
sLine = "<div><pre><code>" & sLine
Endif
aResult.Add(sLine)
Continue
Endif
GoSub CLOSE_CODE
' Title
If Left(sLine) = "#" Then
I = InStr(sLine, " ")
If I <= 7 Then
Dec I
If Left(sLine, I) = String$(I, "#") Then
sLine = Mid$(sLine, I + 2)
While sLine Ends "#"
sLine = Left(sLine, -1)
Wend
sLine = RTrim(sLine)
If Left(sLine) = "[" And If Right(sLine) = "]" Then
sLine = ConvertLine(Mid$(sLine, 2, -1))
$aIndex.Add(String$(I - 1, " ") & "- [" & sLine & "] (#t" & CStr($aIndex.Count + 1) & ")")
sLine = "<a name=\"t" & CStr($aIndex.Count) & "\"></a>" & "<h" & CStr(I) & ">" & sLine & "</h" & CStr(I) & ">"
Else
sLine = "<h" & CStr(I) & ">" & ConvertLine(sLine) & "</h" & CStr(I) & ">"
Endif
GoSub CLOSE_PARA
aResult.Add(sLine)
Continue
Endif
Endif
Endif
If Trim(sLine) Then
If Not bInsidePar And If LTrim(sLine) Not Begins "<h" Then
If bIgnorePar Then
bIgnorePar = False
Else If Not bJustList Then
bAddPar = True
'Else
' bAddPar = Not Trim(aLine[iLine - 1])
Endif
Endif
Else
bJustList = False
GoSub CLOSE_PARA
Continue
Endif
sLine = ConvertLine(sLine)
If bAddPar Then
sLine = Left(sLine, GetIndent(sLine)) & "<p>" & LTrim(sLine)
bInsidePar = True
bAddPar = False
Endif
aResult.Add(sLine)
Next
GoSub CLOSE_CODE
GoSub CLOSE_BLOCKQUOTE
iIndent = 0
GoSub CLOSE_LIST
'If $aMarkup.Count Then Error.Raise("Missing markup: " & $aMarkup[$aMarkup.Max])
If $aIndex.Count And If iIndexPos >= 0 Then
iIndent = GetIndent($aIndex[0])
For I = 1 To $aIndex.Max
iIndent = Min(iIndent, GetIndent($aIndex[I]))
Next
If iIndent Then
For I = 0 To $aIndex.Max
$aIndex[I] = Mid$($aIndex[I], iIndent + 1)
Next
Endif
aResult[iIndexPos] = ConvertMarkup($aIndex)
Endif
Return aResult.Join("\n")
CLOSE_CODE:
If bCode Then
aResult.Add("</code></pre></div>")
bCode = False
Endif
Return
CLOSE_BLOCKQUOTE:
While iBlockQuote
aResult.Add("</blockquote>")
Dec iBlockQuote
Wend
Return
CLOSE_LIST:
While iIndent < iCurrentIndent
GoSub CLOSE_PARA
GoSub CLOSE_CODE
aResult.Add(Space$(iCurrentIndent) & "</li>")
bJustList = False
aResult.Add(Space$(aList[aList.Max].iIndent) & "</" & aList[aList.Max].sType & ">")
aList.Remove(aList.Max)
If aList.Count Then
iCurrentIndent = aList[aList.Max].iIndent
Else
iCurrentIndent = 0
Endif
Wend
Return
CLOSE_PARA:
If bInsidePar Then
If Not bIgnorePar Then aResult[aResult.Max] &= "</p>"
'aResult.Add("")
bInsidePar = False
bIgnorePar = False
Else If iLine > 0 And If aResult[aResult.Max] Then
'aResult.Add("")
Endif
Return
'Catch
' Error.Raise("Line " & CStr(iLine + 1) & ": " & Error.Text)
End
Private Sub ConvertLine(sLine As String) As String
Dim sResult As String
Dim I As Integer
Dim sCar As String
Dim I1, I2 As Integer
Dim sPattern As String
Dim bCode As Boolean
Dim bEmph As Boolean
Dim bStrong As Boolean
Dim sText, sTitle, sLink As String
Dim bBlank As Boolean
MAIN_LOOP:
If I >= Len(sLine) Then
If bEmph Then
sResult &= "</em>"
Else If bStrong Then
sResult &= "</strong>"
Endif
Return sResult
Endif
GoSub NEXT_CAR
If sCar = "\\" Then
If I = Len(sLine) Then
sResult &= "<br>"
Else
GoSub NEXT_CAR
sResult &= sCar
Endif
Goto MAIN_LOOP
Endif
If sCar = "<" Then Goto ENTER_MARKUP
If $aMarkup.Count = 0 Then
If sCar = "&" Then Goto ENTER_AMPERSAND
If sCar = "[" And If Mid$(sLine, I + 1, 1) <> " " Then Goto ENTER_LINK
Endif
If sCar = "`" Then Goto ENTER_CODE
If sCar = "*" Then Goto ENTER_STAR
If sCar = ">" Then
sCar = "&gt;"
Endif
sResult &= sCar
Goto MAIN_LOOP
NEXT_CAR:
Inc I
If I > Len(sLine) Then Error.Raise("Unexpected end of line")
sCar = Mid$(sLine, I, 1)
Return
LOOK_CAR:
sCar = Mid$(sLine, I + 1, 1)
Return
ENTER_MARKUP:
I1 = I
GoSub NEXT_CAR
If sCar <> "/" And If Not IsLetter(sCar) Then
sResult &= "&lt;"
Dec I
Goto MAIN_LOOP
Endif
Repeat
GoSub NEXT_CAR
Until sCar = ">"
sPattern = Mid$(sLine, I1 + 1, I - I1 - 1)
'sPattern = LCase(sPattern)
If Left$(sPattern) = "/" Then
If $aMarkup.Count = 0 Or If LCase($aMarkup[$aMarkup.Max]) <> LCase(Mid$(sPattern, 2)) Then
Error.Raise("Mismatched markup: " & sPattern)
Endif
sResult &= "<" & sPattern & ">"
$aMarkup.Remove($aMarkup.Max)
Else
sResult &= "<" & sPattern & ">"
If Right$(sPattern) <> "/" Then
I1 = InStr(sPattern, " ")
If I1 Then sPattern = Left(sPattern, I1 - 1)
$aMarkup.Add(sPattern)
Endif
Endif
Goto MAIN_LOOP
ENTER_AMPERSAND:
For I1 = I To I + 6
sCar = Mid$(sLine, I1, 1)
If sCar = ";" Then
sResult &= Mid$(sLine, I, I1 - I + 1)
I = I1
Goto MAIN_LOOP
Else If sCar = "\n" Then
Break
Endif
Next
sResult &= "&amp;"
Goto MAIN_LOOP
ENTER_CODE:
' sResult &= "<code style=\"white-space:nowrap;\">"
' I1 = 1
' bCode = False
' bCodeExit = False
' Do
' GoSub NEXT_CAR
' If sCar = "`" Then
' If Not bCode Then
' Inc I1
' Continue
' Else
' If Mid$(sLine, I, I1) = String$(I1, "`") Then
' sResult &= "</code>"
' I += I1 - 1
' Break
' Endif
' Endif
' Endif
' sResult &= Html(sCar)
' bCode = True
' Loop
' Goto MAIN_LOOP
If bCode Then
sResult &= "</code>"
bCode = False
Else
sResult &= "<code style=\"white-space:nowrap;\">"
bCode = True
Endif
Goto MAIN_LOOP
ENTER_STAR:
GoSub LOOK_CAR
If sCar = "*" Then
Inc I
Goto ENTER_STRONG
Endif
If bEmph Then
sResult &= "</em>"
bEmph = False
Else If Not bStrong Then
sResult &= "<em>"
bEmph = True
Else
sResult &= "*"
Endif
Goto MAIN_LOOP
ENTER_STRONG:
If bStrong Then
sResult &= "</strong>"
bStrong = False
Else If Not bEmph Then
sResult &= "<strong>"
bStrong = True
Else
sResult &= "**"
Endif
Goto MAIN_LOOP
ENTER_LINK:
I1 = InStr(sLine, "]", I + 1)
If I1 = 0 Then
I = Len(sLine)
Goto MAIN_LOOP
Else If I1 = (I + 1) Then
sResult &= "[]"
Inc I
Goto MAIN_LOOP
Endif
sLink = ""
sTitle = ""
sText = Mid$(sLine, I + 1, I1 - I - 1)
I = I1
I2 = I
Do
GoSub LOOK_CAR
If Not sCar Then Break
Inc I
If Asc(sCar) > 32 Then Break
Loop
If sCar = "(" Then
I1 = InStr(sLine, ")", I + 1)
If I1 > 0 Then
sLink = Mid$(sLine, I + 1, I1 - I - 1)
I = I1
Endif
Else If sCar = "[" Then
I1 = InStr(sLine, "]", I + 1)
If I1 > 0 Then
sLink = $cLink[Mid$(sLine, I + 1, I1 - I - 1)]
I = I1
Endif
Else
I = I2
Endif
If sLink Then
I1 = InStr(sLink, Chr$(34))
If I1 And If Right(sLink) = Chr$(34) Then
sTitle = Mid$(sLink, I1 + 1, -1)
sLink = Trim(Left$(sLink, I1 - 1))
Else
sTitle = ""
sLink = Trim$(sLink)
Endif
Endif
If Not sLink And If InStr(sText, "/") Then
sLink = sText
sText = ""
Endif
$hLink.Link = sLink
$hLink.Text = sText
$hLink.Title = sTitle
$hLink.Html = ""
$hMarkdown.Link($hLink)
If $hLink.Html Then
sResult &= $hLink.Html
Goto MAIN_LOOP
Endif
sLink = $hLink.Link
sText = $hLink.Text
sTitle = $hLink.Title
If Not sText Then sText = sLink
If sLink Then
bBlank = sLink Like "*://*"
sResult &= "<a href=\"" & Html$(sLink) & "\""
If sTitle Then sResult &= " title=\"" & Html$(sTitle) & "\""
If bBlank Then sResult &= " target=\"_blank\""
sResult &= ">" & sText & "</a>"
Endif
Goto MAIN_LOOP
End

View file

@ -23,7 +23,7 @@ Private Sub _PrintPage()
If Not Main.Exist Then
Print "<div class=\"unknown\">This page does not exist."
If Session.Id Then Print " Click on the <b>Create</b> button to create it!"
If Session.Id Then Print "<p>Click on the <b>Create</b> button to create it!"
Print "</div>"
Return
Endif
@ -41,10 +41,7 @@ Private Sub _PrintParent()
Dim sPath As String
sPath = Request.Path
If sPath Then
If File.Name(sPath) = "index" Then sPath = File.Dir(sPath)
sPath = File.Dir(sPath)
Endif
If sPath Then sPath = File.Dir(sPath)
Print Application.Root &/ sPath;
End

View file

@ -6,13 +6,14 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="<%/%>/style.css">
<link rel="icon" href="<%/%>/logo.png" type="image/png">
<title><%_PrintTitle%></title>
<title>Gambas SmallWiki - <%_PrintTitle%></title>
</head>
<body <%If Main.Edit Then%>style="background: #F0F0F0;"<%Endif%>>
<form method="post" enctype="multipart/form-data">
<%If Not Request.Exist("v") Then%>
<div class="header">
<div class="up">
<%If Main.Edit%>
@ -61,13 +62,17 @@
</div>
<%Endif%>
<%Endif%>
<div class="page">
<%If Main.Edit Then%>
<%If Main.Image Then%>
<p>Select the image file to upload...</p>
<input type="file" name="file" />
<%Else%>
<div class="edit-frame">
<textarea class="edit" id="page" name="page"><%_PrintPage%></textarea>
</div>
<script type="text/javascript">document.getElementById('page').focus();</script>
<%Endif%>
<%Else%>

View file

@ -7,7 +7,7 @@ Public Sub Link(hLink As MarkdownLink)
Dim sPath As String
If hLink.Link Begins "/" Then
sPath = Mid$(hLink.Link, 3)
sPath = Mid$(hLink.Link, 2)
Else If hLink.Link Begins "./" Then
sPath = Request.Path &/ Mid$(hLink.Link, 3)
Else

View file

@ -2,8 +2,7 @@ Main
SmallWiki
0
0
0.0.125
3.5.90
gb.markdown
gb.web