Home | History | Annotate | Download | only in writer
      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
     18 
     19 import lexer.Token
     20 import parser.files.AbstractFileParser
     21 import java.io.File
     22 import java.nio.file.Path
     23 import java.nio.file.Paths
     24 
     25 fun warn(msg: String) {
     26     System.err.println("WARNING: $msg")
     27 }
     28 
     29 /**
     30  * Get values of tokens, useful for debugging.
     31  */
     32 fun tokenValues(tokens: List<Token>): String {
     33     return tokens.map { it.value }.joinToString("|")
     34 }
     35 
     36 /**
     37  * Escape string for HTML.
     38  */
     39 fun htmlEscape(string: String): String {
     40     val out = StringBuilder(Math.max(16, string.length))
     41     string.toCharArray().forEach { c ->
     42         if (c.toInt() > 127 || c == '"' || c == '<' || c == '>' || c == '&' || c == '$' || c == '{' || c == '}') {
     43             out.append("&#")
     44             out.append(c.toInt())
     45             out.append(';')
     46         } else {
     47             out.append(c)
     48         }
     49     }
     50     return out.toString()
     51 }
     52 
     53 /**
     54  * Used to display description text.
     55  */
     56 fun formatTextasHTML(string: String, useParagraphs: Boolean = true): String {
     57     if (string.isEmpty()) return string
     58 
     59     val sb = StringBuilder()
     60     if (useParagraphs) sb.append("<p>")
     61     //match and replace empty lines
     62     val replaceText = if (useParagraphs) "</p>\n<p>" else "<br>\n"
     63     sb.append(htmlEscape(string.trim()).replace(Regex("\\s*\n\n\\s*"), replaceText))
     64     if (useParagraphs) sb.append("</p>")
     65     return sb.toString()
     66 }
     67 
     68 private val summaryRegex = Regex("\\.|\n\n") //match period or empty line
     69 
     70 /**
     71  * Given a block of description text, return the first sentence.
     72  */
     73 fun getDescSummaryText(string: String): String {
     74     return if (string.isEmpty()) {
     75         string
     76     } else {
     77         val s = string.trimStart() // remove any beginning empty lines/whitespace
     78         val sb = StringBuilder(summaryRegex.split(s)[0])
     79         if (sb[sb.length - 1] != '.') sb.append(".") // add period, if needed
     80         formatTextasHTML(sb.toString())
     81     }
     82 }
     83 
     84 /**
     85  * Return the out file path for a given parser.
     86  */
     87 fun getOutPath(parser: AbstractFileParser, outDir: Path): Path {
     88     val pkgPath = parser.packageName.replace(".", File.separator)
     89     val dirPath = "${outDir}${File.separator}${pkgPath}${File.separator}${parser.packageVersion}"
     90     return Paths.get("${dirPath}${File.separator}${parser.name}.html")
     91 }