-
Notifications
You must be signed in to change notification settings - Fork 601
feat(background-file) Add the background-file CLI option #206
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
Open
Victor-D
wants to merge
1
commit into
alibaba:main
Choose a base branch
from
Victor-D:feat/background_from_file
base: main
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
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 hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
| "unicode" | ||
| ) | ||
|
|
||
| const ( | ||
| backgroundSoftLimit = 2000 | ||
| backgroundHardLimit = 8000 | ||
| backgroundOpenTag = "<ocr_user_background>" | ||
| backgroundCloseTag = "</ocr_user_background>" | ||
| maxBackgroundFileBytes = 1 << 20 // 1 MB | ||
| ) | ||
|
|
||
| var multiNewline = regexp.MustCompile(`\n{3,}`) | ||
|
|
||
| // mergeBackground combines the inline --background value (or an auto-populated | ||
| // commit message) with the content read from --background-file, separated by a | ||
| // blank line. The inline value is sanitised the same way as the file content so | ||
| // both portions are cleaned consistently. The file content is already wrapped | ||
| // and sanitised by loadBackgroundFile. | ||
| func mergeBackground(inline, fromFile string) string { | ||
| inline = sanitizeMarkdown(inline) | ||
| switch { | ||
| case inline == "": | ||
| return fromFile | ||
| case fromFile == "": | ||
| return inline | ||
| default: | ||
| return inline + "\n\n" + fromFile | ||
| } | ||
| } | ||
|
|
||
| func loadBackgroundFile(path string) (string, error) { | ||
| info, err := os.Stat(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("read background file %q: %w", path, err) | ||
| } | ||
| if info.IsDir() { | ||
| return "", fmt.Errorf("background file %q is a directory, not a file", path) | ||
| } | ||
| if info.Size() > maxBackgroundFileBytes { | ||
| return "", fmt.Errorf( | ||
| "background file %q is %d bytes, exceeding the maximum of %d bytes; please provide a smaller file", | ||
| path, info.Size(), maxBackgroundFileBytes, | ||
| ) | ||
| } | ||
|
|
||
| raw, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("read background file %q: %w", path, err) | ||
| } | ||
|
|
||
| cleaned := sanitizeMarkdown(string(raw)) | ||
| if cleaned == "" { | ||
| return "", fmt.Errorf("background file %q is empty after sanitisation", path) | ||
| } | ||
|
|
||
| if strings.Contains(cleaned, backgroundOpenTag) || strings.Contains(cleaned, backgroundCloseTag) { | ||
| return "", fmt.Errorf( | ||
| "background file %q must not contain the reserved delimiters %q or %q", | ||
| path, backgroundOpenTag, backgroundCloseTag, | ||
| ) | ||
| } | ||
|
|
||
| wrapped := backgroundOpenTag + "\n" + cleaned + "\n" + backgroundCloseTag | ||
|
Victor-D marked this conversation as resolved.
|
||
|
|
||
| if n := len([]rune(wrapped)); n > backgroundHardLimit { | ||
| return "", fmt.Errorf( | ||
| "background content is %d characters, exceeding the hard limit of %d (aborting)", | ||
| n, backgroundHardLimit, | ||
| ) | ||
| } else if n > backgroundSoftLimit { | ||
| fmt.Fprintf(os.Stderr, | ||
| "[ocr] --background-file content is %d characters, exceeding the recommended %d (continuing but review quality might be impacted)\n", | ||
| n, backgroundSoftLimit, | ||
| ) | ||
| } | ||
|
|
||
| return wrapped, nil | ||
| } | ||
|
|
||
| func sanitizeMarkdown(s string) string { | ||
| var b strings.Builder | ||
| b.Grow(len(s)) | ||
|
|
||
| for _, r := range s { | ||
| switch r { | ||
| case '\n', '\t': | ||
| b.WriteRune(r) | ||
| continue | ||
| case '\r': | ||
| continue | ||
| } | ||
| if isForbiddenChar(r) { | ||
| continue | ||
| } | ||
| b.WriteRune(r) | ||
| } | ||
|
|
||
| collapsed := multiNewline.ReplaceAllString(b.String(), "\n\n") | ||
| return strings.TrimSpace(collapsed) | ||
| } | ||
|
|
||
| func isForbiddenChar(r rune) bool { | ||
| switch { | ||
| case r <= 0x1F: // C0 control characters (includes NUL) | ||
| return true | ||
| case r >= 0x7F && r <= 0x9F: // DEL and C1 control characters | ||
| return true | ||
| } | ||
|
|
||
| switch r { | ||
| case '\u200B', // zero-width space | ||
| '\u200C', // zero-width non-joiner | ||
| '\u200D', // zero-width joiner | ||
| '\u200E', // left-to-right mark | ||
| '\u200F', // right-to-left mark | ||
| '\u2060', // word joiner | ||
| '\u00AD', // soft hyphen | ||
| '\uFEFF': // BOM / zero-width no-break space | ||
| return true | ||
| } | ||
|
|
||
| return unicode.Is(unicode.Cf, r) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.