Skip to content

Commit

Permalink
migrate images to own tex file
Browse files Browse the repository at this point in the history
  • Loading branch information
nce committed Sep 21, 2024
1 parent 050da27 commit 4ddf43b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ linters:
- funlen
- testpackage


linters-settings:
gocognit:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 40
cyclop:
# The maximal code complexity to report.
# Default: 10
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
minimum_pre_commit_version: '2.17'
exclude: 'pkg/stravaapi/.*'
minimum_pre_commit_version: "2.17"
exclude: "pkg/stravaapi/.*"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand All @@ -19,7 +19,7 @@ repos:
- id: go-vet
- id: go-imports
- id: go-cyclo
args: [-over=15]
args: [-over=25]
- id: validate-toml
- id: no-go-testing
- id: golangci-lint
Expand Down
103 changes: 103 additions & 0 deletions pkg/migrate/splitdescription.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,106 @@ func SplitDescriptionFile(textDir string) (bool, error) {

return true, nil
}

func SplitImagesIncludeInOwnFile(textDir string) (bool, error) {
// check if file already exists, assume it's setup correct
if _, err := os.Stat(textDir + "images.tex"); err == nil {
return false, nil
}

sourceFile, err := os.Open(textDir + "description.md")
if err != nil {
return false, fmt.Errorf("error opening file: %w", err)
}
defer sourceFile.Close()

// create a temporary file for the modified description file
tempFile, err := os.CreateTemp("", "description_temp_*.md")
if err != nil {
return false, fmt.Errorf("error creating temp file: %w", err)
}
defer tempFile.Close()

scanner := bufio.NewScanner(sourceFile)

var insideFigureBlock bool

var figureContent []string

var destFile *os.File

for scanner.Scan() {
line := scanner.Text()

// Check if line starts with \begin{figure}[b!]
if strings.HasPrefix(line, `\begin{figure}[b!]`) {
insideFigureBlock = true

figureContent = append(figureContent, `\hfill`+"\n")
figureContent = append(figureContent, line)

destFile, err = os.Create("images.tex")
if err != nil {
return false, fmt.Errorf("error creating destination file: %w", err)
}
defer destFile.Close()

continue
}

// If inside the figure block, collect the lines but don't write them to the temp file
//nolint: nestif
if insideFigureBlock {
figureContent = append(figureContent, line)

if strings.HasPrefix(line, `\end{figure}`) {
insideFigureBlock = false
// Write the figure block to images.tex
for _, figLine := range figureContent {
_, err := destFile.WriteString(figLine + "\n")
if err != nil {
return false, fmt.Errorf("error writing image content to images.tex: %w", err)
}
}

_, err := destFile.WriteString("\n") // Add a blank line after each figure block
if err != nil {
return false, fmt.Errorf("error writing newline to new file: %w", err)
}

figureContent = nil
}

continue
}

// Write non-figure block lines to the temporary file
// remove all \hfill attributes
if line != `\hfill` {
_, err := tempFile.WriteString(line + "\n")
if err != nil {
return false, fmt.Errorf("error writing existing content to tempfile: %w", err)
}
}
}

if insideFigureBlock {
_, err := tempFile.WriteString("\n" + `\input{\textpath/images}`)
if err != nil {
return false, fmt.Errorf("error writing image include to description: %w", err)
}
}

if err := scanner.Err(); err != nil {
return false, fmt.Errorf("error reading file: %w", err)
}

tempFile.Close()

// replace the original file with the updated temp file
if err := os.Rename(tempFile.Name(), textDir+"description.md"); err != nil {
return false, fmt.Errorf("error replacing the original file: %w", err)
}

return true, nil
}
4 changes: 3 additions & 1 deletion pkg/strava/strava.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ func FetchStravaData(date time.Time) (*Activity, error) {
apiClient := api.NewAPIClient(configuration)

opts := &api.ActivitiesApiGetLoggedInAthleteActivitiesOpts{
//nolint: gosec
Before: optional.NewInt32(int32(date.Add(24 * time.Hour).Unix())),
After: optional.NewInt32(int32(date.Unix())),
//nolint: gosec
After: optional.NewInt32(int32(date.Unix())),
}

allActivites, response, err := apiClient.ActivitiesApi.GetLoggedInAthleteActivities(context.Background(), opts)
Expand Down

0 comments on commit 4ddf43b

Please sign in to comment.