Finish write unify parser

main
Leonid Maslakov 5 months ago
parent b07d839503
commit 0884484e2e
  1. 247
      highlight.go
  2. 36
      highlight_dockerfile.go
  3. 21
      highlight_golang.go
  4. 16
      highlight_python.go
  5. 7
      highlight_robots_txt.go

@ -282,250 +282,3 @@ func formatOpenClose(text string, openStr string, closeStr string, styleClass st
return result
}
// Processes `"` and `'` brackets.
func formatBrackets(text string) string {
result := ""
textRune := []rune(text)
textLen := len(textRune)
otherSpanTagOpen := 0
markOpen := false // `"`
singleMarkOpen := false // `'`
for i := range textRune {
char := string(textRune[i])
// Find <span
if textLen > i+5 {
if text[i:i+5] == "<span" {
otherSpanTagOpen = otherSpanTagOpen + 1
}
}
// Find </span>
if textLen > i+7 {
if text[i:i+7] == "</span>" {
otherSpanTagOpen = otherSpanTagOpen - 1
}
}
// Check
if char == `"` && singleMarkOpen == false && otherSpanTagOpen == 0 {
if markOpen == false {
result = result + "<span class='" + StyleBrackets + "'>" + `"`
markOpen = true
} else {
result = result + `"` + "</span>"
markOpen = false
}
} else if char == `'` && markOpen == false && otherSpanTagOpen == 0 {
if singleMarkOpen == false {
result = result + "<span class='" + StyleBrackets + "'>" + `'`
singleMarkOpen = true
} else {
result = result + `'` + "</span>"
singleMarkOpen = false
}
} else {
result = result + char
}
}
// Close not closed <span> tags
if markOpen == true {
result = result + "</span>"
markOpen = false
}
if singleMarkOpen == true {
result = result + "</span>"
singleMarkOpen = false
}
return result
}
// Processes '//' (C-like) comments.
func formatCComment(line string) string {
result := ""
lineRune := []rune(line)
lineLen := len(lineRune)
otherSpanTagOpen := 0
commentOpen := false
for i, charRune := range lineRune {
// Current char
char := string(charRune)
// Get next char
nextChar := ""
if lineLen > i+1 {
nextChar = string(lineRune[i+1])
}
// Find <span
if lineLen > i+5 {
if line[i:i+5] == "<span" {
otherSpanTagOpen = otherSpanTagOpen + 1
}
}
// Find </span>
if lineLen > i+7 {
if line[i:i+7] == "</span>" {
otherSpanTagOpen = otherSpanTagOpen - 1
}
}
// Parse
if commentOpen == false && char == "/" && nextChar == "/" && otherSpanTagOpen == 0 {
result = result + "<span class='" + StyleComment + "'>/"
commentOpen = true
continue
}
if commentOpen == true && char == "\n" {
result = result + "</span>\n"
commentOpen = false
continue
}
result = result + char
}
// Close not closed <span> tags
if commentOpen == true {
result = result + "</span>"
}
return result
}
// Processes '#' (sharp) comments.
func formatSharpComment(text string) string {
result := ""
textRune := []rune(text)
textLen := len(textRune)
otherSpanTagOpen := 0
commentOpen := false
for i, charRune := range textRune {
char := string(charRune)
// Find <span
if textLen > i+5 {
if text[i:i+5] == "<span" {
otherSpanTagOpen = otherSpanTagOpen + 1
}
}
// Find </span>
if textLen > i+7 {
if text[i:i+7] == "</span>" {
otherSpanTagOpen = otherSpanTagOpen - 1
}
}
// Parse
if commentOpen == false && char == "#" && otherSpanTagOpen == 0 {
result = result + "<span class='" + StyleComment + "'>#"
commentOpen = true
continue
}
if commentOpen == true && char == "\n" {
result = result + "</span>\n"
commentOpen = false
continue
}
result = result + char
}
// Close not closed <span> tags
if commentOpen == true {
result = result + "</span>"
}
return result
}
// Processes multi-line comments ('/* comment */').
func formatCMultiComment(text string) string {
result := ""
textRune := []rune(text)
textLen := len(textRune)
commentOpen := false
otherSpanTagOpen := 0
skip := 0
for i, charRune := range textRune {
// Skip
if skip != 0 {
skip = skip - 1
continue
}
// Current char
char := string(charRune)
// Get next char
nextChar := ""
if textLen > i+1 {
nextChar = string(textRune[i+1])
}
// Find <span
if textLen > i+5 {
if text[i:i+5] == "<span" {
otherSpanTagOpen = otherSpanTagOpen + 1
}
}
// Find </span>
if textLen > i+7 {
if text[i:i+7] == "</span>" {
otherSpanTagOpen = otherSpanTagOpen - 1
}
}
// Parse comment open: /*
if commentOpen == false && char == "/" && nextChar == "*" && otherSpanTagOpen == 0 {
result = result + "<span class='" + StyleComment + "'>/*"
skip = 1
commentOpen = true
continue
}
// Parse comment close: */
if commentOpen == true && char == "*" && nextChar == "/" && otherSpanTagOpen == 0 {
result = result + "*/</span>"
skip = 1
commentOpen = false
continue
}
result = result + char
}
if commentOpen == true {
result = result + "</span>"
commentOpen = false
}
return result
}

