diff --git a/README.md b/README.md index c1e59ef..2b00415 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,12 @@ ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/jlengrand/OpenGraphKt) -[OpenGraphKt](https://github.com/jlengrand/OpenGraphKt) is a minimalist Kotlin library to work with the [Open Graph tags](https://ogp.me/) protocol. +[OpenGraphKt](https://github.com/jlengrand/OpenGraphKt) is a minimalist Kotlin multiplatform library to work with the [Open Graph tags](https://ogp.me/) protocol. OpenGraphKt is a tiny wrapper on top of JSoup. ## Current status * Library can extract OpenGraph tags from HTML via a `URL`, `String` or `File` input. -* Current implementation is JVM only, due to the `JSoup` dependency. * Protocol implementation is complete for `og:` tags, but types aren't fully correct (most types currently are `String`). * Library should be considered in pre-alpha, use this in production at your own risks :). @@ -28,7 +27,7 @@ In short : * Add dependency to your Maven / Gradle file. For example : ```bash - implementation("fr.lengrand:opengraphkt:0.0.2") + implementation("fr.lengrand:opengraphkt:0.1.0") ``` * Enjoy: @@ -44,11 +43,6 @@ println("Is valid: ${openGraphDataDoc.isValid()}") // Is valid: true ``` - -## Dependencies - -- [JSoup](https://jsoup.org/) - ## Author * [Julien Lengrand-Lambert](https://github.com/jlengrand) diff --git a/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/Generator.kt b/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/Generator.kt new file mode 100644 index 0000000..858f811 --- /dev/null +++ b/opengraphkt/src/main/kotlin/fr/lengrand/opengraphkt/Generator.kt @@ -0,0 +1,337 @@ +package fr.lengrand.opengraphkt + +import java.time.OffsetDateTime + +/** + * A generator for Open Graph protocol HTML meta tags. + * + * This class converts an OpenGraph Data object into HTML meta tags according to the Open Graph protocol specification. + * It can be used to generate the appropriate meta tags for embedding in HTML documents. + * + * @see Open Graph Protocol + */ +class Generator { + + /** + * Generates HTML meta tags from an OpenGraph Data object. + * + * @param data The OpenGraph Data object to convert to HTML meta tags + * @return A string containing the HTML meta tags + */ + fun generate(data: Data): String { + val tags = mutableListOf() + + // Add basic metadata tags + addBasicMetaTags(data, tags) + + // Add image tags + addImageTags(data.images, tags) + + // Add video tags + addVideoTags(data.videos, tags) + + // Add audio tags + addAudioTags(data.audios, tags) + + // Add type-specific tags + when (data.getType()) { + Type.ARTICLE -> addArticleTags(data.article, tags) + Type.PROFILE -> addProfileTags(data.profile, tags) + Type.BOOK -> addBookTags(data.book, tags) + Type.MUSIC_SONG -> addMusicSongTags(data.musicSong, tags) + Type.MUSIC_ALBUM -> addMusicAlbumTags(data.musicAlbum, tags) + Type.MUSIC_PLAYLIST -> addMusicPlaylistTags(data.musicPlaylist, tags) + Type.MUSIC_RADIO_STATION -> addMusicRadioStationTags(data.musicRadioStation, tags) + Type.VIDEO_MOVIE, Type.VIDEO_TV_SHOW, Type.VIDEO_OTHER -> addVideoMovieTags(data.videoMovie, tags) + Type.VIDEO_EPISODE -> addVideoEpisodeTags(data.videoEpisode, tags) + else -> { /* No additional tags for other types */ } + } + + return tags.joinToString("\n") + } + + /** + * Adds basic Open Graph meta tags to the list. + * + * @param data The OpenGraph Data object + * @param tags The list to add the tags to + */ + private fun addBasicMetaTags(data: Data, tags: MutableList) { + // Required properties + data.title?.let { tags.add(createMetaTag("og:title", it)) } + data.type?.let { tags.add(createMetaTag("og:type", it)) } + data.url?.let { tags.add(createMetaTag("og:url", it.toString())) } + + // Optional properties + data.description?.let { tags.add(createMetaTag("og:description", it)) } + data.siteName?.let { tags.add(createMetaTag("og:site_name", it)) } + data.determiner?.let { tags.add(createMetaTag("og:determiner", it)) } + data.locale?.let { tags.add(createMetaTag("og:locale", it)) } + + // Locale alternates + data.localeAlternate.forEach { locale -> + tags.add(createMetaTag("og:locale:alternate", locale)) + } + } + + /** + * Adds image meta tags to the list. + * + * @param images The list of Image objects + * @param tags The list to add the tags to + */ + private fun addImageTags(images: List, tags: MutableList) { + images.forEach { image -> + image.url?.let { tags.add(createMetaTag("og:image", it)) } + image.secureUrl?.let { tags.add(createMetaTag("og:image:secure_url", it)) } + image.type?.let { tags.add(createMetaTag("og:image:type", it)) } + image.width?.let { tags.add(createMetaTag("og:image:width", it.toString())) } + image.height?.let { tags.add(createMetaTag("og:image:height", it.toString())) } + image.alt?.let { tags.add(createMetaTag("og:image:alt", it)) } + } + } + + /** + * Adds video meta tags to the list. + * + * @param videos The list of Video objects + * @param tags The list to add the tags to + */ + private fun addVideoTags(videos: List