libutils.ftl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. [#ftl]
  2. [#--
  3. ChibiOS/RT - Copyright (C) 2006..2018 Giovanni Di Sirio
  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. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. --]
  14. [#--
  15. -- Returns the trimmed text "s" making sure it is terminated by a dot.
  16. -- The empty string is always returned as an empty string, the dot is not
  17. -- added.
  18. --]
  19. [#function WithDot s]
  20. [#local s = s?trim /]
  21. [#if s == ""]
  22. [#return s /]
  23. [/#if]
  24. [#if s?ends_with(".")]
  25. [#return s /]
  26. [/#if]
  27. [#return s + "." /]
  28. [/#function]
  29. [#--
  30. -- Returns the trimmed text "s" making sure it is not terminated by a dot.
  31. --]
  32. [#function WithoutDot s]
  33. [#local s = s?trim /]
  34. [#if s?ends_with(".")]
  35. [#return s?substring(0, s?length - 1) /]
  36. [/#if]
  37. [#return s /]
  38. [/#function]
  39. [#--
  40. -- Returns the trimmed text "s" making sure it is terminated by a dot if the
  41. -- text is composed of multiple phrases, if the text is composed of a single
  42. -- phrase then makes sure it is *not* terminated by a dot.
  43. -- A phrase is recognized by the pattern ". " into the text.
  44. -- The empty string is always returned as an empty string, the dot is never
  45. -- added.
  46. --]
  47. [#function IntelligentDot s]
  48. [#local s = s?trim /]
  49. [#if s?contains(". ")]
  50. [#return WithDot(s) /]
  51. [/#if]
  52. [#return WithoutDot(s) /]
  53. [/#function]
  54. [#--
  55. -- Formats a text string in a sequence of strings no longer than "len" (first
  56. -- line) or "lenn" (subsequent lines).
  57. -- White spaces are normalized between words, sequences of white spaces become
  58. -- a single space.
  59. --]
  60. [#function StringToText len1 lenn s]
  61. [#local words=s?word_list /]
  62. [#local line="" /]
  63. [#local lines=[] /]
  64. [#list words as word]
  65. [#if lines?size == 0]
  66. [#local len = len1 /]
  67. [#else]
  68. [#local len = lenn /]
  69. [/#if]
  70. [#if (line?length + word?length + 1 > len)]
  71. [#local lines = lines + [line?trim] /]
  72. [#local line = word + " " /]
  73. [#else]
  74. [#local line = line + word + " " /]
  75. [/#if]
  76. [/#list]
  77. [#if line != ""]
  78. [#local lines = lines + [line?trim] /]
  79. [/#if]
  80. [#return lines /]
  81. [/#function]
  82. [#--
  83. -- Emits a string "s" as a formatted text, the first line is prefixed by the
  84. -- "p1" parameter, subsequent lines are prefixed by the "pn" paramenter.
  85. -- Emitted lines are no longer than the "len" parameter.
  86. -- White spaces are normalized between words.
  87. --]
  88. [#macro FormatStringAsText p1 pn s len]
  89. [#local lines = StringToText(len - p1?length, len - pn?length, s) /]
  90. [#list lines as line]
  91. [#if line_index == 0]
  92. ${p1}${line}
  93. [#else]
  94. ${pn}${line}
  95. [/#if]
  96. [/#list]
  97. [/#macro]
  98. [#--
  99. -- Emits a C function body code reformatting the indentation using the
  100. -- specified tab size and line prefix.
  101. --]
  102. [#macro EmitIndentedCCode start tab ccode]
  103. [#assign lines = ccode?string?split("^", "rm") /]
  104. [#list lines as line]
  105. [#if (line_index > 0) || (line?trim?length > 0)]
  106. [#if line?trim?length > 0]
  107. [#if line[0] == "#"]
  108. ${line?chop_linebreak}
  109. [#else]
  110. ${start + line?chop_linebreak}
  111. [/#if]
  112. [#else]
  113. [/#if]
  114. [/#if]
  115. [/#list]
  116. [/#macro]