-
Notifications
You must be signed in to change notification settings - Fork 1
Add draft exercise entry parsing scaffolding #151
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
forketyfork
wants to merge
5
commits into
main
Choose a base branch
from
codex/add-exercise-tracking-functionality
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b55a926
Add draft exercise entry parsing
forketyfork 628fac0
Add per-rep workout calorie resolution
forketyfork 98a60a6
Address review feedback
forketyfork 15c6c57
Merge branch 'main' into codex/add-exercise-tracking-functionality
forketyfork c76f354
Merge branch 'main' into codex/add-exercise-tracking-functionality
forketyfork 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { SPECIAL_CHARS_REGEX } from "./constants"; | ||
|
|
||
| export interface ExerciseEntry { | ||
| name: string; | ||
| sets: number[]; | ||
| weight?: { | ||
| value: number; | ||
| unit: string; | ||
| }; | ||
| lineNumber: number; | ||
| rawText: string; | ||
| } | ||
|
|
||
| /** | ||
| * Lightweight parser for exercise entries in daily notes. | ||
| * | ||
| * Expected format: #workout [[Exercise Name]] 40kg 15-15-15 | ||
| */ | ||
| export default class ExerciseEntryParser { | ||
| private workoutTag: string; | ||
|
|
||
| constructor(workoutTag: string) { | ||
| this.workoutTag = workoutTag.trim(); | ||
| } | ||
|
|
||
| updateWorkoutTag(newTag: string): void { | ||
| this.workoutTag = newTag.trim(); | ||
| } | ||
|
|
||
| parse(content: string): ExerciseEntry[] { | ||
| if (this.workoutTag.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| const escapedTag = this.workoutTag.replace(SPECIAL_CHARS_REGEX, "\\$&"); | ||
| const regex = new RegExp( | ||
| `^#${escapedTag}\\s+(?<name>\\[\\[[^\\]]+\\]\\]|[^\\d\\r\\n]+?)\\s+(?:(?<weight>\\d+(?:\\.\\d+)?)(?<unit>kg|kgs?|lb|lbs?)?\\s+)?(?<sets>\\d+(?:-\\d+)*)(?=\\s*$|\\s+[^\\s])`, | ||
| "i" | ||
| ); | ||
|
|
||
| return content | ||
| .split(/\r?\n/) | ||
| .map((line, index) => this.parseLine(line.trim(), index + 1, regex)) | ||
| .filter((entry): entry is ExerciseEntry => entry !== null); | ||
| } | ||
|
|
||
| private parseLine(line: string, lineNumber: number, regex: RegExp): ExerciseEntry | null { | ||
| if (line.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const match = line.match(regex); | ||
| if (!match?.groups) { | ||
| return null; | ||
| } | ||
|
|
||
| const sets = match.groups.sets | ||
| .split("-") | ||
| .map(value => Number.parseInt(value, 10)) | ||
| .filter(reps => !Number.isNaN(reps) && reps > 0); | ||
|
|
||
| if (sets.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const name = this.extractName(match.groups.name); | ||
| const weight = this.parseWeight(match.groups.weight, match.groups.unit); | ||
|
|
||
| return { | ||
| name, | ||
| sets, | ||
| weight: weight ?? undefined, | ||
| lineNumber, | ||
| rawText: line, | ||
| }; | ||
| } | ||
|
|
||
| private extractName(rawName: string): string { | ||
| const trimmedName = rawName.trim(); | ||
| if (trimmedName.startsWith("[[") && trimmedName.endsWith("]]")) { | ||
| return trimmedName.slice(2, -2).trim(); | ||
| } | ||
| return trimmedName; | ||
| } | ||
|
|
||
| private parseWeight(rawWeight?: string, unit?: string): ExerciseEntry["weight"] | null { | ||
| if (!rawWeight) { | ||
| return null; | ||
| } | ||
|
|
||
| const value = Number.parseFloat(rawWeight); | ||
| if (Number.isNaN(value) || value <= 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const normalizedUnit = unit?.toLowerCase() ?? ""; | ||
| return { | ||
| value, | ||
| unit: normalizedUnit.length > 0 ? normalizedUnit : "kg", | ||
forketyfork marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| } | ||
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,78 @@ | ||
| import { App } from "obsidian"; | ||
|
|
||
| const CALORIES_PER_REP_KEYS = ["kcal_per_rep", "calories_per_rep"]; | ||
|
|
||
| export default class ExerciseMetadataService { | ||
| private app: App; | ||
| private cache: Map<string, number | null> = new Map(); | ||
|
|
||
| constructor(app: App) { | ||
| this.app = app; | ||
| } | ||
|
|
||
| getCaloriesPerRep(exerciseName: string, sourcePath?: string): number | null { | ||
| const normalizedName = exerciseName.trim(); | ||
| if (normalizedName.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const cacheKey = `${normalizedName}::${sourcePath ?? ""}`; | ||
| if (this.cache.has(cacheKey)) { | ||
| return this.cache.get(cacheKey) ?? null; | ||
| } | ||
|
|
||
| try { | ||
| const file = this.app.metadataCache.getFirstLinkpathDest(normalizedName, sourcePath ?? ""); | ||
| if (!file) { | ||
| this.cache.set(cacheKey, null); | ||
| return null; | ||
| } | ||
|
|
||
| const frontmatter = this.app.metadataCache.getFileCache(file)?.frontmatter; | ||
| if (!frontmatter) { | ||
| this.cache.set(cacheKey, null); | ||
| return null; | ||
| } | ||
|
|
||
| const caloriesPerRep = this.extractCaloriesPerRep(frontmatter); | ||
| this.cache.set(cacheKey, caloriesPerRep); | ||
| return caloriesPerRep; | ||
| } catch (error) { | ||
| console.error(`Error resolving exercise calories for ${normalizedName}:`, error); | ||
| this.cache.set(cacheKey, null); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| clear(): void { | ||
| this.cache.clear(); | ||
| } | ||
|
|
||
| private extractCaloriesPerRep(frontmatter: Record<string, unknown>): number | null { | ||
| for (const key of CALORIES_PER_REP_KEYS) { | ||
| const value = frontmatter[key]; | ||
| const parsed = this.parseCaloriesPerRep(value); | ||
| if (parsed !== null) { | ||
| return parsed; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private parseCaloriesPerRep(value: unknown): number | null { | ||
| if (value === null || value === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| const numericValue = | ||
| typeof value === "number" ? value : typeof value === "string" ? Number.parseFloat(value) : null; | ||
|
|
||
| if (numericValue === null) { | ||
| return null; | ||
| } | ||
| if (Number.isNaN(numericValue) || numericValue <= 0) { | ||
| return null; | ||
| } | ||
| return numericValue; | ||
| } | ||
| } |
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
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.