├── .github └── workflows │ ├── build.yaml │ └── check.yaml ├── .gitignore ├── .idea ├── misc.xml └── vcs.xml ├── CHANGELOG.md ├── LICENCE ├── README.md ├── build.gradle.kts ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main ├── kotlin │ └── io │ │ └── data2viz │ │ └── kotlinx │ │ └── htmlplugin │ │ ├── conversion │ │ ├── HtmlDataToHtmlKotlinx.kt │ │ ├── HtmlModel.kt │ │ └── HtmlPsiToHtmlDataConverter.kt │ │ └── ide │ │ ├── ConvertHTMLToKotlinxAction.kt │ │ ├── ConvertTextHTMLCopyPasteProcessor.kt │ │ ├── HtmlTextTransferableData.kt │ │ └── KotlinPasteFromHtmlDialog.kt └── resources │ └── META-INF │ ├── export.svg │ ├── plugin.xml │ └── pluginIcon_dark.svg └── test ├── java └── com │ └── intellij │ └── testFramework │ └── LightPlatform4TestCase.java ├── kotlin └── io │ └── data2viz │ └── kotlinx │ └── htmlplugin │ ├── HtmlDataToHtmlKotlinXTest.kt │ ├── HtmlTextToHtmlDataTest.kt │ ├── HtmlTextToKotlinXTextTest.kt │ └── ResourcesTest.kt └── resources ├── attrs_a.html ├── attrs_a.htmlkotlinx ├── attrs_base.html ├── attrs_base.htmlkotlinx ├── attrs_class.html ├── attrs_class.htmlkotlinx ├── attrs_custom.html ├── attrs_custom.htmlkotlinx ├── attrs_img.html ├── attrs_img.htmlkotlinx ├── base.html ├── base.htmlkotlinx ├── htmlFor.html ├── htmlFor.htmlkotlinx ├── inline.html ├── inline.htmlkotlinx ├── nested.html ├── nested.htmlkotlinx ├── nested_several_text_childs.html ├── nested_several_text_childs.htmlkotlinx ├── textarea.html ├── textarea.htmlkotlinx ├── uppercase.html └── uppercase.htmlkotlinx /.github/workflows/build.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | name: checks & builds the plugin 3 | 4 | permissions: 5 | contents: write 6 | checks: write 7 | id-token: write 8 | 9 | on: 10 | push: 11 | branches: 12 | - 'master' 13 | paths-ignore: 14 | - 'doc/**' 15 | - '*.md' 16 | - '*.adoc' 17 | 18 | jobs: 19 | build: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - name: Checkout project sources 23 | uses: actions/checkout@v4 24 | 25 | - name: Setup Java 26 | uses: actions/setup-java@v4 27 | with: 28 | distribution: corretto 29 | java-version: 21 30 | 31 | - name: Setup Gradle 32 | uses: gradle/actions/setup-gradle@v4 33 | 34 | - name: Run tests 35 | run: ./gradlew check 36 | 37 | - name: Create Test Report 38 | uses: mikepenz/action-junit-report@v4 39 | if: success() || failure() # always run even if the previous step fails 40 | with: 41 | check_name: Test Report 42 | report_paths: '**/build/test-results/test/TEST-*.xml' 43 | detailed_summary: true 44 | include_passed: true 45 | 46 | - name: Build Plugin 47 | run: ./gradlew buildPlugin 48 | 49 | - name: read pluginVersion from build.gradle 50 | uses: christian-draeger/read-properties@1.1.1 51 | id: read_plugin_version 52 | with: 53 | path: './gradle.properties' 54 | properties: 'pluginVersion' 55 | 56 | - name: Upload Plugin Artefact 57 | uses: actions/upload-artifact@v4 58 | with: 59 | name: distribution 60 | path: './build/distributions/kotlinx.html-plugin-${{ steps.read_plugin_version.outputs.pluginVersion }}.zip' 61 | 62 | deploy: 63 | runs-on: ubuntu-latest 64 | concurrency: 65 | group: deployment-to-jetbrains-marketplace 66 | cancel-in-progress: true 67 | environment: JETBRAINS_MARKETPLACE 68 | env: 69 | PUBLISH_TOKEN: ${{ secrets.JETBRAINS_MARKETPLACE_TOKEN }} 70 | needs: build 71 | steps: 72 | - name: Checkout project sources 73 | uses: actions/checkout@v4 74 | 75 | - name: Download Plugin Artifact 76 | uses: actions/download-artifact@v4 77 | with: 78 | name: distribution 79 | path: . 80 | 81 | - name: Setup Java 82 | uses: actions/setup-java@v4 83 | with: 84 | distribution: corretto 85 | java-version: 21 86 | 87 | - name: Setup Gradle 88 | uses: gradle/actions/setup-gradle@v4 89 | 90 | - name: deploy to Jetbrains marketplace without rebuilding zip file 91 | run: ./gradlew --info -DPUBLISH_TOKEN=${{ env.PUBLISH_TOKEN }} :publishPlugin --exclude-task :signPlugin --exclude-task :buildPlugin --exclude-task :check --exclude-task :patchChangelog 92 | 93 | - name: read pluginVersion from build.gradle 94 | uses: christian-draeger/read-properties@1.1.1 95 | id: read_plugin_version 96 | with: 97 | path: './gradle.properties' 98 | properties: 'pluginVersion' 99 | 100 | - name: Create tag 101 | uses: actions/github-script@v7 102 | with: 103 | script: | 104 | github.rest.git.createRef({ 105 | owner: context.repo.owner, 106 | repo: context.repo.repo, 107 | ref: 'refs/tags/v${{ steps.read_plugin_version.outputs.pluginVersion }}', 108 | sha: context.sha 109 | }) 110 | 111 | 112 | -------------------------------------------------------------------------------- /.github/workflows/check.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | name: checks the plugin 3 | 4 | permissions: 5 | contents: read 6 | checks: write 7 | id-token: write 8 | 9 | on: 10 | push: 11 | branches-ignore: 12 | - 'master' 13 | paths-ignore: 14 | - 'doc/**' 15 | - '*.md' 16 | - '*.adoc' 17 | 18 | jobs: 19 | build-gradle-project: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - name: Checkout project sources 23 | uses: actions/checkout@v4 24 | 25 | - name: Setup Java 26 | uses: actions/setup-java@v4 27 | with: 28 | distribution: corretto 29 | java-version: 21 30 | 31 | - name: Setup Gradle 32 | uses: gradle/actions/setup-gradle@v4 33 | 34 | - name: Run tests 35 | run: ./gradlew check 36 | 37 | - name: Create Test Report 38 | uses: mikepenz/action-junit-report@v4 39 | if: success() || failure() # always run even if the previous step fails 40 | with: 41 | check_name: Test Report 42 | report_paths: '**/build/test-results/test/TEST-*.xml' 43 | detailed_summary: true 44 | include_passed: true 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea/shelf 3 | /android.tests.dependencies 4 | /confluence/target 5 | /dependencies/repo 6 | /dist 7 | /local 8 | /gh-pages 9 | /ideaSDK 10 | /clionSDK 11 | /android-studio/sdk 12 | out/ 13 | /tmp 14 | workspace.xml 15 | *.versionsBackup 16 | /idea/testData/debugger/tinyApp/classes* 17 | /jps-plugin/testData/kannotator 18 | /js/js.translator/testData/out/ 19 | /js/js.translator/testData/out-min/ 20 | .gradle/ 21 | build/ 22 | !**/src/**/build 23 | !**/test/**/build 24 | *.iml 25 | !**/testData/**/*.iml 26 | .idea/libraries/Gradle*.xml 27 | .idea/libraries/Maven*.xml 28 | .idea/artifacts/PILL_*.xml 29 | .idea/modules 30 | .idea/runConfigurations/JPS_*.xml 31 | .idea/runConfigurations/PILL_*.xml 32 | .idea/libraries 33 | .idea/modules.xml 34 | .idea/gradle.xml 35 | .idea/compiler.xml 36 | .idea/inspectionProfiles/profiles_settings.xml 37 | .idea/.name 38 | .idea/artifacts/dist_auto_* 39 | kotlin-ultimate/ 40 | node_modules/ 41 | .rpt2_cache/ 42 | libraries/tools/kotlin-test-nodejs-runner/lib/ 43 | local.properties 44 | uiDesigner.xml 45 | 46 | .kotlin/ 47 | .intellijPlatform/ 48 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Changelog kotlinx.html-plugin 4 | 5 | ## [Unreleased] 6 | 7 | ### Added 8 | 9 | ### Changed 10 | 11 | ### Deprecated 12 | 13 | ### Removed 14 | 15 | ### Fixed 16 | 17 | ### Security 18 | 19 | ## [1.1.2] - 2025-01-17 20 | 21 | ### Added 22 | 23 | - chore: added build output for debugging purposes 24 | - chore: added write permission for tagging in the repository 25 | 26 | ## [1.1.1] - 2025-01-17 27 | 28 | ### Added 29 | 30 | - chore: automated deployment to the JetBrains marketplace 31 | - chore: protect JETBRAINS_MARKETPLACE environment (see Github/Settings/Environments) with manual review 32 | - chore: add `libs.versions.toml` for dependency management 33 | - chore: read plugin description from `README.md` 34 | - chore: read version information from `CHANGELOG.md` 35 | - chore: tag version in git after successful release 36 | 37 | ### Changed 38 | 39 | - unset `until-build` for upward compatibility 40 | 41 | ## [1.1.0] - 2025-01-15 42 | 43 | ### Fixed 44 | 45 | - Fix issue #22: K2 compatibility issue 46 | 47 | ## [1.0.7] - 2020-05-28 48 | 49 | ### Fixed 50 | 51 | - Fix #20: "classes" should be treated as a special parameter 52 | - Fix #16: label.for attribute should be converted in htmlFor 53 | - Fix #18: Parse error on windows and IntelliJ 2019.2 54 | 55 | ## [1.0.6] - 2020-05-26 56 | 57 | ## [1.0.5] - 2020-05-26 58 | 59 | ## [1.0.4] - 2019-06-10 60 | 61 | ### Fixed 62 | 63 | - Fix #14: class names should be added to tag constructor 64 | 65 | ## [1.0.3] - 2019-06-10 66 | 67 | ### Fixed 68 | 69 | - Fix #10: split class names 70 | - Fix #9: textArea instead of textarea 71 | 72 | ## [1.0.2] - 2019-04-26 73 | 74 | ### Fixed 75 | 76 | - Fix #7: attributes should be converted into code inside the block. 77 | - Fix #6: the plugin shouldn't apply for Kotlin code with generics. 78 | 79 | ## [1.0.1] - 2019-04-05 80 | 81 | ### Changed 82 | 83 | - Align Kotlin version on Intellij 2018 84 | 85 | [Unreleased]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.1.2...HEAD 86 | [1.1.2]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.1.1...v1.1.2 87 | [1.1.1]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.1.0...v1.1.1 88 | [1.1.0]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.7...v1.1.0 89 | [1.0.7]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.6...v1.0.7 90 | [1.0.6]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.5...v1.0.6 91 | [1.0.5]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.4...v1.0.5 92 | [1.0.4]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.3...v1.0.4 93 | [1.0.3]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.2...v1.0.3 94 | [1.0.2]: https://github.com/data2viz/kotlinx.html-plugin/compare/v1.0.1...v1.0.2 95 | [1.0.1]: https://github.com/data2viz/kotlinx.html-plugin/commits/v1.0.1 96 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kotlinx.html-plugin 2 | 3 | 4 | An Intellij plugin to copy/paste from HTML to kotlinx.html. 5 | 6 | This plugin simplifies the transformation of HTML code to a kotlinx.html project 7 | by automatically doing the conversion during a copy/paste. 8 | 9 | 10 | See https://plugins.jetbrains.com/plugin/12205-html-to-kotlinx-html to install it. 11 | -------------------------------------------------------------------------------- /build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.changelog.Changelog 2 | import org.jetbrains.changelog.markdownToHTML 3 | import org.jetbrains.intellij.platform.gradle.TestFrameworkType 4 | 5 | plugins { 6 | id("java") 7 | alias(libs.plugins.intelliJPlatform) 8 | alias(libs.plugins.kotlin) 9 | alias(libs.plugins.changelog) 10 | } 11 | 12 | version = providers.gradleProperty("pluginVersion").get() 13 | 14 | repositories { 15 | mavenCentral() 16 | 17 | intellijPlatform { 18 | defaultRepositories() 19 | } 20 | } 21 | 22 | dependencies { 23 | testImplementation(libs.junit) 24 | 25 | intellijPlatform { 26 | create(providers.gradleProperty("platformType"), providers.gradleProperty("platformVersion")) 27 | 28 | 29 | pluginVerifier() 30 | testFramework(TestFrameworkType.Platform) 31 | } 32 | } 33 | 34 | intellijPlatform { 35 | pluginConfiguration { 36 | name = providers.gradleProperty("pluginName") 37 | group = providers.gradleProperty("pluginGroup") 38 | version = providers.gradleProperty("pluginVersion") 39 | 40 | // Extract the section from README.md and provide for the plugin's manifest 41 | description = providers.fileContents(layout.projectDirectory.file("README.md")).asText.map { 42 | val start = "" 43 | val end = "" 44 | 45 | with(it.lines()) { 46 | if (!containsAll(listOf(start, end))) { 47 | throw GradleException("Plugin description section not found in README.md:\n$start ... $end") 48 | } 49 | subList(indexOf(start) + 1, indexOf(end)).joinToString("\n").let(::markdownToHTML) 50 | } 51 | } 52 | 53 | val changelog = project.changelog // local variable for configuration cache compatibility 54 | // Get the latest available change notes from the changelog file 55 | changeNotes = providers.gradleProperty("pluginVersion").map { pluginVersion -> 56 | with(changelog) { 57 | renderItem( 58 | (getOrNull(pluginVersion) ?: getUnreleased()) 59 | .withHeader(false) 60 | .withEmptySections(false), 61 | Changelog.OutputType.HTML, 62 | ) 63 | } 64 | } 65 | 66 | ideaVersion { 67 | sinceBuild = providers.gradleProperty("pluginSinceBuild") 68 | // untilBuild = providers.gradleProperty("pluginUntilBuild") 69 | untilBuild = provider { null } // unset for the latest IDE version, see: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-p lugin-extension.html#intellijPlatform-pluginConfiguration-ideaVersion-untilBuild 70 | } 71 | 72 | } 73 | 74 | pluginVerification { 75 | ides { 76 | recommended() 77 | } 78 | } 79 | 80 | } 81 | 82 | java { 83 | sourceCompatibility = JavaVersion.VERSION_21 84 | targetCompatibility = JavaVersion.VERSION_21 85 | } 86 | 87 | kotlin { 88 | jvmToolchain(21) 89 | } 90 | 91 | // Configure Gradle Changelog Plugin - read more: https://github.com/JetBrains/gradle-changelog-plugin 92 | changelog { 93 | path.set(file("CHANGELOG.md").canonicalPath) 94 | groups.set(listOf("Added", "Changed", "Deprecated", "Removed", "Fixed", "Security")) 95 | repositoryUrl.set(providers.gradleProperty("pluginRepositoryUrl")) 96 | } 97 | 98 | tasks { 99 | test { 100 | useJUnit() 101 | } 102 | 103 | buildPlugin { 104 | dependsOn(patchChangelog) 105 | } 106 | 107 | publishPlugin { 108 | // use the version from the first build in .github/workflows/build.yml 109 | val tmpArchiveFile = layout.projectDirectory.file("${project.name}-${project.version}.zip") 110 | val tmpChannel = providers.gradleProperty("pluginVersion").map { listOf(it.substringAfter('-', "").substringBefore('.').ifEmpty { "default" }) } 111 | 112 | doFirst { 113 | println ("archive-file : ${tmpArchiveFile}") 114 | println ("archive-file-exists: ${tmpArchiveFile.asFile.exists()}") 115 | println ("channel : $tmpChannel") 116 | } 117 | 118 | // set by .github/workflows/build.yml 119 | token = providers.systemProperty("PUBLISH_TOKEN") 120 | // The pluginVersion is based on the SemVer (https://semver.org) and supports pre-release labels, like 2.1.7-alpha.3 121 | // Specify pre-release label to publish the plugin in a custom Release Channel automatically. Read more: 122 | // https://plugins.jetbrains.com/docs/intellij/deployment.html#specifying-a-release-channel 123 | channels = tmpChannel 124 | 125 | archiveFile = tmpArchiveFile 126 | } 127 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # For local, developer specific configurations create a `local.properties` file, 2 | # it will be used by gradle if present 3 | 4 | # SemVer format -> https://semver.org 5 | pluginVersion = 1.1.2 6 | pluginGroup = io.data2viz 7 | pluginName = HTML to kotlinx.html 8 | pluginRepositoryUrl = https://github.com/data2viz/kotlinx.html-plugin 9 | 10 | # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html 11 | pluginSinceBuild = 243 12 | # pluginUntilBuild = 243.* # this is commented out because we want to support all versions 13 | 14 | # IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension 15 | platformType = IC 16 | platformVersion = 2024.3.1.1 17 | 18 | # Enable Gradle Build Cache -> https://docs.gradle.org/current/userguide/build_cache.html 19 | org.gradle.caching = true 20 | 21 | # we have to add the following property so that we do not add the 22 | # kotlin stdlib dependency by default 23 | kotlin.stdlib.default.dependency = false 24 | 25 | -------------------------------------------------------------------------------- /gradle/libs.versions.toml: -------------------------------------------------------------------------------- 1 | [versions] 2 | # libraries 3 | junit = "4.13.2" 4 | 5 | # plugins 6 | changelog = "2.2.1" 7 | intelliJPlatform = "2.2.1" 8 | kotlin = "2.0.20" 9 | 10 | [libraries] 11 | junit = { group = "junit", name = "junit", version.ref = "junit" } 12 | 13 | [plugins] 14 | changelog = { id = "org.jetbrains.changelog", version.ref = "changelog" } 15 | intelliJPlatform = { id = "org.jetbrains.intellij.platform", version.ref = "intelliJPlatform" } 16 | kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } 17 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/data2viz/kotlinx.html-plugin/a2f00ccadfe5b855ba6955c4f23633a99a580f16/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 09 17:40:27 CEST 2024 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'kotlinx.html-plugin' 2 | 3 | -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/conversion/HtmlDataToHtmlKotlinx.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.conversion 2 | 3 | 4 | const val INDENT = " " // 4 spaces 5 | 6 | fun StringBuilder.addTabIndent(currentIndent: Int) = repeat(currentIndent) { append(INDENT) } 7 | 8 | fun HtmlElement.toKotlinx(currentIndent: Int = 0): String = 9 | when (this) { 10 | is HtmlTag -> toKotlinx(currentIndent) 11 | is HtmlText -> toKotlinxText(currentIndent) 12 | else -> error("${this.javaClass.typeName} not supported") 13 | } 14 | 15 | /** 16 | * Tags with only one text child is inline 17 | */ 18 | fun HtmlTag.isInline(): Boolean = 19 | children.size == 1 && 20 | children[0] is HtmlText && 21 | attributes.filterBodyAttributes().isEmpty() 22 | 23 | /** 24 | * Custom attributes, which not supported by KotlinX as fields, for example data-* or aria-* 25 | */ 26 | fun HtmlAttribute.isCustomForTag(tag: HtmlTag): Boolean { 27 | var custom = attrName.contains("-") 28 | 29 | if (!custom) { 30 | when (tag.tagName) { 31 | "link" -> { 32 | custom = (attrName == "integrity") or (attrName == "crossorigin") 33 | } 34 | } 35 | } 36 | 37 | return custom 38 | } 39 | 40 | fun Collection.filterConstructorAttributes(): List = 41 | filter { it.isConstructorAttribute() } 42 | 43 | fun Collection.filterBodyAttributes(): List = 44 | filter { !it.isConstructorAttribute() } 45 | 46 | 47 | fun HtmlAttribute.isConstructorAttribute(): Boolean = when (attrName) { 48 | "class" -> true 49 | else -> false 50 | } 51 | 52 | 53 | fun HtmlTag.toKotlinx(currentIndent: Int = 0): String { 54 | val inline = isInline() 55 | val sb = StringBuilder() 56 | 57 | sb.addTabIndent(currentIndent) 58 | val tagNameLowerCase = tagName.lowercase() 59 | 60 | val kotlinXTagName = when (tagNameLowerCase) { 61 | "textarea" -> "textArea" 62 | else -> tagNameLowerCase 63 | } 64 | 65 | sb.append(kotlinXTagName) 66 | 67 | 68 | val constructorAttributes = attributes.filterConstructorAttributes() 69 | 70 | if (constructorAttributes.isNotEmpty()) { 71 | sb.append("(") 72 | 73 | 74 | if (tagNameLowerCase in tagsWithOnlyClasses 75 | && constructorAttributes.size == 1 76 | && constructorAttributes[0].attrName.lowercase() == "class") { 77 | sb.append(""""${constructorAttributes[0].value}"""") 78 | 79 | } else { 80 | sb.append(constructorAttributes.joinToString(", ") { attribute -> 81 | attribute.toKotlinx(this) 82 | }) 83 | } 84 | 85 | 86 | sb.append(")") 87 | } 88 | 89 | 90 | sb.append(" {") 91 | if (!inline) { 92 | sb.append("\n") 93 | } 94 | 95 | attributes.filterBodyAttributes().forEach { attribute -> 96 | sb.addTabIndent(currentIndent + 1) 97 | if (attribute.isCustomForTag(this)) { 98 | sb.append(attribute.toKotlinxCustomAttribute()) 99 | } else { 100 | sb.append(attribute.toKotlinx(this)) 101 | } 102 | sb.append("\n") 103 | } 104 | 105 | 106 | 107 | for (child in children) { 108 | if (!inline) { 109 | sb.appendLine(child.toKotlinx(currentIndent + 1)) 110 | } else { 111 | 112 | // add space before + inline 113 | sb.append(" ") 114 | sb.append(child.toKotlinx(0)) 115 | } 116 | } 117 | 118 | if (!inline) { 119 | sb.addTabIndent(currentIndent) 120 | } 121 | sb.append("}") 122 | return sb.toString() 123 | } 124 | 125 | 126 | fun HtmlText.toKotlinxText(currentIndent: Int = 0): String = 127 | StringBuilder().apply { 128 | addTabIndent(currentIndent) 129 | append("+ \"\"\"$text\"\"\"") 130 | }.toString() 131 | 132 | 133 | fun Collection.toKotlinx(currentIndent: Int = 0): String = 134 | joinToString("\n") { it.toKotlinx(currentIndent) } 135 | 136 | fun HtmlAttribute.toKotlinx(owner: HtmlTag? = null): String { 137 | // remap for kotlinx 138 | val attrNameLowerCase = attrName.lowercase() 139 | val attrValue = """"$value"""" 140 | var attrName = when (attrNameLowerCase) { 141 | "class" -> "classes" 142 | else -> attrNameLowerCase 143 | } 144 | 145 | // remap for by owner tag name 146 | if(owner != null) { 147 | when(owner.tagName.lowercase()) { 148 | "label" -> { 149 | when(attrNameLowerCase) { 150 | "for" -> attrName = "htmlFor" 151 | } 152 | } 153 | } 154 | } 155 | 156 | 157 | return when { 158 | value != null -> """$attrName = $attrValue""".trim() 159 | else -> """$attrName = "true" """.trim() 160 | } 161 | 162 | } 163 | 164 | fun HtmlAttribute.toKotlinxCustomAttribute(): String = 165 | when { 166 | value != null -> """attributes["$attrName"] = "$value" """.trim() 167 | else -> """attributes["$attrName"] = "true" """.trim() 168 | } 169 | 170 | 171 | 172 | val tagsWithOnlyClasses = """ 173 | abbr 174 | address 175 | article 176 | aside 177 | audio 178 | b 179 | base 180 | bdi 181 | bdo 182 | blockQuote 183 | body 184 | br 185 | canvas 186 | canvas 187 | caption 188 | cite 189 | code 190 | col 191 | colGroup 192 | dataList 193 | dd 194 | del 195 | details 196 | dfn 197 | dialog 198 | div 199 | dl 200 | dt 201 | em 202 | embed 203 | fieldSet 204 | figcaption 205 | figure 206 | footer 207 | h1 208 | h2 209 | h3 210 | h4 211 | h5 212 | h6 213 | header 214 | hGroup 215 | hr 216 | i 217 | ins 218 | kbd 219 | label 220 | legend 221 | li 222 | main 223 | mark 224 | math 225 | mathml 226 | mathml 227 | meter 228 | nav 229 | noScript 230 | htmlObject 231 | ol 232 | option 233 | option 234 | output 235 | p 236 | pre 237 | progress 238 | q 239 | rp 240 | rt 241 | ruby 242 | samp 243 | section 244 | select 245 | small 246 | source 247 | span 248 | strong 249 | sub 250 | sup 251 | svg 252 | svg 253 | table 254 | tbody 255 | td 256 | tfoot 257 | thead 258 | time 259 | tr 260 | ul 261 | htmlVar 262 | video 263 | """.trimIndent().lines().toSet() -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/conversion/HtmlModel.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.conversion 2 | 3 | 4 | interface HtmlElement 5 | 6 | 7 | class HtmlText(val text: String) : HtmlElement 8 | 9 | 10 | class HtmlTag(val tagName: String) : HtmlElement { 11 | 12 | val attributes: MutableList = mutableListOf() 13 | 14 | val children: MutableList = mutableListOf() 15 | } 16 | 17 | 18 | 19 | class HtmlAttribute(val attrName: String, val value: String? = null) 20 | 21 | -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/conversion/HtmlPsiToHtmlDataConverter.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.conversion 2 | 3 | import com.intellij.lang.html.HTMLLanguage 4 | import com.intellij.openapi.project.Project 5 | import com.intellij.psi.PsiElement 6 | import com.intellij.psi.PsiFile 7 | import com.intellij.psi.PsiFileFactory 8 | import com.intellij.psi.impl.source.html.HtmlDocumentImpl 9 | import com.intellij.psi.impl.source.html.HtmlFileImpl 10 | import com.intellij.psi.xml.* 11 | import io.data2viz.kotlinx.htmlplugin.ide.debug 12 | import io.data2viz.kotlinx.htmlplugin.ide.logger 13 | 14 | 15 | object HtmlPsiToHtmlDataConverter { 16 | 17 | fun convertPsiFileToHtmlTag(psiFile: PsiFile): List { 18 | val result = mutableListOf() 19 | for (child in psiFile.children) { 20 | when (child) { 21 | is HtmlDocumentImpl -> child 22 | .children 23 | .forEach { 24 | convertPsiElementToHtmlElement(it)?.let { 25 | result.add(it) 26 | } 27 | } 28 | else -> { 29 | val htmlTag = convertPsiElementToHtmlElement(child) 30 | if (htmlTag != null) { 31 | result.add(htmlTag) 32 | } 33 | } 34 | } 35 | 36 | } 37 | 38 | return result 39 | } 40 | 41 | 42 | private fun convertPsiElementToHtmlElement(psiElement: PsiElement, parentHtmlTag: HtmlTag? = null): HtmlElement? = 43 | when (psiElement) { 44 | is XmlTag -> { 45 | 46 | val htmlElement = HtmlTag(psiElement.name) 47 | 48 | psiElement.children 49 | .mapNotNull { convertPsiElementToHtmlElement(it, htmlElement) } 50 | .forEach { htmlElement.children += it } 51 | 52 | htmlElement 53 | 54 | } 55 | 56 | is XmlAttribute -> { 57 | parentHtmlTag?.attributes?.add(psiElement.toHtmlAttribute()) 58 | null 59 | } 60 | 61 | is XmlText -> psiElement.text.toHtmlText() 62 | else -> null 63 | } 64 | 65 | private fun XmlAttribute.toHtmlAttribute(): HtmlAttribute = HtmlAttribute(name, value) 66 | 67 | private fun String.toHtmlText(): HtmlText? = if (trim().isEmpty()) null else HtmlText(trim()) 68 | 69 | private fun isStartsWithXmlElement(psiElement: PsiElement): Boolean { 70 | 71 | logger.debug {"isStartsWithXmlElement type $psiElement"} 72 | 73 | var isStartsWithXmlElement: Boolean 74 | when (psiElement) { 75 | 76 | is HtmlDocumentImpl, is HtmlFileImpl -> { 77 | val children = psiElement.children 78 | if (children.isNotEmpty()) { 79 | 80 | isStartsWithXmlElement = false 81 | for (child in children) { 82 | isStartsWithXmlElement = isStartsWithXmlElement(child) 83 | if (isStartsWithXmlElement) { 84 | break 85 | } else { 86 | if(child is XmlToken) { 87 | // it looks like "client.request {" which is not valid html code 88 | isStartsWithXmlElement = false 89 | break 90 | } 91 | } 92 | } 93 | 94 | 95 | } else { 96 | isStartsWithXmlElement = false 97 | } 98 | } 99 | 100 | is XmlTag, is XmlDoctype -> isStartsWithXmlElement = true 101 | 102 | else -> isStartsWithXmlElement = false 103 | } 104 | 105 | logger.debug { "isStartsWithXmlElement result=$isStartsWithXmlElement class ${psiElement.javaClass.name} \n ${psiElement.text}" } 106 | 107 | return isStartsWithXmlElement 108 | } 109 | 110 | fun isLooksLikeHtml(psiFile: PsiFile): Boolean = isStartsWithXmlElement(psiFile) 111 | 112 | fun createHtmlFileFromText(project: Project, fileName: String, text: String): PsiFile = 113 | PsiFileFactory 114 | .getInstance(project) 115 | .createFileFromText(fileName, HTMLLanguage.INSTANCE, text) 116 | 117 | fun createHtmlFileFromText(project: Project, text: String): PsiFile = 118 | PsiFileFactory 119 | .getInstance(project) 120 | .createFileFromText(HTMLLanguage.INSTANCE, text) 121 | } 122 | 123 | 124 | fun HtmlFileImpl.converToHtmlElements(): List = 125 | HtmlPsiToHtmlDataConverter.convertPsiFileToHtmlTag(this) 126 | -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/ide/ConvertHTMLToKotlinxAction.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.ide 2 | 3 | import com.intellij.openapi.actionSystem.AnAction 4 | import com.intellij.openapi.actionSystem.AnActionEvent 5 | import com.intellij.openapi.actionSystem.CommonDataKeys 6 | import com.intellij.openapi.command.WriteCommandAction 7 | import com.intellij.psi.PsiFile 8 | import com.intellij.psi.impl.source.html.HtmlFileImpl 9 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlPsiToHtmlDataConverter 10 | import io.data2viz.kotlinx.htmlplugin.conversion.converToHtmlElements 11 | import io.data2viz.kotlinx.htmlplugin.conversion.toKotlinx 12 | 13 | 14 | class ConvertHTMLToKotlinxAction : AnAction("Convert HTML To Kotlinx.html") { 15 | 16 | override fun actionPerformed(e: AnActionEvent) { 17 | logger.debug { "actionPerformed" } 18 | 19 | val editor = e.getRequiredData(CommonDataKeys.EDITOR) 20 | val project = e.project ?: return 21 | 22 | val document = editor.document 23 | val selectionModel = editor.selectionModel 24 | val start = selectionModel.selectionStart 25 | val end = selectionModel.selectionEnd 26 | 27 | 28 | val text = selectionModel.selectedText 29 | 30 | if (text.isNullOrEmpty()) { 31 | return 32 | } 33 | 34 | val sourcePsiFileFromText: PsiFile = HtmlPsiToHtmlDataConverter.createHtmlFileFromText(project, text!!) 35 | 36 | if (sourcePsiFileFromText !is HtmlFileImpl) { 37 | return 38 | } 39 | 40 | val convertedToKotlinText = sourcePsiFileFromText 41 | .converToHtmlElements() 42 | .toKotlinx() 43 | 44 | if (convertedToKotlinText.isEmpty()) { 45 | return 46 | } 47 | 48 | //Making the replacement 49 | WriteCommandAction.runWriteCommandAction(project) { 50 | document.replaceString(start, end, convertedToKotlinText) 51 | } 52 | selectionModel.removeSelection() 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/ide/ConvertTextHTMLCopyPasteProcessor.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.ide 2 | 3 | import com.intellij.codeInsight.editorActions.CopyPastePostProcessor 4 | import com.intellij.codeInsight.editorActions.TextBlockTransferableData 5 | import com.intellij.openapi.application.ApplicationManager 6 | import com.intellij.openapi.diagnostic.Logger 7 | import com.intellij.openapi.diagnostic.debug 8 | import com.intellij.openapi.editor.CaretStateTransferableData 9 | import com.intellij.openapi.editor.Editor 10 | import com.intellij.openapi.editor.RangeMarker 11 | import com.intellij.openapi.project.DumbService 12 | import com.intellij.openapi.project.Project 13 | import com.intellij.openapi.util.Ref 14 | import com.intellij.psi.PsiDocumentManager 15 | import com.intellij.psi.PsiFile 16 | import com.intellij.psi.impl.source.html.HtmlFileImpl 17 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlPsiToHtmlDataConverter 18 | import io.data2viz.kotlinx.htmlplugin.conversion.converToHtmlElements 19 | import io.data2viz.kotlinx.htmlplugin.conversion.toKotlinx 20 | import java.awt.datatransfer.DataFlavor 21 | import java.awt.datatransfer.Transferable 22 | 23 | val logger = Logger.getInstance(ConvertTextHTMLCopyPasteProcessor::class.java) 24 | 25 | 26 | inline fun Logger.debug (lazyMessage: () -> String ) { 27 | if (isDebugEnabled) { 28 | debug(lazyMessage()) 29 | } 30 | } 31 | 32 | 33 | class ConvertTextHTMLCopyPasteProcessor : CopyPastePostProcessor() { 34 | 35 | override fun collectTransferableData(file: PsiFile, editor: Editor, startOffsets: IntArray, endOffsets: IntArray): List = 36 | when (file) { 37 | is HtmlFileImpl -> listOf(HtmlTextTransferableData(file.name, file.text, startOffsets, endOffsets, true)) 38 | else -> listOf() 39 | } 40 | 41 | override fun extractTransferableData(content: Transferable): List { 42 | 43 | val result = mutableListOf() 44 | val dataFlavor = htmlDataFlavor 45 | val isCopyFromHtmlFile = content.isDataFlavorSupported(dataFlavor) 46 | 47 | logger.debug {"extractTransferableData isCopyFromHtmlFile=$isCopyFromHtmlFile" } 48 | if (isCopyFromHtmlFile) { 49 | val transferableData = content.getTransferData(dataFlavor) as TextBlockTransferableData 50 | result.add(transferableData) 51 | 52 | } else { 53 | 54 | val isNotCopyPasteFromIdeaFile = !content.isDataFlavorSupported(CaretStateTransferableData.FLAVOR) 55 | val isSomeStringContent = content.isDataFlavorSupported(DataFlavor.stringFlavor) 56 | 57 | logger.debug { "extractTransferableData isNotCopyPasteFromIdeaFile=$isNotCopyPasteFromIdeaFile" } 58 | logger.debug { "extractTransferableData isSomeStringContent=$isSomeStringContent"} 59 | if (isNotCopyPasteFromIdeaFile && isSomeStringContent) { 60 | val transferableData = content.getTransferData(DataFlavor.stringFlavor) 61 | 62 | val str = transferableData as String 63 | 64 | val external = ExternalFileHtmlTextTransferableData(str) 65 | result.add(external) 66 | } 67 | } 68 | 69 | logger.debug {"extractTransferableData result.size=${result.size}"} 70 | return result 71 | } 72 | 73 | override fun processTransferableData( 74 | project: Project, 75 | editor: Editor, 76 | bounds: RangeMarker, 77 | caretOffset: Int, 78 | indented: Ref, 79 | textValues: MutableList 80 | ) { 81 | 82 | val isDump = DumbService.getInstance(project).isDumb 83 | logger.debug { "processTransferableData isDump=$isDump" } 84 | if (isDump) { 85 | return 86 | } 87 | 88 | 89 | val psiDocumentManager = PsiDocumentManager.getInstance(project) 90 | val targetPsiFile = psiDocumentManager.getPsiFile(editor.document) 91 | logger.debug { "processTransferableData targetPsiFile=$targetPsiFile" } 92 | 93 | if (targetPsiFile == null) { 94 | return 95 | } 96 | 97 | val name = targetPsiFile.name 98 | if (!name.endsWith(".kt")) { 99 | return 100 | } 101 | 102 | 103 | 104 | val textValuesSize = textValues.size 105 | 106 | logger.debug { "processTransferableData textValuesSize=$textValuesSize" } 107 | if (textValuesSize != 1) { 108 | return 109 | } 110 | val textBlockTransferableData = textValues[0] 111 | 112 | logger.debug { "processTransferableData textBlockTransferableData=$textBlockTransferableData" } 113 | 114 | if (textBlockTransferableData !is HtmlTextTransferableData) { 115 | return 116 | } 117 | val htmlTextTransferableData = textBlockTransferableData 118 | 119 | 120 | val fileText = htmlTextTransferableData.fileText 121 | val fileName = htmlTextTransferableData.fileName 122 | 123 | val selectedText = if ( 124 | htmlTextTransferableData.startOffsets.isNotEmpty() && 125 | htmlTextTransferableData.endOffsets.isNotEmpty()) { 126 | fileText.subSequence( 127 | htmlTextTransferableData.startOffsets[0], 128 | htmlTextTransferableData.endOffsets[0]) 129 | .toString() 130 | } else { 131 | fileText 132 | } 133 | 134 | 135 | val sourcePsiFileFromText: PsiFile = HtmlPsiToHtmlDataConverter.createHtmlFileFromText(project, fileName, selectedText) 136 | 137 | logger.debug { "processTransferableData sourcePsiFileFromText=$sourcePsiFileFromText" } 138 | if (sourcePsiFileFromText !is HtmlFileImpl) { 139 | return 140 | } 141 | 142 | val isFromHtmlFile = htmlTextTransferableData.isFromHtmlFile 143 | logger.debug { "processTransferableData isFromHtmlFile=$isFromHtmlFile" } 144 | 145 | if (!isFromHtmlFile) { 146 | val looksLikeHtml = HtmlPsiToHtmlDataConverter.isLooksLikeHtml(sourcePsiFileFromText) 147 | logger.debug { "processTransferableData isLooksLikeHtml=$looksLikeHtml" } 148 | if (!looksLikeHtml) { 149 | return 150 | } 151 | } 152 | 153 | 154 | val htmlElements = sourcePsiFileFromText.converToHtmlElements() 155 | 156 | val convertedToKotlinText = htmlElements.toKotlinx().replaceInvalidLineSeparators() 157 | 158 | 159 | val notEmpty = convertedToKotlinText.isNotEmpty() 160 | logger.debug { "processTransferableData notEmpty = $notEmpty convertedToKotlinText=$convertedToKotlinText" } 161 | if (notEmpty) { 162 | 163 | 164 | val dialogIsOk = KotlinPasteFromHtmlDialog.isOK(project) 165 | logger.debug { "processTransferableData dialogIsOk=$dialogIsOk" } 166 | if (!dialogIsOk) { 167 | return 168 | } 169 | 170 | ApplicationManager.getApplication().runWriteAction { 171 | 172 | logger.debug { "processTransferableData runWriteAction" } 173 | val startOffset = bounds.startOffset 174 | editor.document.replaceString(bounds.startOffset, bounds.endOffset, convertedToKotlinText) 175 | val endOffsetAfterCopy = startOffset + convertedToKotlinText.length 176 | editor.caretModel.moveToOffset(endOffsetAfterCopy) 177 | val codeStyleManager = com.intellij.psi.codeStyle.CodeStyleManager.getInstance(project) 178 | 179 | codeStyleManager.reformatText(targetPsiFile, startOffset, endOffsetAfterCopy) 180 | PsiDocumentManager.getInstance(targetPsiFile.project).commitDocument(editor.document) 181 | } 182 | 183 | } 184 | } 185 | 186 | } 187 | 188 | val invalidLineSeparatorsRegex = Regex("(\r\n|\n\r|\r|\n)") 189 | /** 190 | * 191 | * Starting from 2019.2 IDEA don't like '\r' new lines 192 | * Fix for 18 issue 193 | * See [com.intellij.openapi.util.text.StringUtil.assertValidSeparators] 194 | * Actually, '\r' and '\n' is new line characters with 195 | * small difference 196 | * Formatting difference is doesn't matter because plugin apply new formatting after conversion 197 | */ 198 | fun String.replaceInvalidLineSeparators(): String { 199 | return replace(invalidLineSeparatorsRegex, "\n") 200 | } -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/ide/HtmlTextTransferableData.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.ide 2 | 3 | import com.intellij.codeInsight.editorActions.TextBlockTransferableData 4 | import java.awt.datatransfer.DataFlavor 5 | 6 | 7 | val htmlDataFlavor: DataFlavor = DataFlavor(ConvertTextHTMLCopyPasteProcessor::class.java, "class: ConvertTextHTMLCopyPasteProcessor") 8 | 9 | open class HtmlTextTransferableData(val fileName: String, 10 | val fileText: String, 11 | val startOffsets: IntArray, 12 | val endOffsets: IntArray, 13 | val isFromHtmlFile: Boolean) : TextBlockTransferableData { 14 | 15 | override fun getFlavor(): DataFlavor = htmlDataFlavor 16 | override fun getOffsetCount(): Int = 0 17 | override fun getOffsets(offsets: IntArray, index: Int): Int = index 18 | override fun setOffsets(offsets: IntArray, index: Int): Int = index 19 | 20 | } 21 | 22 | class ExternalFileHtmlTextTransferableData( 23 | fileText: String) 24 | : HtmlTextTransferableData( 25 | "external", fileText, intArrayOf(0), intArrayOf(fileText.length), false) -------------------------------------------------------------------------------- /src/main/kotlin/io/data2viz/kotlinx/htmlplugin/ide/KotlinPasteFromHtmlDialog.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin.ide 2 | 3 | 4 | import com.intellij.CommonBundle 5 | import com.intellij.openapi.project.Project 6 | import com.intellij.openapi.ui.DialogWrapper 7 | import com.intellij.uiDesigner.core.GridConstraints 8 | import com.intellij.uiDesigner.core.GridLayoutManager 9 | import com.intellij.uiDesigner.core.Spacer 10 | 11 | import java.awt.Insets 12 | import javax.swing.Action 13 | import javax.swing.JLabel 14 | import javax.swing.JPanel 15 | 16 | class KotlinPasteFromHtmlDialog(project: Project) : DialogWrapper(project, true) { 17 | 18 | companion object { 19 | 20 | fun isOK(project: Project): Boolean { 21 | val dialog = KotlinPasteFromHtmlDialog(project) 22 | dialog.show() 23 | return dialog.isOK 24 | } 25 | } 26 | 27 | private var myPanel: javax.swing.JPanel? = buildPanel() 28 | private val buttonOK: javax.swing.JButton? = null 29 | 30 | init { 31 | isModal = true 32 | rootPane.defaultButton = this.buttonOK 33 | title = "Convert Code From HTML" 34 | init() 35 | } 36 | 37 | override fun createCenterPanel(): javax.swing.JComponent? = this.myPanel 38 | 39 | override fun getContentPane(): java.awt.Container? = this.myPanel 40 | 41 | override fun createActions(): Array { 42 | setOKButtonText(CommonBundle.getYesButtonText()) 43 | setCancelButtonText(CommonBundle.getNoButtonText()) 44 | return arrayOf(okAction, cancelAction) 45 | } 46 | 47 | private fun buildPanel() = 48 | JPanel(GridLayoutManager(2, 1, Insets(0, 0, 0, 0), -1, -1, false, false)).apply { 49 | add(JLabel("Clipboard content copied from HTML file. Do you want to convert it to Kotlin code?"), 50 | GridConstraints(0, 0, 1, 1, 8, 0, 0, 0, null, null, null)) 51 | add(Spacer(), GridConstraints(1, 0, 1, 1, 0, 2, 1, 6, null, null, null)) 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/export.svg: -------------------------------------------------------------------------------- 1 | export -------------------------------------------------------------------------------- /src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | io.data2viz.kotlinx.htmlplugin 3 | HTML to kotlinx.html 4 | data2viz.io 5 | 6 | 9 | This plugin simplifies the transformation of HTML code to a kotlinx.html project by automatically 10 | doing the conversion during a copy/paste. 11 | 12 | 13 | ]]> 14 | 15 | 16 | 18 |
  • Fix issue #22: K2 compatibility issue
  • 19 | 20 | ]]> 21 |
    22 | 23 | 24 | com.intellij.modules.java 25 | org.jetbrains.kotlin 26 | 27 | 28 | 30 | 31 | 32 | 33 | 34 | 38 | 39 | 40 | 41 | 42 | 43 |
    -------------------------------------------------------------------------------- /src/main/resources/META-INF/pluginIcon_dark.svg: -------------------------------------------------------------------------------- 1 | pluginIcon_dark -------------------------------------------------------------------------------- /src/test/java/com/intellij/testFramework/LightPlatform4TestCase.java: -------------------------------------------------------------------------------- 1 | package com.intellij.testFramework; 2 | 3 | import com.intellij.openapi.util.text.StringUtil; 4 | import org.junit.Rule; 5 | import org.junit.rules.TestRule; 6 | import org.junit.runner.RunWith; 7 | import org.junit.runners.JUnit4; 8 | import org.junit.runners.model.Statement; 9 | 10 | 11 | /** 12 | * @author gregsh 13 | */ 14 | @RunWith(JUnit4.class) 15 | public abstract class LightPlatform4TestCase extends LightPlatformTestCase { 16 | @Rule 17 | public TestRule rule = (base, description) -> new Statement() { 18 | @Override 19 | public void evaluate() { 20 | String name = description.getMethodName(); 21 | setName(name.startsWith("test") ? name : "test" + StringUtil.capitalize(name)); 22 | } 23 | }; 24 | } -------------------------------------------------------------------------------- /src/test/kotlin/io/data2viz/kotlinx/htmlplugin/HtmlDataToHtmlKotlinXTest.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin 2 | 3 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlAttribute 4 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlTag 5 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlText 6 | import io.data2viz.kotlinx.htmlplugin.conversion.INDENT 7 | import io.data2viz.kotlinx.htmlplugin.conversion.isCustomForTag 8 | import io.data2viz.kotlinx.htmlplugin.conversion.isInline 9 | import io.data2viz.kotlinx.htmlplugin.conversion.toKotlinx 10 | import io.data2viz.kotlinx.htmlplugin.ide.replaceInvalidLineSeparators 11 | import org.junit.Assert 12 | import org.junit.Test 13 | 14 | class HtmlDataToHtmlKotlinXTest { 15 | 16 | @Test 17 | fun HtmlTagtoKotlinXBase() { 18 | Assert.assertEquals("div {\n}", HtmlTag("div").toKotlinx()) 19 | } 20 | 21 | @Test 22 | fun HtmlAttributeIsCustom() { 23 | 24 | val htmlTag = HtmlTag("link") 25 | 26 | var htmlAttribute = HtmlAttribute("data-label") 27 | Assert.assertEquals(true, htmlAttribute.isCustomForTag(htmlTag)) 28 | htmlAttribute = HtmlAttribute("data") 29 | Assert.assertEquals(false, htmlAttribute.isCustomForTag(htmlTag)) 30 | 31 | htmlAttribute = HtmlAttribute("link") 32 | Assert.assertEquals(false, htmlAttribute.isCustomForTag(htmlTag)) 33 | 34 | htmlAttribute = HtmlAttribute("integrity", "integrity") 35 | Assert.assertEquals(true, htmlAttribute.isCustomForTag(htmlTag)) 36 | 37 | htmlAttribute = HtmlAttribute("crossorigin", "crossorigin") 38 | Assert.assertEquals(true, htmlAttribute.isCustomForTag(htmlTag)) 39 | 40 | htmlAttribute = HtmlAttribute("link", "src") 41 | Assert.assertEquals(false, htmlAttribute.isCustomForTag(htmlTag)) 42 | 43 | } 44 | 45 | @Test 46 | fun HtmlTagIsInline() { 47 | var htmlTag = HtmlTag("div") 48 | Assert.assertEquals(false, htmlTag.isInline()) 49 | htmlTag.children.add(HtmlText("text")) 50 | 51 | Assert.assertEquals(true, htmlTag.isInline()) 52 | htmlTag.children.add(HtmlText("text")) 53 | Assert.assertEquals(false, htmlTag.isInline()) 54 | 55 | htmlTag = HtmlTag("div") 56 | htmlTag.children.add(HtmlTag("div")) 57 | Assert.assertEquals(false, htmlTag.isInline()) 58 | htmlTag.children.add(HtmlTag("div")) 59 | Assert.assertEquals(false, htmlTag.isInline()) 60 | 61 | } 62 | 63 | @Test 64 | fun HtmlTagtoKotlinXInline() { 65 | val htmlTag = HtmlTag("div") 66 | htmlTag.children.add(HtmlText("text")) 67 | Assert.assertEquals("div { + \"\"\"text\"\"\"}", htmlTag.toKotlinx()) 68 | } 69 | 70 | 71 | @Test 72 | fun HtmlTagtoKotlinXNested() { 73 | val htmlTag = HtmlTag("div") 74 | htmlTag.children.add(HtmlTag("p")) 75 | htmlTag.children.add(HtmlTag("span")) 76 | Assert.assertEquals("div {\n${INDENT}p {\n$INDENT}\n${INDENT}span {\n$INDENT}\n}", htmlTag.toKotlinx()) 77 | } 78 | 79 | @Test 80 | fun InvalidLineSeparatorsReplaceTest() { 81 | val htmlTag = HtmlTag("div") 82 | htmlTag.children.add(HtmlText("one\ntwo")) 83 | Assert.assertEquals("div { + \"\"\"one\ntwo\"\"\"}", htmlTag.toKotlinx().replaceInvalidLineSeparators()) 84 | htmlTag.children.clear() 85 | htmlTag.children.add(HtmlText("one\rtwo")) 86 | Assert.assertEquals("div { + \"\"\"one\ntwo\"\"\"}", htmlTag.toKotlinx().replaceInvalidLineSeparators()) 87 | htmlTag.children.clear() 88 | htmlTag.children.add(HtmlText("one\r\ntwo")) 89 | Assert.assertEquals("div { + \"\"\"one\ntwo\"\"\"}", htmlTag.toKotlinx().replaceInvalidLineSeparators()) 90 | htmlTag.children.clear() 91 | htmlTag.children.add(HtmlText("one\n\rtwo")) 92 | Assert.assertEquals("div { + \"\"\"one\ntwo\"\"\"}", htmlTag.toKotlinx().replaceInvalidLineSeparators()) 93 | } 94 | 95 | 96 | @Test 97 | fun HtmlTagtoKotlinXNestedTwice() { 98 | val htmlTag = HtmlTag("div") 99 | val inner = HtmlTag("p") 100 | htmlTag.children.add(inner) 101 | inner.children.add(HtmlTag("span")) 102 | Assert.assertEquals("div {\n${INDENT}p {\n$INDENT${INDENT}span {\n$INDENT$INDENT}\n$INDENT}\n}", htmlTag.toKotlinx()) 103 | } 104 | 105 | 106 | @Test 107 | fun HtmlTagtoKotlinXCustomAttributes() { 108 | val htmlTag = HtmlTag("div") 109 | 110 | htmlTag.attributes.add(HtmlAttribute("aria-label1")) 111 | htmlTag.attributes.add(HtmlAttribute("aria-label2", "value2")) 112 | 113 | Assert.assertEquals("div {\n${INDENT}attributes[\"aria-label1\"] = \"true\"\n${INDENT}attributes[\"aria-label2\"] = \"value2\"\n}", htmlTag.toKotlinx()) 114 | } 115 | 116 | @Test 117 | fun HtmlAttributetoKotlinXWithoutValue() { 118 | Assert.assertEquals("attr_name = \"true\"", HtmlAttribute("attr_name").toKotlinx()) 119 | } 120 | 121 | @Test 122 | fun HtmlAttributetoKotlinXWithValue() { 123 | Assert.assertEquals("attr_name = \"attr_value\"", HtmlAttribute("attr_name", "attr_value").toKotlinx()) 124 | } 125 | } -------------------------------------------------------------------------------- /src/test/kotlin/io/data2viz/kotlinx/htmlplugin/HtmlTextToHtmlDataTest.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin 2 | 3 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlTag 4 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlText 5 | import org.junit.Assert 6 | import org.junit.Test 7 | 8 | class HtmlTextToHtmlDataTest : ResourcesTest() { 9 | 10 | 11 | @Test 12 | fun fileHtmlToHtmlDataBase() { 13 | val htmlTags = loadHtmlData("base.html") 14 | 15 | Assert.assertEquals(1, htmlTags.size) 16 | 17 | 18 | (htmlTags[0] as HtmlTag).apply { 19 | Assert.assertEquals("div", tagName) 20 | } 21 | 22 | } 23 | 24 | @Test 25 | fun fileHtmlToHtmlDataAttrsBase() { 26 | val htmlTags = loadHtmlData("attrs_base.html") 27 | 28 | Assert.assertEquals(1, htmlTags.size) 29 | 30 | (htmlTags[0] as HtmlTag).apply { 31 | Assert.assertEquals("div", tagName) 32 | Assert.assertEquals(2, attributes.size) 33 | attributes[0].apply { 34 | Assert.assertEquals("attr1", attrName) 35 | Assert.assertEquals(null, value) 36 | } 37 | attributes[1].apply { 38 | Assert.assertEquals("attr2", attrName) 39 | Assert.assertEquals("value2", value) 40 | } 41 | 42 | 43 | } 44 | 45 | } 46 | 47 | @Test 48 | fun fileHtmlToHtmlDataNested() { 49 | val htmlTags = loadHtmlData("nested.html") 50 | 51 | Assert.assertEquals(1, htmlTags.size) 52 | 53 | 54 | (htmlTags[0] as HtmlTag).apply { 55 | Assert.assertEquals("div", tagName) 56 | Assert.assertEquals(1, children.size) 57 | (children[0] as HtmlTag).apply { 58 | Assert.assertEquals("div", tagName) 59 | } 60 | 61 | } 62 | 63 | } 64 | 65 | @Test 66 | fun fileHtmlToHtmlDataNestedSeveralChilds() { 67 | val htmlTags = loadHtmlData("nested_several_text_childs.html") 68 | 69 | Assert.assertEquals(1, htmlTags.size) 70 | 71 | 72 | (htmlTags[0] as HtmlTag).apply { 73 | Assert.assertEquals("div", tagName) 74 | Assert.assertEquals(4, children.size) 75 | (children[0] as HtmlText).apply { 76 | Assert.assertEquals("Text", text) 77 | } 78 | (children[1] as HtmlTag).apply { 79 | Assert.assertEquals("b", tagName) 80 | } 81 | 82 | (children[2] as HtmlText).apply { 83 | Assert.assertEquals("and", text) 84 | } 85 | 86 | (children[3] as HtmlTag).apply { 87 | Assert.assertEquals("i", tagName) 88 | } 89 | 90 | } 91 | 92 | 93 | } 94 | 95 | 96 | } -------------------------------------------------------------------------------- /src/test/kotlin/io/data2viz/kotlinx/htmlplugin/HtmlTextToKotlinXTextTest.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin 2 | 3 | 4 | import io.data2viz.kotlinx.htmlplugin.conversion.toKotlinx 5 | import org.junit.Assert 6 | import org.junit.Test 7 | 8 | class HtmlTextToKotlinXTextTest : ResourcesTest() { 9 | 10 | 11 | @Test 12 | fun FileHtmlToKotlinXBase() { 13 | assertFiles("base.html", "base.htmlkotlinx") 14 | } 15 | 16 | @Test 17 | fun FileHtmlToKotlinXAttrsBase() { 18 | assertFiles("attrs_base.html", "attrs_base.htmlkotlinx") 19 | } 20 | 21 | @Test 22 | fun FileHtmlToKotlinXAttrsCustom() { 23 | assertFiles("attrs_custom.html", "attrs_custom.htmlkotlinx") 24 | } 25 | 26 | @Test 27 | fun FileHtmlToKotlinXAttrsImg() { 28 | assertFiles("attrs_img.html", "attrs_img.htmlkotlinx") 29 | } 30 | 31 | 32 | @Test 33 | fun FileHtmlToKotlinXAttrsA() { 34 | assertFiles("attrs_a.html", "attrs_a.htmlkotlinx") 35 | } 36 | 37 | 38 | @Test 39 | fun FileHtmlToKotlinXAttrsInline() { 40 | assertFiles("inline.html", "inline.htmlkotlinx") 41 | } 42 | 43 | @Test 44 | fun FileHtmlToKotlinXNested() { 45 | assertFiles("nested.html", "nested.htmlkotlinx") 46 | } 47 | 48 | @Test 49 | fun FileHtmlToKotlinXHtmlFor() { 50 | assertFiles("htmlFor.html", "htmlFor.htmlkotlinx") 51 | } 52 | 53 | @Test 54 | fun FileHtmlToKotlinXNestedSeveralTextChilds() { 55 | assertFiles("nested_several_text_childs.html", "nested_several_text_childs.htmlkotlinx") 56 | } 57 | 58 | @Test 59 | fun FileHtmlToKotlinXClasses() { 60 | assertFiles("attrs_class.html", "attrs_class.htmlkotlinx") 61 | } 62 | 63 | @Test 64 | fun FileHtmlToKotlinXTextarea() { 65 | assertFiles("textarea.html", "textarea.htmlkotlinx") 66 | } 67 | @Test 68 | fun FileHtmlToKotlinXUppercase() { 69 | assertFiles("uppercase.html", "uppercase.htmlkotlinx") 70 | } 71 | private fun assertFiles(filenameHtml: String, filenameKotlinX: String) { 72 | 73 | val kotlinXText = loadFileText(filenameKotlinX) 74 | 75 | val htmlTags = loadHtmlData(filenameHtml) 76 | 77 | val convertedToKotlinXText = htmlTags.toKotlinx() 78 | 79 | Assert.assertEquals(kotlinXText, convertedToKotlinXText) 80 | 81 | } 82 | 83 | 84 | } -------------------------------------------------------------------------------- /src/test/kotlin/io/data2viz/kotlinx/htmlplugin/ResourcesTest.kt: -------------------------------------------------------------------------------- 1 | package io.data2viz.kotlinx.htmlplugin 2 | 3 | import com.intellij.testFramework.LightPlatform4TestCase 4 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlElement 5 | import io.data2viz.kotlinx.htmlplugin.conversion.HtmlPsiToHtmlDataConverter 6 | import java.io.File 7 | 8 | abstract class ResourcesTest : LightPlatform4TestCase() { 9 | 10 | 11 | protected fun loadHtmlData(filenameHtml: String): List { 12 | 13 | val htmlText = loadFileText(filenameHtml) 14 | 15 | val psiHtmlFile = HtmlPsiToHtmlDataConverter.createHtmlFileFromText(project, filenameHtml, htmlText) 16 | 17 | return HtmlPsiToHtmlDataConverter.convertPsiFileToHtmlTag(psiHtmlFile) 18 | 19 | } 20 | 21 | protected fun loadFileText(filename: String): String { 22 | val classLoader = javaClass.classLoader 23 | val file = File(classLoader.getResource(filename)!!.file) 24 | return file.readText() 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/resources/attrs_a.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /src/test/resources/attrs_a.htmlkotlinx: -------------------------------------------------------------------------------- 1 | a { 2 | href = "#" 3 | } -------------------------------------------------------------------------------- /src/test/resources/attrs_base.html: -------------------------------------------------------------------------------- 1 |
    2 | 3 |
    -------------------------------------------------------------------------------- /src/test/resources/attrs_base.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div { 2 | attr1 = "true" 3 | attr2 = "value2" 4 | } -------------------------------------------------------------------------------- /src/test/resources/attrs_class.html: -------------------------------------------------------------------------------- 1 |
    2 | 3 |
    -------------------------------------------------------------------------------- /src/test/resources/attrs_class.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div("one two") { 2 | button(classes = "btn") { 3 | } 4 | } -------------------------------------------------------------------------------- /src/test/resources/attrs_custom.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /src/test/resources/attrs_custom.htmlkotlinx: -------------------------------------------------------------------------------- 1 | link { 2 | attributes["integrity"] = "value" 3 | attributes["aria-label"] = "value" 4 | attr2 = "value2" 5 | } -------------------------------------------------------------------------------- /src/test/resources/attrs_img.html: -------------------------------------------------------------------------------- 1 | text 2 | 3 | -------------------------------------------------------------------------------- /src/test/resources/attrs_img.htmlkotlinx: -------------------------------------------------------------------------------- 1 | img { 2 | src = "1.png" 3 | alt = "text" 4 | } -------------------------------------------------------------------------------- /src/test/resources/base.html: -------------------------------------------------------------------------------- 1 |
    2 | 3 |
    -------------------------------------------------------------------------------- /src/test/resources/base.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div("abc") { 2 | } -------------------------------------------------------------------------------- /src/test/resources/htmlFor.html: -------------------------------------------------------------------------------- 1 |
    2 | 3 |
    -------------------------------------------------------------------------------- /src/test/resources/htmlFor.htmlkotlinx: -------------------------------------------------------------------------------- 1 | form { 2 | action = "/action_page.php" 3 | label { 4 | htmlFor = "male" 5 | + """Male""" 6 | } 7 | } -------------------------------------------------------------------------------- /src/test/resources/inline.html: -------------------------------------------------------------------------------- 1 |
    2 | text 3 |
    -------------------------------------------------------------------------------- /src/test/resources/inline.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div { + """text"""} -------------------------------------------------------------------------------- /src/test/resources/nested.html: -------------------------------------------------------------------------------- 1 |
    2 |
    3 |
    -------------------------------------------------------------------------------- /src/test/resources/nested.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div { 2 | div { 3 | } 4 | } -------------------------------------------------------------------------------- /src/test/resources/nested_several_text_childs.html: -------------------------------------------------------------------------------- 1 |
    2 | Text bold and italic 3 |
    -------------------------------------------------------------------------------- /src/test/resources/nested_several_text_childs.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div { 2 | + """Text""" 3 | b { + """bold"""} 4 | + """and""" 5 | i { + """italic"""} 6 | } -------------------------------------------------------------------------------- /src/test/resources/textarea.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/test/resources/textarea.htmlkotlinx: -------------------------------------------------------------------------------- 1 | textArea { 2 | } -------------------------------------------------------------------------------- /src/test/resources/uppercase.html: -------------------------------------------------------------------------------- 1 |
    2 | 3 |
    -------------------------------------------------------------------------------- /src/test/resources/uppercase.htmlkotlinx: -------------------------------------------------------------------------------- 1 | div { 2 | attr2 = "value2" 3 | } --------------------------------------------------------------------------------