refactor: Standardize method names and introduce context propagation

Removes the `Get` prefix from exporter methods (e.g., GetSupportedFormat -> SupportedFormat) to better align with Go conventions for simple accessors.

Introduces `context.Context` propagation through the application, starting from `ProcessCourseFromURI` down to the HTTP request in the parser. This makes network operations cancellable and allows for setting deadlines, improving application robustness.

Additionally, optimizes the HTML cleaner by pre-compiling regular expressions for a minor performance gain.
This commit is contained in:
2025-11-06 04:25:54 +01:00
parent 65469ea52e
commit 2790064ad5
16 changed files with 90 additions and 69 deletions

View File

@ -191,10 +191,10 @@ func (e *DocxExporter) exportSubItem(doc *docx.Docx, subItem *models.SubItem) {
}
}
// GetSupportedFormat returns the format name this exporter supports.
// SupportedFormat returns the format name this exporter supports.
//
// Returns:
// - A string representing the supported format ("docx")
func (e *DocxExporter) GetSupportedFormat() string {
func (e *DocxExporter) SupportedFormat() string {
return "docx"
}

View File

@ -30,13 +30,13 @@ func TestNewDocxExporter(t *testing.T) {
}
}
// TestDocxExporter_GetSupportedFormat tests the GetSupportedFormat method.
func TestDocxExporter_GetSupportedFormat(t *testing.T) {
// TestDocxExporter_SupportedFormat tests the SupportedFormat method.
func TestDocxExporter_SupportedFormat(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner()
exporter := NewDocxExporter(htmlCleaner)
expected := "docx"
result := exporter.GetSupportedFormat()
result := exporter.SupportedFormat()
if result != expected {
t.Errorf("Expected format '%s', got '%s'", expected, result)

View File

@ -55,11 +55,11 @@ func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) {
}
}
// GetSupportedFormats returns a list of all supported export formats.
// SupportedFormats returns a list of all supported export formats.
// This includes both primary format names and their aliases.
//
// Returns:
// - A string slice containing all supported format names
func (f *Factory) GetSupportedFormats() []string {
func (f *Factory) SupportedFormats() []string {
return []string{"markdown", "md", "docx", "word", "html", "htm"}
}

View File

@ -125,7 +125,7 @@ func TestFactory_CreateExporter(t *testing.T) {
}
// Check supported format
supportedFormat := exporter.GetSupportedFormat()
supportedFormat := exporter.SupportedFormat()
if supportedFormat != tc.expectedFormat {
t.Errorf("Expected supported format '%s' for format '%s', got '%s'", tc.expectedFormat, tc.format, supportedFormat)
}
@ -173,7 +173,7 @@ func TestFactory_CreateExporter_CaseInsensitive(t *testing.T) {
t.Fatalf("CreateExporter returned nil for format '%s'", tc.format)
}
supportedFormat := exporter.GetSupportedFormat()
supportedFormat := exporter.SupportedFormat()
if supportedFormat != tc.expectedFormat {
t.Errorf("Expected supported format '%s' for format '%s', got '%s'", tc.expectedFormat, tc.format, supportedFormat)
}
@ -221,15 +221,15 @@ func TestFactory_CreateExporter_ErrorMessages(t *testing.T) {
}
}
// TestFactory_GetSupportedFormats tests the GetSupportedFormats method.
func TestFactory_GetSupportedFormats(t *testing.T) {
// TestFactory_SupportedFormats tests the SupportedFormats method.
func TestFactory_SupportedFormats(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner()
factory := NewFactory(htmlCleaner)
formats := factory.GetSupportedFormats()
formats := factory.SupportedFormats()
if formats == nil {
t.Fatal("GetSupportedFormats() returned nil")
t.Fatal("SupportedFormats() returned nil")
}
expected := []string{"markdown", "md", "docx", "word", "html", "htm"}
@ -246,22 +246,22 @@ func TestFactory_GetSupportedFormats(t *testing.T) {
for _, format := range formats {
exporter, err := factory.CreateExporter(format)
if err != nil {
t.Errorf("Format '%s' from GetSupportedFormats() should be creatable, got error: %v", format, err)
t.Errorf("Format '%s' from SupportedFormats() should be creatable, got error: %v", format, err)
}
if exporter == nil {
t.Errorf("Format '%s' from GetSupportedFormats() should create non-nil exporter", format)
t.Errorf("Format '%s' from SupportedFormats() should create non-nil exporter", format)
}
}
}
// TestFactory_GetSupportedFormats_Immutable tests that the returned slice is safe to modify.
func TestFactory_GetSupportedFormats_Immutable(t *testing.T) {
// TestFactory_SupportedFormats_Immutable tests that the returned slice is safe to modify.
func TestFactory_SupportedFormats_Immutable(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner()
factory := NewFactory(htmlCleaner)
// Get formats twice
formats1 := factory.GetSupportedFormats()
formats2 := factory.GetSupportedFormats()
formats1 := factory.SupportedFormats()
formats2 := factory.SupportedFormats()
// Modify first slice
if len(formats1) > 0 {
@ -270,13 +270,13 @@ func TestFactory_GetSupportedFormats_Immutable(t *testing.T) {
// Check that second call returns unmodified data
if len(formats2) > 0 && formats2[0] == "modified" {
t.Error("GetSupportedFormats() should return independent slices")
t.Error("SupportedFormats() should return independent slices")
}
// Verify original functionality still works
formats3 := factory.GetSupportedFormats()
formats3 := factory.SupportedFormats()
if len(formats3) == 0 {
t.Error("GetSupportedFormats() should still return formats after modification")
t.Error("SupportedFormats() should still return formats after modification")
}
}
@ -436,7 +436,7 @@ func TestFactory_FormatNormalization(t *testing.T) {
t.Fatalf("Failed to create exporter for '%s': %v", tc.input, err)
}
format := exporter.GetSupportedFormat()
format := exporter.SupportedFormat()
if format != tc.expected {
t.Errorf("Expected format '%s' for input '%s', got '%s'", tc.expected, tc.input, format)
}
@ -464,12 +464,12 @@ func BenchmarkFactory_CreateExporter_Docx(b *testing.B) {
}
}
// BenchmarkFactory_GetSupportedFormats benchmarks the GetSupportedFormats method.
func BenchmarkFactory_GetSupportedFormats(b *testing.B) {
// BenchmarkFactory_SupportedFormats benchmarks the SupportedFormats method.
func BenchmarkFactory_SupportedFormats(b *testing.B) {
htmlCleaner := services.NewHTMLCleaner()
factory := NewFactory(htmlCleaner)
for b.Loop() {
_ = factory.GetSupportedFormats()
_ = factory.SupportedFormats()
}
}

View File

@ -113,12 +113,12 @@ func (e *HTMLExporter) Export(course *models.Course, outputPath string) error {
return os.WriteFile(outputPath, buf.Bytes(), 0644)
}
// GetSupportedFormat returns the format name this exporter supports
// SupportedFormat returns the format name this exporter supports
// It indicates the file format that the HTMLExporter can generate.
//
// Returns:
// - A string representing the supported format ("html")
func (e *HTMLExporter) GetSupportedFormat() string {
func (e *HTMLExporter) SupportedFormat() string {
return "html"
}

View File

@ -32,13 +32,13 @@ func TestNewHTMLExporter(t *testing.T) {
}
}
// TestHTMLExporter_GetSupportedFormat tests the GetSupportedFormat method.
func TestHTMLExporter_GetSupportedFormat(t *testing.T) {
// TestHTMLExporter_SupportedFormat tests the SupportedFormat method.
func TestHTMLExporter_SupportedFormat(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner()
exporter := NewHTMLExporter(htmlCleaner)
expected := "html"
result := exporter.GetSupportedFormat()
result := exporter.SupportedFormat()
if result != expected {
t.Errorf("Expected format '%s', got '%s'", expected, result)

View File

@ -92,12 +92,12 @@ func (e *MarkdownExporter) Export(course *models.Course, outputPath string) erro
return os.WriteFile(outputPath, buf.Bytes(), 0644)
}
// GetSupportedFormat returns the format name this exporter supports
// SupportedFormat returns the format name this exporter supports
// It indicates the file format that the MarkdownExporter can generate.
//
// Returns:
// - A string representing the supported format ("markdown")
func (e *MarkdownExporter) GetSupportedFormat() string {
func (e *MarkdownExporter) SupportedFormat() string {
return "markdown"
}

View File

@ -32,13 +32,13 @@ func TestNewMarkdownExporter(t *testing.T) {
}
}
// TestMarkdownExporter_GetSupportedFormat tests the GetSupportedFormat method.
func TestMarkdownExporter_GetSupportedFormat(t *testing.T) {
// TestMarkdownExporter_SupportedFormat tests the SupportedFormat method.
func TestMarkdownExporter_SupportedFormat(t *testing.T) {
htmlCleaner := services.NewHTMLCleaner()
exporter := NewMarkdownExporter(htmlCleaner)
expected := "markdown"
result := exporter.GetSupportedFormat()
result := exporter.SupportedFormat()
if result != expected {
t.Errorf("Expected format '%s', got '%s'", expected, result)