├── .gitignore ├── LICENSE ├── README.md ├── bootstrap ├── src │ └── acavailhez │ │ └── html │ │ └── bootstrap │ │ ├── Bootstrap4AlertTrait.groovy │ │ ├── Bootstrap4BtnTrait.groovy │ │ ├── Bootstrap4Color.groovy │ │ ├── Bootstrap4GridTrait.groovy │ │ ├── Bootstrap4ModalTrait.groovy │ │ ├── Bootstrap4NavTrait.groovy │ │ ├── Bootstrap4Size.groovy │ │ └── Bootstrap4Trait.groovy └── test │ └── acavailhez │ └── html │ └── bootstrap │ ├── BootstrapAlertTests.groovy │ ├── BootstrapButtonTests.groovy │ ├── BootstrapGridTests.groovy │ ├── BootstrapModalTests.groovy │ └── BootstrapNavTests.groovy ├── build.gradle ├── core ├── README.md ├── src │ └── acavailhez │ │ ├── html │ │ ├── Html.groovy │ │ ├── HtmlFragment.groovy │ │ ├── HtmlPage.groovy │ │ ├── HtmlRenderMode.groovy │ │ ├── HtmlStyle.groovy │ │ ├── Javascript.groovy │ │ ├── builder │ │ │ ├── EscapedHtmlBuilder.groovy │ │ │ ├── HtmlBuilder.groovy │ │ │ └── RawHtmlBuilder.groovy │ │ ├── internal │ │ │ └── HtmlEngine.java │ │ ├── scope │ │ │ ├── HtmlMapDomScoped.java │ │ │ ├── HtmlScopable.groovy │ │ │ ├── HtmlScope.java │ │ │ └── HtmlScopeListener.groovy │ │ ├── traits │ │ │ ├── AttemptTrait.groovy │ │ │ ├── CaptureTrait.groovy │ │ │ ├── HeadTrait.groovy │ │ │ ├── Html5Trait.groovy │ │ │ ├── HtmlFragmentTrait.groovy │ │ │ ├── HtmlTrait.groovy │ │ │ └── ShortcutTrait.groovy │ │ └── utils │ │ │ ├── HtmlAttributes.java │ │ │ └── HtmlUtils.groovy │ │ └── optget │ │ ├── CastUtils.java │ │ ├── OptGet.java │ │ ├── OptGetMap.java │ │ └── OptGetWrapper.java ├── test-resources │ ├── grails.vm │ └── modal.vm └── test │ ├── acavailhez │ ├── html │ │ ├── AttemptTests.groovy │ │ ├── CaptureTests.groovy │ │ ├── EscapeTests.groovy │ │ ├── FragmentTests.groovy │ │ ├── Html5TraitTests.groovy │ │ ├── PageTests.groovy │ │ ├── ScopeTests.groovy │ │ ├── ShortcutTests.groovy │ │ ├── SimpleTests.groovy │ │ ├── TagTests.groovy │ │ ├── performance │ │ │ ├── GrailsHomepage.groovy │ │ │ ├── ModalHtml.groovy │ │ │ └── PerformanceTests.groovy │ │ └── scope │ │ │ └── HtmlScopeListenerTests.groovy │ └── optget │ │ ├── GetOptTests.groovy │ │ └── OptGetWrapperTests.groovy │ └── perf-velocity.vm ├── resources ├── README.md ├── src │ └── acavailhez │ │ └── html │ │ └── resources │ │ ├── HtmlResource.groovy │ │ ├── HtmlResourceDisposition.groovy │ │ ├── HtmlResourcesPack.groovy │ │ └── HtmlResourcesTrait.groovy └── test │ └── acavailhez │ └── html │ └── resources │ └── SimpleTests.groovy ├── scripts ├── src │ ├── GenerateBootstrap4GridCols.groovy │ ├── GenerateHtml5Trait.groovy │ ├── GenerateHtml5TraitTest.groovy │ ├── GrailsHomepageToVelocity.groovy │ ├── HtmlToGroovyConverter.groovy │ └── HtmlToVelocityConverter.groovy └── test │ └── acavailhez │ └── html │ └── ConverterTest.groovy ├── settings.gradle ├── spark-demo ├── resources │ └── web │ │ ├── css │ │ └── example.css │ │ └── js │ │ └── example.js └── src │ ├── Main.groovy │ └── acavailhez │ └── html │ └── demo │ └── Frontpage.groovy ├── spark └── src │ └── acavailhez │ └── html │ └── spark │ ├── AbstractSparkRoute.java │ └── HtmlPageSparkRoute.java └── tests └── src └── acavailhez └── html └── tests └── AbstractTests.groovy /.gitignore: -------------------------------------------------------------------------------- 1 | **/target 2 | **/.settings 3 | 4 | **/.gradle 5 | **/build 6 | 7 | **/.idea 8 | **/.idea/misc.xml 9 | **/.idea/tasks.xml 10 | **/.idea/workspace.xml 11 | **/*.iml 12 | 13 | **/*.iws 14 | **/*.log 15 | **/*.log.* 16 | **/deploy/ 17 | **/atlassian-ide-plugin.xml 18 | **/out 19 | 20 | gradlew 21 | gradlew.bat 22 | 23 | 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | # groovy-html-renderer 2 | 3 | in-code html5 rendering engine leveraging the groovy syntax: 4 | 5 | ``` 6 | div(class:'box'){ 7 | div(class:'box-body'){ 8 | escape << "content of the box" 9 | } 10 | } 11 | ``` 12 | 13 | renders: 14 | 15 | ``` 16 |
17 |
18 | content of the box 19 |
20 |
21 | ``` 22 | 23 | ## Why? 24 | 25 | Server-side rendering of complex html is not everyone's cup of tea, but if you fancy it, `groovy-html-renderer` provides a reliable way to do it 26 | 27 | In java, your choice is usually limited to jsp or similar templating engine, working on html and adding clumsy `if` and `for` statements in custom tags (``). 28 | In `groovy-html-renderer` you are rendering the html in the code itself, so you can leverage the power of the tools you already have, as well as type-check your objects. 29 | 30 | Once you are in code, all sorts of interesting features become possible, such as the `attempt` function, which will act like a try-catch: 31 | 32 | ``` 33 | div { 34 | attempt { 35 | div { 36 | escape << 'text' 37 | throw new Exception("oups") 38 | } 39 | } { Throwable t -> 40 | span { 41 | escape << 'failed with:' << t.getMessage() 42 | } 43 | } 44 | } 45 | ``` 46 | 47 | renders: 48 | 49 | ``` 50 |
51 | oups 52 |
53 | ``` 54 | 55 | This will prevent the whole document from crashing if only a non-critical part failed to render 56 | 57 | Using Groovy's `Trait`, it's easy to add new shortcuts and features to the syntax. 58 | Taking the example of Bootstrap's modal ([http://getbootstrap.com/javascript/#static-example](http://getbootstrap.com/javascript/#static-example)), writing it directly requires typing: 59 | 60 | ``` 61 | 78 | ``` 79 | 80 | With `Bootstrap4Trait` it can be simplified to: 81 | 82 | ``` 83 | public class Snippet extends Html implements Bootstrap4Trait{ 84 | public void build() { 85 | 86 | modal(title: "Modal title", closeLabel: "Close") { 87 | p("One fine body…") 88 | } { 89 | button(btn('data-dismiss': 'modal'), "Close") 90 | button(btn(color: Bootstrap4Color.PRIMARY), "Save changes") 91 | } 92 | 93 | } 94 | } 95 | ``` 96 | 97 | Exposing only the important content and making the code easier to read 98 | 99 | 100 | ## Performances 101 | 102 | The order of magnitude you should expect for the rendering of a html page is < 10ms. 103 | 104 | 105 | This is a number to consider relative to what you are doing around the generation of the page. If you are making even a single SQL query in order to generate the page, then using `groovy-html-renderer` will not have any noticeable impact on your performances. 106 | Now if you are rendering static content, then `groovy-html-renderer` is probably not the right tool for the job. 107 | 108 | Here are some tips to improve performances of your html pages: 109 | 110 | - Use `@CompileStatic` in your pages. This will have a 5x performance boost on pages with a lot of subclosures 111 | - Opt for a basic rendering engine for pages with a lot of static content. `groovy-html-renderer` will perform poorly there 112 | 113 | Let's have a look at the best and worst case scenarios, we compare against 1000 run `groovy-html-renderer` to Apache's `velocity` which is build for speed: 114 | 115 | *Rendering the boostrap modal example - over 1000 runs* 116 | 117 | | Velocity | groovy-html-renderer | 118 | |---|---| 119 | | 0.086ms | 0.254 ms | 120 | 121 | In this run, a small snippet with a for-loop, `groovy-html-renderer` performs 3x slower than the highly optimized `velocity` 122 | 123 | *Rendering the grails homepage - 1000 runs* 124 | 125 | | Velocity | groovy-html-renderer | 126 | |---|---| 127 | | 0.037ms | 1.262 ms | 128 | 129 | In the worst case scenario, a long static page with no customized content, `groovy-html-renderer` performs 34x slower than `velocity` 130 | 131 | But even in the worst-case scenario, the rendering of a long static page containing a lot of tags written with closures, `groovy-html-renderer` renders the page in a matter of milliseconds. 132 | Any SQL request made by your application will dwarf those times to negligeable amounts. 133 | 134 | ## Detailed documentation by component 135 | 136 | ### [Core features](core/README.md) 137 | ### [Resources (js and css)](resources/README.md) 138 | -------------------------------------------------------------------------------- /bootstrap/src/acavailhez/html/bootstrap/Bootstrap4AlertTrait.groovy: -------------------------------------------------------------------------------- 1 | package acavailhez.html.bootstrap 2 | 3 | import acavailhez.html.traits.Html5Trait 4 | import acavailhez.html.utils.HtmlAttributes 5 | import groovy.transform.CompileStatic 6 | 7 | // http://v4-alpha.getbootstrap.com/components/alerts/ 8 | @CompileStatic 9 | trait Bootstrap4AlertTrait extends Html5Trait { 10 | 11 | public void alert(Map map, Closure body) { 12 | HtmlAttributes attrs = HtmlAttributes.wrap(map) 13 | Bootstrap4Color color = attrs.opt('color', Bootstrap4Color, Bootstrap4Color.DEFAULT) 14 | attrs.remove('color') 15 | attrs.put('role', 'alert') 16 | attrs.addToClass('alert alert-' + color.name().toLowerCase()) 17 | div(attrs) { 18 | body() 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /bootstrap/src/acavailhez/html/bootstrap/Bootstrap4BtnTrait.groovy: -------------------------------------------------------------------------------- 1 | package acavailhez.html.bootstrap 2 | 3 | import acavailhez.html.traits.Html5Trait 4 | import acavailhez.html.utils.HtmlAttributes 5 | import groovy.transform.CompileStatic 6 | 7 | // shortcuts for bootstrap 4 buttons 8 | // http://v4-alpha.getbootstrap.com/components/buttons/ 9 | @CompileStatic 10 | trait Bootstrap4BtnTrait extends Html5Trait { 11 | 12 | // Reconfigures the attributes of a tag 13 | HtmlAttributes btn(Map map) { 14 | HtmlAttributes attrs = HtmlAttributes.wrap(map) 15 | 16 | attrs.addToClass('btn') 17 | attrs.put('type', 'button') 18 | 19 | // color 20 | Bootstrap4Color color = attrs.opt('color', Bootstrap4Color, Bootstrap4Color.DEFAULT) 21 | attrs.remove('color') 22 | String btnClass = 'btn-' + color.name().toLowerCase() 23 | if (attrs.opt('outline', Boolean, false)) { 24 | btnClass += '-outline' 25 | } 26 | attrs.remove('outline') 27 | attrs.addToClass(btnClass) 28 | 29 | // size 30 | Bootstrap4Size size = attrs.opt('size', Bootstrap4Size, null) 31 | attrs.remove('size') 32 | if (size) { 33 | attrs.addToClass('btn-' + size.name().toLowerCase()) 34 | } 35 | 36 | return attrs 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bootstrap/src/acavailhez/html/bootstrap/Bootstrap4Color.groovy: -------------------------------------------------------------------------------- 1 | package acavailhez.html.bootstrap 2 | 3 | enum Bootstrap4Color { 4 | 5 | DEFAULT, 6 | PRIMARY, 7 | SECONDARY, 8 | SUCCESS, 9 | INFO, 10 | WARNING, 11 | DANGER, 12 | LINK 13 | 14 | } 15 | -------------------------------------------------------------------------------- /bootstrap/src/acavailhez/html/bootstrap/Bootstrap4GridTrait.groovy: -------------------------------------------------------------------------------- 1 | package acavailhez.html.bootstrap 2 | 3 | import acavailhez.html.traits.Html5Trait 4 | import acavailhez.html.utils.HtmlAttributes 5 | import groovy.transform.CompileStatic 6 | import org.apache.log4j.Logger 7 | 8 | // http://v4-alpha.getbootstrap.com/layout/grid/ 9 | // Will manage the rows automatically 10 | @CompileStatic 11 | trait Bootstrap4GridTrait extends Html5Trait { 12 | 13 | private static final Logger log = Logger.getLogger(Bootstrap4GridTrait) 14 | private static final String COLS_COUNT_KEY = '_bnoccr' // Bootstrap Number Of Columns In Current Row 15 | 16 | public void container(Closure body) { 17 | container([:], body) 18 | } 19 | 20 | public void container(Map map, Closure body) { 21 | HtmlAttributes attrs = HtmlAttributes.wrap(map) 22 | div(attrs.addToClass('container')) { 23 | body() 24 | if (scope.opt(COLS_COUNT_KEY, Integer, 0) > 0) { 25 | log.warn('row forced closed with only ' + scope.get(COLS_COUNT_KEY, Integer) + ' cols') 26 | html << '' 27 | scope.put(COLS_COUNT_KEY, 0) 28 | } 29 | } 30 | } 31 | 32 | private void bootstrapCol(Map map, int cols, Bootstrap4Size size, Closure body) { 33 | 34 | //open the row if first one 35 | if (scope.opt(COLS_COUNT_KEY, Integer, 0) == 0) { 36 | scope.put(COLS_COUNT_KEY, 0) 37 | html << '
' 38 | } 39 | 40 | HtmlAttributes attrs = HtmlAttributes.wrap(map) 41 | attrs.addToClass('col-' + size.name().toLowerCase() + '-' + cols) 42 | 43 | div(attrs) { 44 | if (body) { 45 | body() 46 | } else { 47 | html << ' ' // empty col for spacing 48 | } 49 | } 50 | 51 | scope.put(COLS_COUNT_KEY, scope.get(COLS_COUNT_KEY, Integer) + cols) 52 | 53 | //close the row if too big 54 | if (scope.get(COLS_COUNT_KEY, Integer) >= 12) { 55 | if (scope.get(COLS_COUNT_KEY, Integer) > 12) { 56 | log.warn('current bootstrap row is more than 12 cols') 57 | } 58 | html << '
' 59 | scope.put(COLS_COUNT_KEY, 0) 60 | } 61 | } 62 | 63 | public void colSm1(Closure body) { 64 | bootstrapCol([:], 1, Bootstrap4Size.SM, body) 65 | } 66 | 67 | public void colSm1(Map map, Closure body) { 68 | bootstrapCol(map, 1, Bootstrap4Size.SM, body) 69 | } 70 | 71 | public void colSm2(Closure body) { 72 | bootstrapCol([:], 2, Bootstrap4Size.SM, body) 73 | } 74 | 75 | public void colSm2(Map map, Closure body) { 76 | bootstrapCol(map, 2, Bootstrap4Size.SM, body) 77 | } 78 | 79 | public void colSm3(Closure body) { 80 | bootstrapCol([:], 3, Bootstrap4Size.SM, body) 81 | } 82 | 83 | public void colSm3(Map map, Closure body) { 84 | bootstrapCol(map, 3, Bootstrap4Size.SM, body) 85 | } 86 | 87 | public void colSm4(Closure body) { 88 | bootstrapCol([:], 4, Bootstrap4Size.SM, body) 89 | } 90 | 91 | public void colSm4(Map map, Closure body) { 92 | bootstrapCol(map, 4, Bootstrap4Size.SM, body) 93 | } 94 | 95 | public void colSm5(Closure body) { 96 | bootstrapCol([:], 5, Bootstrap4Size.SM, body) 97 | } 98 | 99 | public void colSm5(Map map, Closure body) { 100 | bootstrapCol(map, 5, Bootstrap4Size.SM, body) 101 | } 102 | 103 | public void colSm6(Closure body) { 104 | bootstrapCol([:], 6, Bootstrap4Size.SM, body) 105 | } 106 | 107 | public void colSm6(Map map, Closure body) { 108 | bootstrapCol(map, 6, Bootstrap4Size.SM, body) 109 | } 110 | 111 | public void colSm7(Closure body) { 112 | bootstrapCol([:], 7, Bootstrap4Size.SM, body) 113 | } 114 | 115 | public void colSm7(Map map, Closure body) { 116 | bootstrapCol(map, 7, Bootstrap4Size.SM, body) 117 | } 118 | 119 | public void colSm8(Closure body) { 120 | bootstrapCol([:], 8, Bootstrap4Size.SM, body) 121 | } 122 | 123 | public void colSm8(Map map, Closure body) { 124 | bootstrapCol(map, 8, Bootstrap4Size.SM, body) 125 | } 126 | 127 | public void colSm9(Closure body) { 128 | bootstrapCol([:], 9, Bootstrap4Size.SM, body) 129 | } 130 | 131 | public void colSm9(Map map, Closure body) { 132 | bootstrapCol(map, 9, Bootstrap4Size.SM, body) 133 | } 134 | 135 | public void colSm10(Closure body) { 136 | bootstrapCol([:], 10, Bootstrap4Size.SM, body) 137 | } 138 | 139 | public void colSm10(Map map, Closure body) { 140 | bootstrapCol(map, 10, Bootstrap4Size.SM, body) 141 | } 142 | 143 | public void colSm11(Closure body) { 144 | bootstrapCol([:], 11, Bootstrap4Size.SM, body) 145 | } 146 | 147 | public void colSm11(Map map, Closure body) { 148 | bootstrapCol(map, 11, Bootstrap4Size.SM, body) 149 | } 150 | 151 | public void colSm12(Closure body) { 152 | bootstrapCol([:], 12, Bootstrap4Size.SM, body) 153 | } 154 | 155 | public void colSm12(Map map, Closure body) { 156 | bootstrapCol(map, 12, Bootstrap4Size.SM, body) 157 | } 158 | 159 | public void colMd1(Closure body) { 160 | bootstrapCol([:], 1, Bootstrap4Size.MD, body) 161 | } 162 | 163 | public void colMd1(Map map, Closure body) { 164 | bootstrapCol(map, 1, Bootstrap4Size.MD, body) 165 | } 166 | 167 | public void colMd2(Closure body) { 168 | bootstrapCol([:], 2, Bootstrap4Size.MD, body) 169 | } 170 | 171 | public void colMd2(Map map, Closure body) { 172 | bootstrapCol(map, 2, Bootstrap4Size.MD, body) 173 | } 174 | 175 | public void colMd3(Closure body) { 176 | bootstrapCol([:], 3, Bootstrap4Size.MD, body) 177 | } 178 | 179 | public void colMd3(Map map, Closure body) { 180 | bootstrapCol(map, 3, Bootstrap4Size.MD, body) 181 | } 182 | 183 | public void colMd4(Closure body) { 184 | bootstrapCol([:], 4, Bootstrap4Size.MD, body) 185 | } 186 | 187 | public void colMd4(Map map, Closure body) { 188 | bootstrapCol(map, 4, Bootstrap4Size.MD, body) 189 | } 190 | 191 | public void colMd5(Closure body) { 192 | bootstrapCol([:], 5, Bootstrap4Size.MD, body) 193 | } 194 | 195 | public void colMd5(Map map, Closure body) { 196 | bootstrapCol(map, 5, Bootstrap4Size.MD, body) 197 | } 198 | 199 | public void colMd6(Closure body) { 200 | bootstrapCol([:], 6, Bootstrap4Size.MD, body) 201 | } 202 | 203 | public void colMd6(Map map, Closure body) { 204 | bootstrapCol(map, 6, Bootstrap4Size.MD, body) 205 | } 206 | 207 | public void colMd7(Closure body) { 208 | bootstrapCol([:], 7, Bootstrap4Size.MD, body) 209 | } 210 | 211 | public void colMd7(Map map, Closure body) { 212 | bootstrapCol(map, 7, Bootstrap4Size.MD, body) 213 | } 214 | 215 | public void colMd8(Closure body) { 216 | bootstrapCol([:], 8, Bootstrap4Size.MD, body) 217 | } 218 | 219 | public void colMd8(Map map, Closure body) { 220 | bootstrapCol(map, 8, Bootstrap4Size.MD, body) 221 | } 222 | 223 | public void colMd9(Closure body) { 224 | bootstrapCol([:], 9, Bootstrap4Size.MD, body) 225 | } 226 | 227 | public void colMd9(Map map, Closure body) { 228 | bootstrapCol(map, 9, Bootstrap4Size.MD, body) 229 | } 230 | 231 | public void colMd10(Closure body) { 232 | bootstrapCol([:], 10, Bootstrap4Size.MD, body) 233 | } 234 | 235 | public void colMd10(Map map, Closure body) { 236 | bootstrapCol(map, 10, Bootstrap4Size.MD, body) 237 | } 238 | 239 | public void colMd11(Closure body) { 240 | bootstrapCol([:], 11, Bootstrap4Size.MD, body) 241 | } 242 | 243 | public void colMd11(Map map, Closure body) { 244 | bootstrapCol(map, 11, Bootstrap4Size.MD, body) 245 | } 246 | 247 | public void colMd12(Closure body) { 248 | bootstrapCol([:], 12, Bootstrap4Size.MD, body) 249 | } 250 | 251 | public void colMd12(Map map, Closure body) { 252 | bootstrapCol(map, 12, Bootstrap4Size.MD, body) 253 | } 254 | 255 | public void colLg1(Closure body) { 256 | bootstrapCol([:], 1, Bootstrap4Size.LG, body) 257 | } 258 | 259 | public void colLg1(Map map, Closure body) { 260 | bootstrapCol(map, 1, Bootstrap4Size.LG, body) 261 | } 262 | 263 | public void colLg2(Closure body) { 264 | bootstrapCol([:], 2, Bootstrap4Size.LG, body) 265 | } 266 | 267 | public void colLg2(Map map, Closure body) { 268 | bootstrapCol(map, 2, Bootstrap4Size.LG, body) 269 | } 270 | 271 | public void colLg3(Closure body) { 272 | bootstrapCol([:], 3, Bootstrap4Size.LG, body) 273 | } 274 | 275 | public void colLg3(Map map, Closure body) { 276 | bootstrapCol(map, 3, Bootstrap4Size.LG, body) 277 | } 278 | 279 | public void colLg4(Closure body) { 280 | bootstrapCol([:], 4, Bootstrap4Size.LG, body) 281 | } 282 | 283 | public void colLg4(Map map, Closure body) { 284 | bootstrapCol(map, 4, Bootstrap4Size.LG, body) 285 | } 286 | 287 | public void colLg5(Closure body) { 288 | bootstrapCol([:], 5, Bootstrap4Size.LG, body) 289 | } 290 | 291 | public void colLg5(Map map, Closure body) { 292 | bootstrapCol(map, 5, Bootstrap4Size.LG, body) 293 | } 294 | 295 | public void colLg6(Closure body) { 296 | bootstrapCol([:], 6, Bootstrap4Size.LG, body) 297 | } 298 | 299 | public void colLg6(Map map, Closure body) { 300 | bootstrapCol(map, 6, Bootstrap4Size.LG, body) 301 | } 302 | 303 | public void colLg7(Closure body) { 304 | bootstrapCol([:], 7, Bootstrap4Size.LG, body) 305 | } 306 | 307 | public void colLg7(Map map, Closure body) { 308 | bootstrapCol(map, 7, Bootstrap4Size.LG, body) 309 | } 310 | 311 | public void colLg8(Closure body) { 312 | bootstrapCol([:], 8, Bootstrap4Size.LG, body) 313 | } 314 | 315 | public void colLg8(Map map, Closure body) { 316 | bootstrapCol(map, 8, Bootstrap4Size.LG, body) 317 | } 318 | 319 | public void colLg9(Closure body) { 320 | bootstrapCol([:], 9, Bootstrap4Size.LG, body) 321 | } 322 | 323 | public void colLg9(Map map, Closure body) { 324 | bootstrapCol(map, 9, Bootstrap4Size.LG, body) 325 | } 326 | 327 | public void colLg10(Closure body) { 328 | bootstrapCol([:], 10, Bootstrap4Size.LG, body) 329 | } 330 | 331 | public void colLg10(Map map, Closure body) { 332 | bootstrapCol(map, 10, Bootstrap4Size.LG, body) 333 | } 334 | 335 | public void colLg11(Closure body) { 336 | bootstrapCol([:], 11, Bootstrap4Size.LG, body) 337 | } 338 | 339 | public void colLg11(Map map, Closure body) { 340 | bootstrapCol(map, 11, Bootstrap4Size.LG, body) 341 | } 342 | 343 | public void colLg12(Closure body) { 344 | bootstrapCol([:], 12, Bootstrap4Size.LG, body) 345 | } 346 | 347 | public void colLg12(Map map, Closure body) { 348 | bootstrapCol(map, 12, Bootstrap4Size.LG, body) 349 | } 350 | 351 | public void colXl1(Closure body) { 352 | bootstrapCol([:], 1, Bootstrap4Size.XL, body) 353 | } 354 | 355 | public void colXl1(Map map, Closure body) { 356 | bootstrapCol(map, 1, Bootstrap4Size.XL, body) 357 | } 358 | 359 | public void colXl2(Closure body) { 360 | bootstrapCol([:], 2, Bootstrap4Size.XL, body) 361 | } 362 | 363 | public void colXl2(Map map, Closure body) { 364 | bootstrapCol(map, 2, Bootstrap4Size.XL, body) 365 | } 366 | 367 | public void colXl3(Closure body) { 368 | bootstrapCol([:], 3, Bootstrap4Size.XL, body) 369 | } 370 | 371 | public void colXl3(Map map, Closure body) { 372 | bootstrapCol(map, 3, Bootstrap4Size.XL, body) 373 | } 374 | 375 | public void colXl4(Closure body) { 376 | bootstrapCol([:], 4, Bootstrap4Size.XL, body) 377 | } 378 | 379 | public void colXl4(Map map, Closure body) { 380 | bootstrapCol(map, 4, Bootstrap4Size.XL, body) 381 | } 382 | 383 | public void colXl5(Closure body) { 384 | bootstrapCol([:], 5, Bootstrap4Size.XL, body) 385 | } 386 | 387 | public void colXl5(Map map, Closure body) { 388 | bootstrapCol(map, 5, Bootstrap4Size.XL, body) 389 | } 390 | 391 | public void colXl6(Closure body) { 392 | bootstrapCol([:], 6, Bootstrap4Size.XL, body) 393 | } 394 | 395 | public void colXl6(Map map, Closure body) { 396 | bootstrapCol(map, 6, Bootstrap4Size.XL, body) 397 | } 398 | 399 | public void colXl7(Closure body) { 400 | bootstrapCol([:], 7, Bootstrap4Size.XL, body) 401 | } 402 | 403 | public void colXl7(Map map, Closure body) { 404 | bootstrapCol(map, 7, Bootstrap4Size.XL, body) 405 | } 406 | 407 | public void colXl8(Closure body) { 408 | bootstrapCol([:], 8, Bootstrap4Size.XL, body) 409 | } 410 | 411 | public void colXl8(Map map, Closure body) { 412 | bootstrapCol(map, 8, Bootstrap4Size.XL, body) 413 | } 414 | 415 | public void colXl9(Closure body) { 416 | bootstrapCol([:], 9, Bootstrap4Size.XL, body) 417 | } 418 | 419 | public void colXl9(Map map, Closure body) { 420 | bootstrapCol(map, 9, Bootstrap4Size.XL, body) 421 | } 422 | 423 | public void colXl10(Closure body) { 424 | bootstrapCol([:], 10, Bootstrap4Size.XL, body) 425 | } 426 | 427 | public void colXl10(Map map, Closure body) { 428 | bootstrapCol(map, 10, Bootstrap4Size.XL, body) 429 | } 430 | 431 | public void colXl11(Closure body) { 432 | bootstrapCol([:], 11, Bootstrap4Size.XL, body) 433 | } 434 | 435 | public void colXl11(Map map, Closure body) { 436 | bootstrapCol(map, 11, Bootstrap4Size.XL, body) 437 | } 438 | 439 | public void colXl12(Closure body) { 440 | bootstrapCol([:], 12, Bootstrap4Size.XL, body) 441 | } 442 | 443 | public void colXl12(Map map, Closure body) { 444 | bootstrapCol(map, 12, Bootstrap4Size.XL, body) 445 | } 446 | } 447 | -------------------------------------------------------------------------------- /bootstrap/src/acavailhez/html/bootstrap/Bootstrap4ModalTrait.groovy: -------------------------------------------------------------------------------- 1 | package acavailhez.html.bootstrap 2 | 3 | import acavailhez.html.internal.HtmlEngine 4 | import acavailhez.html.traits.Html5Trait 5 | import acavailhez.html.utils.HtmlAttributes 6 | import groovy.transform.CompileStatic 7 | 8 | // shortcuts for bootstrap 4 modals 9 | // http://v4-alpha.getbootstrap.com/components/modal/ 10 | @CompileStatic 11 | trait Bootstrap4ModalTrait extends Html5Trait { 12 | 13 | // shortcut 14 | void modal(Map attrs, 15 | Closure body) { 16 | modal(attrs, null, body, null) 17 | } 18 | 19 | // shortcut 20 | void modal(Map attrs, 21 | Closure body, 22 | Closure footer) { 23 | modal(attrs, null, body, footer) 24 | } 25 | 26 | // A bootstrap modal with basic options 27 | // if header is null, a default header will be added 28 | // if footer is null, no footer will be added 29 | // Available attributes: 30 | // title, String: if present and header is null, the modal will have a head with this string (escaped) 31 | // tabindex, int: if present will set the tab-index 32 | // closeLabel, String: the label of the close cross 33 | void modal(Map map, 34 | Closure header, 35 | Closure body, 36 | Closure footer) { 37 | 38 | HtmlAttributes attrs = HtmlAttributes.wrap(map) 39 | 40 | String title = attrs.opt('title', String) 41 | attrs.remove('title') 42 | 43 | String close = attrs.opt('closeLabel', String) 44 | attrs.remove('closeLabel') 45 | 46 | attrs.addToClass('modal fade') 47 | attrs.put('role', 'dialog') 48 | attrs.put('tabindex', attrs.opt('tabindex', Integer, -1)) 49 | 50 | // speed up and go around groovy trait bug (https://issues.apache.org/jira/browse/GROOVY-7843) 51 | HtmlEngine.openTag(html.stringBuilder, 'div', attrs) 52 | HtmlEngine.openTag(html.stringBuilder, 'div', [class: 'modal-dialog']) 53 | HtmlEngine.openTag(html.stringBuilder, 'div', [class: 'modal-content']) 54 | 55 | div(class: 'modal-header') { 56 | if (header) { 57 | header() 58 | } else { 59 | // default header 60 | button(type: 'button', class: 'close', 'data-dismiss': 'modal', 'aria-label': close) { 61 | html << '' 62 | } 63 | if (title && !title.isEmpty()) { 64 | h4(class: 'modal-title') { 65 | escape << title 66 | } 67 | } 68 | 69 | } 70 | } 71 | 72 | div(class: 'modal-body') { 73 | body() 74 | } 75 | 76 | if (footer) { 77 | div(class: 'modal-footer') { 78 | footer() 79 | } 80 | } 81 | HtmlEngine.closeTag(html.stringBuilder, 'div') 82 | HtmlEngine.closeTag(html.stringBuilder, 'div') 83 | HtmlEngine.closeTag(html.stringBuilder, 'div') 84 | } 85 | 86 | // when applied to , will open a modal 87 | HtmlAttributes modal(Map map) { 88 | HtmlAttributes attrs = HtmlAttributes.wrap(map) 89 | 90 | String modalId = attrs.get('modal', String) 91 | attrs.remove('modal') 92 | attrs.put('data-toggle', 'modal') 93 | attrs.put('data-target', '#' + modalId) 94 | 95 | return attrs 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /bootstrap/src/acavailhez/html/bootstrap/Bootstrap4NavTrait.groovy: -------------------------------------------------------------------------------- 1 | package acavailhez.html.bootstrap 2 | 3 | import acavailhez.html.traits.CaptureTrait 4 | import acavailhez.html.traits.Html5Trait 5 | import acavailhez.html.utils.HtmlAttributes 6 | import groovy.transform.CompileStatic 7 | 8 | // shortcuts for bootstrap 4 navs 9 | // http://v4-alpha.getbootstrap.com/components/navs 10 | // http://v4-alpha.getbootstrap.com/components/navbar 11 | @CompileStatic 12 | trait Bootstrap4NavTrait implements Html5Trait, CaptureTrait { 13 | 14 | void navbar(Closure body) { 15 | navbar([:], body) 16 | } 17 | 18 | void navbar(Map map, Closure body) { 19 | HtmlAttributes attrs = HtmlAttributes.copy(map) 20 | nav(attrs.addToClass('navbar navbar-light')) { 21 | scope.put('navbar', true) 22 | body() 23 | } 24 | } 25 | 26 | // Navbar 27 | void brand(String brandName) { 28 | brand([:]) { 29 | escape << brandName 30 | } 31 | } 32 | 33 | void brand(Map map, Closure body) { 34 | a(HtmlAttributes.copy(map).addToClass('navbar-brand'), body) 35 | } 36 | 37 | //