-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(gnoweb): cleanup iteration on gnoweb (#3379)
depends on #3366 This PR cleans up, documents, and reorganizes the `gnoweb` package, which was recently revamped: * Refactored the code for better readability and structure, and added enhanced comments. * Enhanced existing test cases: * Added new test cases for `assets` in `app_test.go`. * Included a new test rule in the Makefile. * Created new tests for WebHandler in `handler_test.go`. * Improved file and directory handling methods in `handler.go`. --------- Signed-off-by: gfanton <[email protected]> Co-authored-by: Morgan <[email protected]>
- Loading branch information
Showing
12 changed files
with
710 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package gnoweb | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/alecthomas/chroma/v2" | ||
"github.com/alecthomas/chroma/v2/formatters/html" | ||
"github.com/alecthomas/chroma/v2/lexers" | ||
) | ||
|
||
// FormatSource defines the interface for formatting source code. | ||
type FormatSource interface { | ||
Format(w io.Writer, fileName string, file []byte) error | ||
} | ||
|
||
// ChromaSourceHighlighter implements the Highlighter interface using the Chroma library. | ||
type ChromaSourceHighlighter struct { | ||
*html.Formatter | ||
style *chroma.Style | ||
} | ||
|
||
// NewChromaSourceHighlighter constructs a new ChromaHighlighter with the given formatter and style. | ||
func NewChromaSourceHighlighter(formatter *html.Formatter, style *chroma.Style) FormatSource { | ||
return &ChromaSourceHighlighter{Formatter: formatter, style: style} | ||
} | ||
|
||
// Format applies syntax highlighting to the source code using Chroma. | ||
func (f *ChromaSourceHighlighter) Format(w io.Writer, fileName string, src []byte) error { | ||
var lexer chroma.Lexer | ||
|
||
// Determine the lexer to be used based on the file extension. | ||
switch strings.ToLower(filepath.Ext(fileName)) { | ||
case ".gno": | ||
lexer = lexers.Get("go") | ||
case ".md": | ||
lexer = lexers.Get("markdown") | ||
case ".mod": | ||
lexer = lexers.Get("gomod") | ||
default: | ||
lexer = lexers.Get("txt") // Unsupported file type, default to plain text. | ||
} | ||
|
||
if lexer == nil { | ||
return fmt.Errorf("unsupported lexer for file %q", fileName) | ||
} | ||
|
||
iterator, err := lexer.Tokenise(nil, string(src)) | ||
if err != nil { | ||
return fmt.Errorf("unable to tokenise %q: %w", fileName, err) | ||
} | ||
|
||
if err := f.Formatter.Format(w, f.style, iterator); err != nil { | ||
return fmt.Errorf("unable to format source file %q: %w", fileName, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// noopFormat is a no-operation highlighter that writes the source code as-is. | ||
type noopFormat struct{} | ||
|
||
// Format writes the source code to the writer without any formatting. | ||
func (f *noopFormat) Format(w io.Writer, fileName string, src []byte) error { | ||
_, err := w.Write(src) | ||
return err | ||
} |
Oops, something went wrong.