|
| 1 | +import { TFile, normalizePath } from "obsidian"; |
| 2 | +import type BetterWordCount from "src/main"; |
| 3 | +import { getCharacterCount, getCitationCount, getFootnoteCount, getPageCount, getSentenceCount, getWordCount } from "src/utils/StatUtils"; |
| 4 | + |
| 5 | +export default class BetterWordCountApi { |
| 6 | + private plugin: BetterWordCount; |
| 7 | + |
| 8 | + constructor(plugin: BetterWordCount) { |
| 9 | + this.plugin = plugin; |
| 10 | + } |
| 11 | + |
| 12 | + // plain utility functions |
| 13 | + public getWordCount(text: string): number { |
| 14 | + return getWordCount(text); |
| 15 | + } |
| 16 | + public getCharacterCount(text: string): number { |
| 17 | + return getCharacterCount(text); |
| 18 | + } |
| 19 | + public getFootnoteCount(text: string): number { |
| 20 | + return getFootnoteCount(text); |
| 21 | + } |
| 22 | + public getCitationCount(text: string): number { |
| 23 | + return getCitationCount(text); |
| 24 | + } |
| 25 | + public getSentenceCount(text: string): number { |
| 26 | + return getSentenceCount(text); |
| 27 | + } |
| 28 | + public getPageCount(text: string, pageWords: number = this.plugin.settings.pageWords): number { |
| 29 | + return getPageCount(text, pageWords); |
| 30 | + } |
| 31 | + |
| 32 | + // Functions using page paths e.g. for use with dataviewjs |
| 33 | + private async countPagePath(path: string, countFunc: (text: string) => number): Promise<number | null> { |
| 34 | + const normalizedPath = normalizePath(path); |
| 35 | + const file = this.plugin.app.vault.getAbstractFileByPath(normalizedPath); |
| 36 | + |
| 37 | + // Check if it exists and is of the correct type |
| 38 | + if (file instanceof TFile) { |
| 39 | + const text = await this.plugin.app.vault.cachedRead(file); |
| 40 | + return countFunc(text); |
| 41 | + } |
| 42 | + |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + public async getWordCountPagePath(path: string): Promise<number | null> { |
| 47 | + return this.countPagePath(path, getWordCount); |
| 48 | + } |
| 49 | + public getCharacterCountPagePath(path: string): Promise<number | null> { |
| 50 | + return this.countPagePath(path, getCharacterCount); |
| 51 | + } |
| 52 | + public getFootnoteCountPagePath(path: string): Promise<number | null> { |
| 53 | + return this.countPagePath(path, getFootnoteCount); |
| 54 | + } |
| 55 | + public getCitationCountPagePath(path: string): Promise<number | null> { |
| 56 | + return this.countPagePath(path, getCitationCount); |
| 57 | + } |
| 58 | + public getSentenceCountPagePath(path: string): Promise<number | null> { |
| 59 | + return this.countPagePath(path, getSentenceCount); |
| 60 | + } |
| 61 | + public getPageCountPagePath(path: string, pageWords: number = this.plugin.settings.pageWords): Promise<number | null> { |
| 62 | + return this.countPagePath(path, (text: string) => getPageCount(text, pageWords)); |
| 63 | + } |
| 64 | + |
| 65 | + // Functions for accessing stats |
| 66 | + public getDailyWords(): number | null { |
| 67 | + if (!this.plugin.statsManager) return null; |
| 68 | + return this.plugin.statsManager.getDailyWords(); |
| 69 | + } |
| 70 | + |
| 71 | + public getDailyCharacters(): number | null { |
| 72 | + if (!this.plugin.statsManager) return null; |
| 73 | + return this.plugin.statsManager.getDailyCharacters(); |
| 74 | + } |
| 75 | + |
| 76 | + public getDailySentences(): number | null { |
| 77 | + if (!this.plugin.statsManager) return null; |
| 78 | + return this.plugin.statsManager.getDailySentences(); |
| 79 | + } |
| 80 | + |
| 81 | + public getDailyFootnotes(): number | null { |
| 82 | + if (!this.plugin.statsManager) return null; |
| 83 | + return this.plugin.statsManager.getDailyFootnotes(); |
| 84 | + } |
| 85 | + |
| 86 | + public getDailyCitations(): number | null { |
| 87 | + if (!this.plugin.statsManager) return null; |
| 88 | + return this.plugin.statsManager.getDailyCitations(); |
| 89 | + } |
| 90 | + |
| 91 | + public getDailyPages(): number | null { |
| 92 | + if (!this.plugin.statsManager) return null; |
| 93 | + return this.plugin.statsManager.getDailyPages(); |
| 94 | + } |
| 95 | + |
| 96 | + public getTotalFiles(): number | null { |
| 97 | + if (!this.plugin.statsManager) return null; |
| 98 | + return this.plugin.statsManager.getTotalFiles(); |
| 99 | + } |
| 100 | + |
| 101 | + public async getTotalWords(): Promise<number | null> { |
| 102 | + if (!this.plugin.statsManager) return null; |
| 103 | + return this.plugin.statsManager.getTotalWords(); |
| 104 | + } |
| 105 | + |
| 106 | + public async getTotalCharacters(): Promise<number | null> { |
| 107 | + if (!this.plugin.statsManager) return null; |
| 108 | + return this.plugin.statsManager.getTotalCharacters(); |
| 109 | + } |
| 110 | + |
| 111 | + public async getTotalSentences(): Promise<number | null> { |
| 112 | + if (!this.plugin.statsManager) return null; |
| 113 | + return this.plugin.statsManager.getTotalSentences(); |
| 114 | + } |
| 115 | + |
| 116 | + public async getTotalFootnotes(): Promise<number | null> { |
| 117 | + if (!this.plugin.statsManager) return null; |
| 118 | + return this.plugin.statsManager.getTotalFootnotes(); |
| 119 | + } |
| 120 | + |
| 121 | + public async getTotalCitations(): Promise<number | null> { |
| 122 | + if (!this.plugin.statsManager) return null; |
| 123 | + return this.plugin.statsManager.getTotalCitations(); |
| 124 | + } |
| 125 | + |
| 126 | + public async getTotalPages(): Promise<number | null> { |
| 127 | + if (!this.plugin.statsManager) return null; |
| 128 | + return this.plugin.statsManager.getTotalPages(); |
| 129 | + } |
| 130 | +} |
0 commit comments