mirror of
https://github.com/kjanat/articulate-parser.git
synced 2026-01-16 09:42:09 +01:00
refactor: Align with Go conventions and improve maintainability
Renames the `OriginalUrl` field to `OriginalURL` across media models to adhere to Go's common initialisms convention. The `json` tag is unchanged to maintain API compatibility. Introduces constants for exporter formats (e.g., `FormatMarkdown`, `FormatDocx`) to eliminate the use of magic strings, enhancing type safety and making the code easier to maintain. Additionally, this commit includes several minor code quality improvements: - Wraps file-writing errors in exporters to provide more context. - Removes redundant package-level comments from test files. - Applies various minor linting fixes throughout the codebase.
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
// Package exporters_test provides benchmarks for all exporters.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
|
||||
@ -69,7 +69,7 @@ func (e *DocxExporter) Export(course *models.Course, outputPath string) error {
|
||||
|
||||
// Ensure output directory exists and add .docx extension
|
||||
if !strings.HasSuffix(strings.ToLower(outputPath), ".docx") {
|
||||
outputPath = outputPath + ".docx"
|
||||
outputPath += ".docx"
|
||||
}
|
||||
|
||||
// Create the file
|
||||
@ -198,5 +198,5 @@ func (e *DocxExporter) exportSubItem(doc *docx.Docx, subItem *models.SubItem) {
|
||||
// Returns:
|
||||
// - A string representing the supported format ("docx")
|
||||
func (e *DocxExporter) SupportedFormat() string {
|
||||
return "docx"
|
||||
return FormatDocx
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// Package exporters_test provides tests for the docx exporter.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
@ -331,7 +330,7 @@ func TestDocxExporter_ComplexCourse(t *testing.T) {
|
||||
Caption: "<p>Watch this introductory video</p>",
|
||||
Media: &models.Media{
|
||||
Video: &models.VideoMedia{
|
||||
OriginalUrl: "https://example.com/intro.mp4",
|
||||
OriginalURL: "https://example.com/intro.mp4",
|
||||
Duration: 300,
|
||||
},
|
||||
},
|
||||
@ -359,7 +358,7 @@ func TestDocxExporter_ComplexCourse(t *testing.T) {
|
||||
Caption: "<p>Course overview diagram</p>",
|
||||
Media: &models.Media{
|
||||
Image: &models.ImageMedia{
|
||||
OriginalUrl: "https://example.com/overview.png",
|
||||
OriginalURL: "https://example.com/overview.png",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
// Package exporters provides implementations of the Exporter interface
|
||||
// for converting Articulate Rise courses into various file formats.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
@ -10,6 +8,13 @@ import (
|
||||
"github.com/kjanat/articulate-parser/internal/services"
|
||||
)
|
||||
|
||||
// Format constants for supported export formats.
|
||||
const (
|
||||
FormatMarkdown = "markdown"
|
||||
FormatDocx = "docx"
|
||||
FormatHTML = "html"
|
||||
)
|
||||
|
||||
// Factory implements the ExporterFactory interface.
|
||||
// It creates appropriate exporter instances based on the requested format.
|
||||
type Factory struct {
|
||||
@ -36,11 +41,11 @@ func NewFactory(htmlCleaner *services.HTMLCleaner) interfaces.ExporterFactory {
|
||||
// Format strings are case-insensitive (e.g., "markdown", "DOCX").
|
||||
func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) {
|
||||
switch strings.ToLower(format) {
|
||||
case "markdown", "md":
|
||||
case FormatMarkdown, "md":
|
||||
return NewMarkdownExporter(f.htmlCleaner), nil
|
||||
case "docx", "word":
|
||||
case FormatDocx, "word":
|
||||
return NewDocxExporter(f.htmlCleaner), nil
|
||||
case "html", "htm":
|
||||
case FormatHTML, "htm":
|
||||
return NewHTMLExporter(f.htmlCleaner), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported export format: %s", format)
|
||||
@ -50,5 +55,5 @@ func (f *Factory) CreateExporter(format string) (interfaces.Exporter, error) {
|
||||
// SupportedFormats returns a list of all supported export formats,
|
||||
// including both primary format names and their aliases.
|
||||
func (f *Factory) SupportedFormats() []string {
|
||||
return []string{"markdown", "md", "docx", "word", "html", "htm"}
|
||||
return []string{FormatMarkdown, "md", FormatDocx, "word", FormatHTML, "htm"}
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// Package exporters_test provides tests for the exporter factory.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
// Package exporters provides implementations of the Exporter interface
|
||||
// for converting Articulate Rise courses into various file formats.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
@ -112,7 +110,10 @@ func (e *HTMLExporter) Export(course *models.Course, outputPath string) error {
|
||||
buf.WriteString("</html>\n")
|
||||
|
||||
// #nosec G306 - 0644 is appropriate for export files that should be readable by others
|
||||
return os.WriteFile(outputPath, buf.Bytes(), 0o644)
|
||||
if err := os.WriteFile(outputPath, buf.Bytes(), 0o644); err != nil {
|
||||
return fmt.Errorf("failed to write HTML file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SupportedFormat returns the format name this exporter supports
|
||||
@ -121,7 +122,7 @@ func (e *HTMLExporter) Export(course *models.Course, outputPath string) error {
|
||||
// Returns:
|
||||
// - A string representing the supported format ("html")
|
||||
func (e *HTMLExporter) SupportedFormat() string {
|
||||
return "html"
|
||||
return FormatHTML
|
||||
}
|
||||
|
||||
// getDefaultCSS returns basic CSS styling for the HTML document.
|
||||
@ -390,7 +391,7 @@ func (e *HTMLExporter) processMultimediaItem(buf *bytes.Buffer, item models.Item
|
||||
if subItem.Media != nil {
|
||||
if subItem.Media.Video != nil {
|
||||
buf.WriteString(" <div class=\"media-info\">\n")
|
||||
fmt.Fprintf(buf, " <p><strong>Video:</strong> %s</p>\n", html.EscapeString(subItem.Media.Video.OriginalUrl))
|
||||
fmt.Fprintf(buf, " <p><strong>Video:</strong> %s</p>\n", html.EscapeString(subItem.Media.Video.OriginalURL))
|
||||
if subItem.Media.Video.Duration > 0 {
|
||||
fmt.Fprintf(buf, " <p><strong>Duration:</strong> %d seconds</p>\n", subItem.Media.Video.Duration)
|
||||
}
|
||||
@ -411,7 +412,7 @@ func (e *HTMLExporter) processImageItem(buf *bytes.Buffer, item models.Item) {
|
||||
for _, subItem := range item.Items {
|
||||
if subItem.Media != nil && subItem.Media.Image != nil {
|
||||
buf.WriteString(" <div class=\"media-info\">\n")
|
||||
fmt.Fprintf(buf, " <p><strong>Image:</strong> %s</p>\n", html.EscapeString(subItem.Media.Image.OriginalUrl))
|
||||
fmt.Fprintf(buf, " <p><strong>Image:</strong> %s</p>\n", html.EscapeString(subItem.Media.Image.OriginalURL))
|
||||
buf.WriteString(" </div>\n")
|
||||
}
|
||||
if subItem.Caption != "" {
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// Package exporters_test provides tests for the html exporter.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
@ -270,7 +269,7 @@ func TestHTMLExporter_ProcessMultimediaItem(t *testing.T) {
|
||||
Title: "<p>Video Title</p>",
|
||||
Media: &models.Media{
|
||||
Video: &models.VideoMedia{
|
||||
OriginalUrl: "https://example.com/video.mp4",
|
||||
OriginalURL: "https://example.com/video.mp4",
|
||||
Duration: 120,
|
||||
},
|
||||
},
|
||||
@ -315,7 +314,7 @@ func TestHTMLExporter_ProcessImageItem(t *testing.T) {
|
||||
{
|
||||
Media: &models.Media{
|
||||
Image: &models.ImageMedia{
|
||||
OriginalUrl: "https://example.com/image.png",
|
||||
OriginalURL: "https://example.com/image.png",
|
||||
},
|
||||
},
|
||||
Caption: "<p>Image caption</p>",
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
// Package exporters provides implementations of the Exporter interface
|
||||
// for converting Articulate Rise courses into various file formats.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
@ -82,12 +80,15 @@ func (e *MarkdownExporter) Export(course *models.Course, outputPath string) erro
|
||||
}
|
||||
|
||||
// #nosec G306 - 0644 is appropriate for export files that should be readable by others
|
||||
return os.WriteFile(outputPath, buf.Bytes(), 0o644)
|
||||
if err := os.WriteFile(outputPath, buf.Bytes(), 0o644); err != nil {
|
||||
return fmt.Errorf("failed to write markdown file: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SupportedFormat returns "markdown".
|
||||
func (e *MarkdownExporter) SupportedFormat() string {
|
||||
return "markdown"
|
||||
return FormatMarkdown
|
||||
}
|
||||
|
||||
// processItemToMarkdown converts a course item into Markdown format.
|
||||
@ -170,7 +171,7 @@ func (e *MarkdownExporter) processMediaSubItem(buf *bytes.Buffer, subItem models
|
||||
// processVideoMedia processes video media content.
|
||||
func (e *MarkdownExporter) processVideoMedia(buf *bytes.Buffer, media *models.Media) {
|
||||
if media.Video != nil {
|
||||
fmt.Fprintf(buf, "**Video**: %s\n", media.Video.OriginalUrl)
|
||||
fmt.Fprintf(buf, "**Video**: %s\n", media.Video.OriginalURL)
|
||||
if media.Video.Duration > 0 {
|
||||
fmt.Fprintf(buf, "**Duration**: %d seconds\n", media.Video.Duration)
|
||||
}
|
||||
@ -180,7 +181,7 @@ func (e *MarkdownExporter) processVideoMedia(buf *bytes.Buffer, media *models.Me
|
||||
// processImageMedia processes image media content.
|
||||
func (e *MarkdownExporter) processImageMedia(buf *bytes.Buffer, media *models.Media) {
|
||||
if media.Image != nil {
|
||||
fmt.Fprintf(buf, "**Image**: %s\n", media.Image.OriginalUrl)
|
||||
fmt.Fprintf(buf, "**Image**: %s\n", media.Image.OriginalURL)
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,7 +190,7 @@ func (e *MarkdownExporter) processImageItem(buf *bytes.Buffer, item models.Item,
|
||||
fmt.Fprintf(buf, "%s Image\n\n", headingPrefix)
|
||||
for _, subItem := range item.Items {
|
||||
if subItem.Media != nil && subItem.Media.Image != nil {
|
||||
fmt.Fprintf(buf, "**Image**: %s\n", subItem.Media.Image.OriginalUrl)
|
||||
fmt.Fprintf(buf, "**Image**: %s\n", subItem.Media.Image.OriginalURL)
|
||||
}
|
||||
if subItem.Caption != "" {
|
||||
caption := e.htmlCleaner.CleanHTML(subItem.Caption)
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// Package exporters_test provides tests for the markdown exporter.
|
||||
package exporters
|
||||
|
||||
import (
|
||||
@ -188,7 +187,7 @@ func TestMarkdownExporter_ProcessMultimediaItem(t *testing.T) {
|
||||
{
|
||||
Media: &models.Media{
|
||||
Video: &models.VideoMedia{
|
||||
OriginalUrl: "https://example.com/video.mp4",
|
||||
OriginalURL: "https://example.com/video.mp4",
|
||||
Duration: 120,
|
||||
},
|
||||
},
|
||||
@ -227,7 +226,7 @@ func TestMarkdownExporter_ProcessImageItem(t *testing.T) {
|
||||
{
|
||||
Media: &models.Media{
|
||||
Image: &models.ImageMedia{
|
||||
OriginalUrl: "https://example.com/image.jpg",
|
||||
OriginalURL: "https://example.com/image.jpg",
|
||||
},
|
||||
},
|
||||
Caption: "<p>Image caption</p>",
|
||||
@ -372,7 +371,7 @@ func TestMarkdownExporter_ProcessVideoMedia(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
media := &models.Media{
|
||||
Video: &models.VideoMedia{
|
||||
OriginalUrl: "https://example.com/video.mp4",
|
||||
OriginalURL: "https://example.com/video.mp4",
|
||||
Duration: 300,
|
||||
},
|
||||
}
|
||||
@ -397,7 +396,7 @@ func TestMarkdownExporter_ProcessImageMedia(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
media := &models.Media{
|
||||
Image: &models.ImageMedia{
|
||||
OriginalUrl: "https://example.com/image.jpg",
|
||||
OriginalURL: "https://example.com/image.jpg",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user