-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(gnoweb): cleanup iteration on gnoweb #3379
Open
gfanton
wants to merge
21
commits into
gnolang:master
Choose a base branch
from
gfanton:feat/gnoweb/tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
60dd19c
fix: simplify url system
gfanton 68b5896
wip: more tests
gfanton 0d9b8a2
fix: improve url
gfanton 845f2d9
feat: improve url tests
gfanton 10bed00
fix: simplify more and add comments
gfanton beda4a2
chore: lint
gfanton 689e355
feat: add public static tests
gfanton 7c8f309
chore: remove println
gfanton c09975d
feat: cleanup gnoweb
gfanton c1433c0
wip: cleanup weblient
gfanton 9aa62cb
wip: rework handler
gfanton 7ba7de4
wip: rework webclient 2
gfanton 0ad6893
wip: rework app
gfanton c135697
wip: handler mock
gfanton 0490da1
feat: add handler test
gfanton a13c2f9
chore: cleanup tests
gfanton d5df397
chore: lint
gfanton cbeafb9
chore: rename highlighter to format
gfanton 792dc4e
feat: add handler tests
gfanton fd8035d
chore: lint
gfanton 3c334b3
Merge remote-tracking branch 'origin/master' into feat/gnoweb/tests
gfanton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't we discuss this getting this from the remote genesis.json, together with the chain id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should ideally be queried from the running node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely want to do it. However, I didn't change any real behaviors (or really minimally, like some errors) in this PR. It's mostly a cleanup iteration with documentation and tests. So i prefer keeping this for a next PR.