j2html icon indicating copy to clipboard operation
j2html copied to clipboard

#225 JS html to j2html code generator via kotlin multiplatform

Open PancakeInvaders2 opened this issue 1 year ago • 8 comments

PancakeInvaders2 avatar Mar 14 '24 00:03 PancakeInvaders2

I used kotlin multiplatform to write the converter so that it's in a language that is type-safe, null-safe, and generally nice to work with while able to compile to javascript

The structure of src, with commonMain, commonTest, jsMain, jsTest, jvmMain, and jvmTest is because kotlin multiplatform structures the code to share some of it between platforms (in this case jvm and javascript), and to have some platform specific code for each target

To generate the javascript, launch ./gradlew build, on build success, the compiled JS file will be at ./build/dist/js/productionExecutable/html-to-j2html.js

I used gradle instead of maven because that seems to be the standard for kotlin multiplatform projects, and I could not find any information on how to do the same in maven. It didn't need to be coupled to the rest of the maven structure anyway because while you can be called from and you can call java code from kotlin multiplatform, you can only call it from the jvm target, not from the javascript target

I included a quick and dirty index.html file that calls the converter. To play around with the converter, the easiest way is to open the html-to-j2html folder in VSCode, right click on index.html, Open with live server. The converter should be operational in the opened browser

Launching ./gradlew run will launch jvmMain.kt in the jvm, which will convert the read the content of input.html, convert it, and write it to output.java

PancakeInvaders2 avatar Mar 14 '24 01:03 PancakeInvaders2

The actual converter logic is in commonMain, in Converter.kt, it uses the library KSoup to read the html. How each attributes is supported in configured in SupportedJ2HtmlAttributes.kt, this file may need to modified in the future if new attributes are supported or change types or something.

I also wrote a bunch of unit tests in ConverterTests in commonTest, using snippets I found online, tweaking the converter until the produced code seemed acceptable. You can have a good look at the expected outputs to see if the generated code is the way you'd want it to be.

Feel free to ask about any unclear parts

PancakeInvaders2 avatar Mar 14 '24 01:03 PancakeInvaders2

Is this waiting for @sembler or me?

tipsy avatar Mar 27 '24 20:03 tipsy

Is this question directed at me ? I wouldn't know, I suppose that's for you and @sembler to decide

PancakeInvaders2 avatar Apr 20 '24 10:04 PancakeInvaders2

any news ?

PancakeInvaders2 avatar May 26 '24 15:05 PancakeInvaders2

any news ?

Sorry @PancakeInvaders2, life is sort of busy at the moment, and this pull request is pretty large.

When I said I could help get this added to the website I was picturing adding a JS library to the website and calling it with some input, not adding a whole new module of a thousand+ lines. I originally handed this project over to @sembler some years ago, but he has become quite busy too.

I'll try to help you get it live, but I don't see any JS library in the PR, could you outline how this can be created and plugged into the website?

tipsy avatar May 26 '24 16:05 tipsy

I understand, the purpose of the index.html was to show how this could be used in the website

Maybe it would be easier if I put the generator in a separate project, maintain it myself, and put it on npm or something so you can use it as you please

PancakeInvaders2 avatar May 26 '24 16:05 PancakeInvaders2

I saw the HTML file, but not the actual html-to-j2html.js file - If you could maintain the generator code and distribute it on jsDelivr (or whatever the default is these day), that would be great.

tipsy avatar May 27 '24 14:05 tipsy