diff --git a/src/main/kotlin/nl/lengrand/opengraphkt/Main.kt b/src/main/kotlin/nl/lengrand/opengraphkt/Main.kt
index 81835a9..8d60847 100644
--- a/src/main/kotlin/nl/lengrand/opengraphkt/Main.kt
+++ b/src/main/kotlin/nl/lengrand/opengraphkt/Main.kt
@@ -16,70 +16,28 @@ fun main() {
val openGraphData = parser.parse(document)
println("Title: ${openGraphData.title}")
- println("Type: ${openGraphData.type}")
- println("URL: ${openGraphData.url}")
- println("Description: ${openGraphData.description}")
- println("Site Name: ${openGraphData.siteName}")
-
- println("Images: ${openGraphData.images.size}")
- openGraphData.images.forEachIndexed { index, image ->
- println("Image ${index + 1}: ${image.url}")
- println(" Width: ${image.width}")
- println(" Height: ${image.height}")
- println(" Alt: ${image.alt}")
- }
-
println("Is valid: ${openGraphData.isValid()}")
} catch (e: Exception) {
println("Error parsing URL: ${e.message}")
}
- // Example 1.5: Parse Open Graph data from a file
- println("\nExample 1.5: Parsing from File")
+ // Example 2: Parse Open Graph data from a file
+ println("\nExample 2: Parsing from File")
try {
- // This is just an example. In a real application, you would use an actual HTML file.
- val tempFile = File.createTempFile("example", ".html")
- tempFile.deleteOnExit() // Clean up after ourselves
-
- // Write some sample HTML to the file
- val sampleHtml = """
-
-
-
- File Example
-
-
-
-
-
-
-
- File Example
-
-
- """.trimIndent()
- tempFile.writeText(sampleHtml)
+ val resourceUrl = object {}.javaClass.getResource("/example.html")
+ val resourceFile = File(resourceUrl.toURI())
// Parse the file
- val document = fetcher.fromFile(tempFile)
+ val document = fetcher.fromFile(resourceFile)
val openGraphData = parser.parse(document)
println("Title: ${openGraphData.title}")
- println("Type: ${openGraphData.type}")
- println("URL: ${openGraphData.url}")
- println("Description: ${openGraphData.description}")
-
- println("Images: ${openGraphData.images.size}")
- openGraphData.images.forEachIndexed { index, image ->
- println("Image ${index + 1}: ${image.url}")
- }
-
println("Is valid: ${openGraphData.isValid()}")
} catch (e: Exception) {
println("Error parsing file: ${e.message}")
}
- // Example 2: Parse Open Graph data from an HTML string
+ // Example 3: Parse Open Graph data from an HTML string
println("\nExample 2: Parsing from HTML string")
val html = """
@@ -105,95 +63,5 @@ fun main() {
val openGraphData = parser.parse(document)
println("Title: ${openGraphData.title}")
- println("Type: ${openGraphData.type}")
- println("URL: ${openGraphData.url}")
- println("Description: ${openGraphData.description}")
- println("Site Name: ${openGraphData.siteName}")
-
- println("Images: ${openGraphData.images.size}")
- openGraphData.images.forEachIndexed { index, image ->
- println("Image ${index + 1}: ${image.url}")
- println(" Width: ${image.width}")
- println(" Height: ${image.height}")
- }
-
println("Is valid: ${openGraphData.isValid()}")
-
- // Example 3: Working with multiple images
- println("\nExample 3: Working with multiple images")
- val multipleImagesHtml = """
-
-
-
- Multiple Images Example
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Photo Gallery
-
-
- """.trimIndent()
-
- val multipleImagesDocument = fetcher.fromString(multipleImagesHtml)
- val multipleImagesData = parser.parse(multipleImagesDocument)
-
- println("Title: ${multipleImagesData.title}")
- println("Images: ${multipleImagesData.images.size}")
- multipleImagesData.images.forEachIndexed { index, image ->
- println("Image ${index + 1}: ${image.url}")
- println(" Width: ${image.width}")
- println(" Height: ${image.height}")
- }
-
- // Example 4: Working with article metadata
- println("\nExample 4: Working with article metadata")
- val articleHtml = """
-
-
-
- Article Example
-
-
-
-
-
-
-
-
-
-
-
-
-
- Breaking News
-
-
- """.trimIndent()
-
- val articleDocument = fetcher.fromString(articleHtml)
- val articleData = parser.parse(articleDocument)
-
- println("Title: ${articleData.title}")
- println("Type: ${articleData.type}")
-
- val article = articleData.article
- if (article != null) {
- println("Published Time: ${article.publishedTime}")
- println("Modified Time: ${article.modifiedTime}")
- println("Section: ${article.section}")
- println("Authors: ${article.authors.joinToString(", ")}")
- println("Tags: ${article.tags.joinToString(", ")}")
- }
}
diff --git a/src/main/resources/example.html b/src/main/resources/example.html
new file mode 100644
index 0000000..733d958
--- /dev/null
+++ b/src/main/resources/example.html
@@ -0,0 +1,14 @@
+
+
+
+ File Example
+
+
+
+
+
+
+
+ File Example
+
+
\ No newline at end of file