@ -18,10 +18,6 @@
package highlight
import (
"strings"
)
// Dockerfile processes Docker builder files.
// Read more: https://docs.docker.com/engine/reference/builder/
//
@ -47,14 +43,9 @@ import (
//
// Comments (#) are also supported.
func Dockerfile(code string) string {
var result string = ""
// Shild HTML
code = shieldHTML(code)
lines := strings.Split(code, "\n")
linesNum := len(lines)
// Commands list
commands := []string{
"FROM", "RUN", "CMD",
@ -65,26 +56,17 @@ func Dockerfile(code string) string {
"STOPSIGNAL", "HEALTHCHECK", "SHELL",
}
for i := range lines {
line := lines[i]
// Comment
line = formatSharpComment(line)
// Brackets
line = formatBrackets(line)
// Sinle-line comments
code = formatOpenClose(code, "#", "\n", StyleComment)
// Commands
for _, cmd := range commands {
line = formatWord(line, cmd, []string{"", " ", "\t"}, []string{"", " ", "\t"}, StyleKeyword)
}
// Single-line brackets
code = formatOpenClose(code, `"`, `"`, StyleBrackets)
code = formatOpenClose(code, `'`, `'`, StyleBrackets)
// Save
if linesNum != i+1 {
result = result + line + "\n"
} else {
result = result + line
}
// Commands
for _, cmd := range commands {
code = formatWord(code, cmd, []string{"", " ", "\t", "\n"}, []string{"", " ", "\t", "\n"}, StyleKeyword)
}
return result
return code
}

@ -94,8 +94,11 @@ package highlight
// rune
// string
//
// Single-line comments (//) and multi-line comments (/* */).
// Brackets(", ') are also supported.
// Also supported:
// Single-line comments (//)
// Multi-line comments (/* */)
// Single-line brackets (", ')
// Multi-line brackets (` `)
func Golang(code string) string {
// Shild HTML
code = shieldHTML(code)
@ -156,13 +159,17 @@ func Golang(code string) string {
}
// Multi-line comments
code = formatCMultiComment(code)
code = formatOpenClose(code, "/*", "*/", StyleComment)
// Single-line comment
code = formatCComment(code)
// Multi-line brackets
code = formatOpenClose(code, "`", "`", StyleBrackets)
// Brackets
code = formatBrackets(code)
// Single-line comments
code = formatOpenClose(code, "//", "\n", StyleComment)
// Single-line brackets
code = formatOpenClose(code, `"`, `"`, StyleBrackets)
code = formatOpenClose(code, `'`, `'`, StyleBrackets)
// Keywords
for _, word := range keywords {

@ -163,7 +163,11 @@ package highlight
// zip()
// __import__()
//
// Single-line comments (#) and brackets(", ') are also supported.
// Also supported:
// Single-line comments (#)
// Multi-line comments (''' ''')
// Single-line brackets (", ')
// Multi-line brackets (""" """)
func Python(code string) string {
// Shild HTML
code = shieldHTML(code)
@ -259,10 +263,16 @@ func Python(code string) string {
"", " ", "\t", ":", ";", "(", ")",
}
// Comment
// Multi-line comments
code = formatOpenClose(code, `'''`, `'''`, StyleComment)
// Multi-line brackets
code = formatOpenClose(code, `"""`, `"""`, StyleBrackets)
// Sinle-line comments
code = formatOpenClose(code, "#", "\n", StyleComment)
// Brackets
// Single-line brackets
code = formatOpenClose(code, `"`, `"`, StyleBrackets)
code = formatOpenClose(code, `'`, `'`, StyleBrackets)

@ -42,15 +42,16 @@ func RobotsTxt(code string) string {
// Shild HTML
code = shieldHTML(code)
// Sinle-line comments
code = formatOpenClose(code, "#", "\n", StyleComment)
// Directives
lines := strings.Split(code, "\n")
linesNum := len(lines)
for i := range lines {
line := lines[i]
// Comment
line = formatSharpComment(line)
// Standard
line = replacePrefix(line, "User-agent:", "<span class='"+StyleKeyword+"'>User-agent:</span>")
line = replacePrefix(line, "Disallow:", "<span class='"+StyleKeyword+"'>Disallow:</span>")

Loading…
Cancel
Save