Home | History | Annotate | Download | only in elements
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package writer.elements
     18 
     19 import parser.elements.EntryParser
     20 import parser.elements.declarations.AbstractDeclarationParser
     21 import writer.formatTextasHTML
     22 import writer.htmlEscape
     23 
     24 abstract class AbstractElement(parser: EntryParser) {
     25     protected val docParser = parser.docParser
     26     protected val annotationParsers = parser.annotationParsers
     27 
     28     abstract protected val declarationParser: AbstractDeclarationParser //specify in subclass
     29     abstract fun declaration(): String
     30     abstract fun detailsRows(): String
     31 
     32     open fun toHTML(): String {
     33         return """
     34 <div>
     35   <h3>${declarationParser.name}</h3>
     36   <pre class="prettyprint devsite-disable-click-to-copy">${declaration()}</pre>
     37   <div>
     38     ${formatTextasHTML(docParser.description)}
     39   </div>
     40   <table class="details responsive">
     41     <thead>
     42       <th colspan="2">Details</th>
     43     </thead>
     44     <tbody>
     45       ${detailsRows()}
     46       ${annotationRows()}
     47     </tbody>
     48   </table>
     49 </div>
     50 """.trim()
     51     }
     52 
     53     private fun annotationRows(): String {
     54         val sb = StringBuilder()
     55         if (annotationParsers.isNotEmpty()) {
     56             sb.append("""
     57             <tr>
     58               <td>Annotations</td>
     59               <td>
     60                 <table class="function param responsive">
     61                   <tbody>""")
     62             //AnnotationParser => name:TokenGrammar, value:String
     63             annotationParsers.forEach { arg ->
     64                 sb.append("""
     65                 <tr>
     66                   <td>
     67                     <code>${arg.name.value}</code>
     68                   </td>
     69                   <td>
     70                     <div>${htmlEscape(arg.value)}</div>
     71                   </td>
     72                 </tr>""")
     73             }
     74             sb.append("""
     75                   </tbody>
     76                 </table>
     77               </td>
     78             </tr>""")
     79         }
     80         return sb.toString().trim()
     81     }
     82 }