lounge_lizard 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (348) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +33 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +50 -0
  10. data/Rakefile +43 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/circle.yml +14 -0
  14. data/ext/drafter/CHANGELOG.md +278 -0
  15. data/ext/drafter/Dockerfile +17 -0
  16. data/ext/drafter/Makefile +62 -0
  17. data/ext/drafter/appveyor.yml +17 -0
  18. data/ext/drafter/bin/drafter +0 -0
  19. data/ext/drafter/build/Makefile +387 -0
  20. data/ext/drafter/build/drafter.Makefile +6 -0
  21. data/ext/drafter/build/drafter.target.mk +159 -0
  22. data/ext/drafter/build/ext/snowcrash/libmarkdownparser.target.mk +141 -0
  23. data/ext/drafter/build/ext/snowcrash/libsnowcrash.target.mk +154 -0
  24. data/ext/drafter/build/ext/snowcrash/libsundown.target.mk +149 -0
  25. data/ext/drafter/build/ext/snowcrash/perf-libsnowcrash.target.mk +147 -0
  26. data/ext/drafter/build/ext/snowcrash/snowcrash.Makefile +6 -0
  27. data/ext/drafter/build/gyp-mac-tool +606 -0
  28. data/ext/drafter/build/libdrafter.target.mk +186 -0
  29. data/ext/drafter/build/libsos.target.mk +137 -0
  30. data/ext/drafter/build/out/Release/drafter +0 -0
  31. data/ext/drafter/build/out/Release/libdrafter.dylib +0 -0
  32. data/ext/drafter/build/out/Release/libmarkdownparser.a +0 -0
  33. data/ext/drafter/build/out/Release/libsnowcrash.a +0 -0
  34. data/ext/drafter/build/out/Release/libsos.a +0 -0
  35. data/ext/drafter/build/out/Release/libsundown.a +0 -0
  36. data/ext/drafter/build/out/Release/obj.target/drafter/src/config.o +0 -0
  37. data/ext/drafter/build/out/Release/obj.target/drafter/src/main.o +0 -0
  38. data/ext/drafter/build/out/Release/obj.target/drafter/src/reporting.o +0 -0
  39. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/ConversionContext.o +0 -0
  40. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/NamedTypesRegistry.o +0 -0
  41. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/RefractAPI.o +0 -0
  42. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/RefractDataStructure.o +0 -0
  43. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/RefractElementFactory.o +0 -0
  44. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/RefractSourceMap.o +0 -0
  45. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/Render.o +0 -0
  46. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/Serialize.o +0 -0
  47. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/SerializeAST.o +0 -0
  48. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/SerializeResult.o +0 -0
  49. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/SerializeSourcemap.o +0 -0
  50. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/drafter.o +0 -0
  51. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/drafter_private.o +0 -0
  52. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/ComparableVisitor.o +0 -0
  53. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/Element.o +0 -0
  54. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/ExpandVisitor.o +0 -0
  55. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/IsExpandableVisitor.o +0 -0
  56. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/JSONSchemaVisitor.o +0 -0
  57. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/PrintVisitor.o +0 -0
  58. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/Query.o +0 -0
  59. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/Registry.o +0 -0
  60. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/RenderJSONVisitor.o +0 -0
  61. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/SerializeCompactVisitor.o +0 -0
  62. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/SerializeVisitor.o +0 -0
  63. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/TypeQueryVisitor.o +0 -0
  64. data/ext/drafter/build/out/Release/obj.target/libdrafter/src/refract/VisitorUtils.o +0 -0
  65. data/ext/drafter/build/out/Release/obj.target/libmarkdownparser/ext/snowcrash/ext/markdown-parser/src/ByteBuffer.o +0 -0
  66. data/ext/drafter/build/out/Release/obj.target/libmarkdownparser/ext/snowcrash/ext/markdown-parser/src/MarkdownNode.o +0 -0
  67. data/ext/drafter/build/out/Release/obj.target/libmarkdownparser/ext/snowcrash/ext/markdown-parser/src/MarkdownParser.o +0 -0
  68. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/Blueprint.o +0 -0
  69. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/BlueprintSourcemap.o +0 -0
  70. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/HTTP.o +0 -0
  71. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/HeadersParser.o +0 -0
  72. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/MSON.o +0 -0
  73. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/MSONOneOfParser.o +0 -0
  74. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/MSONSourcemap.o +0 -0
  75. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/MSONTypeSectionParser.o +0 -0
  76. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/MSONValueMemberParser.o +0 -0
  77. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/Section.o +0 -0
  78. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/Signature.o +0 -0
  79. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/UriTemplateParser.o +0 -0
  80. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/posix/RegexMatch.o +0 -0
  81. data/ext/drafter/build/out/Release/obj.target/libsnowcrash/ext/snowcrash/src/snowcrash.o +0 -0
  82. data/ext/drafter/build/out/Release/obj.target/libsos/ext/sos/src/sos.o +0 -0
  83. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/html/houdini_href_e.o +0 -0
  84. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/html/houdini_html_e.o +0 -0
  85. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/html/html.o +0 -0
  86. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/html/html_smartypants.o +0 -0
  87. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/src/autolink.o +0 -0
  88. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/src/buffer.o +0 -0
  89. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/src/markdown.o +0 -0
  90. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/src/src_map.o +0 -0
  91. data/ext/drafter/build/out/Release/obj.target/libsundown/ext/snowcrash/ext/markdown-parser/ext/sundown/src/stack.o +0 -0
  92. data/ext/drafter/circle.yml +32 -0
  93. data/ext/drafter/config.gypi +10 -0
  94. data/ext/drafter/config.mk +5 -0
  95. data/ext/drafter/configure +224 -0
  96. data/ext/drafter/drafter.gyp +189 -0
  97. data/ext/drafter/drafter.xcworkspace/contents.xcworkspacedata +13 -0
  98. data/ext/drafter/ext/snowcrash/Makefile +58 -0
  99. data/ext/drafter/ext/snowcrash/appveyor.yml +7 -0
  100. data/ext/drafter/ext/snowcrash/common.gypi +165 -0
  101. data/ext/drafter/ext/snowcrash/configure +197 -0
  102. data/ext/drafter/ext/snowcrash/ext/markdown-parser/Makefile +90 -0
  103. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/CONTRIBUTING.md +10 -0
  104. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/Makefile +84 -0
  105. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/Makefile.win +33 -0
  106. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/examples/smartypants.c +72 -0
  107. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/examples/sundown.c +80 -0
  108. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html/houdini.h +37 -0
  109. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html/houdini_href_e.c +108 -0
  110. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html/houdini_html_e.c +84 -0
  111. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html/html.c +647 -0
  112. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html/html.h +77 -0
  113. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html/html_smartypants.c +389 -0
  114. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/html_block_names.txt +25 -0
  115. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/autolink.c +297 -0
  116. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/autolink.h +51 -0
  117. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/buffer.c +225 -0
  118. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/buffer.h +96 -0
  119. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/html_blocks.h +206 -0
  120. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/markdown.c +2726 -0
  121. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/markdown.h +147 -0
  122. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/src_map.c +204 -0
  123. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/src_map.h +58 -0
  124. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/stack.c +81 -0
  125. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/src/stack.h +29 -0
  126. data/ext/drafter/ext/snowcrash/ext/markdown-parser/ext/sundown/sundown.def +20 -0
  127. data/ext/drafter/ext/snowcrash/ext/markdown-parser/msvc/markdown/markdown.vcproj +188 -0
  128. data/ext/drafter/ext/snowcrash/ext/markdown-parser/msvc/msvc.sln +38 -0
  129. data/ext/drafter/ext/snowcrash/ext/markdown-parser/msvc/sundown/sundown.vcproj +206 -0
  130. data/ext/drafter/ext/snowcrash/ext/markdown-parser/src/ByteBuffer.cc +160 -0
  131. data/ext/drafter/ext/snowcrash/ext/markdown-parser/src/ByteBuffer.h +90 -0
  132. data/ext/drafter/ext/snowcrash/ext/markdown-parser/src/MarkdownNode.cc +152 -0
  133. data/ext/drafter/ext/snowcrash/ext/markdown-parser/src/MarkdownNode.h +103 -0
  134. data/ext/drafter/ext/snowcrash/ext/markdown-parser/src/MarkdownParser.cc +388 -0
  135. data/ext/drafter/ext/snowcrash/ext/markdown-parser/src/MarkdownParser.h +106 -0
  136. data/ext/drafter/ext/snowcrash/snowcrash.gyp +196 -0
  137. data/ext/drafter/ext/snowcrash/src/ActionParser.h +560 -0
  138. data/ext/drafter/ext/snowcrash/src/AssetParser.h +123 -0
  139. data/ext/drafter/ext/snowcrash/src/AttributesParser.h +123 -0
  140. data/ext/drafter/ext/snowcrash/src/Blueprint.cc +90 -0
  141. data/ext/drafter/ext/snowcrash/src/Blueprint.h +489 -0
  142. data/ext/drafter/ext/snowcrash/src/BlueprintParser.h +845 -0
  143. data/ext/drafter/ext/snowcrash/src/BlueprintSourcemap.cc +81 -0
  144. data/ext/drafter/ext/snowcrash/src/BlueprintSourcemap.h +345 -0
  145. data/ext/drafter/ext/snowcrash/src/BlueprintUtility.h +111 -0
  146. data/ext/drafter/ext/snowcrash/src/CodeBlockUtility.h +276 -0
  147. data/ext/drafter/ext/snowcrash/src/DataStructureGroupParser.h +157 -0
  148. data/ext/drafter/ext/snowcrash/src/HTTP.cc +49 -0
  149. data/ext/drafter/ext/snowcrash/src/HTTP.h +108 -0
  150. data/ext/drafter/ext/snowcrash/src/HeadersParser.cc +117 -0
  151. data/ext/drafter/ext/snowcrash/src/HeadersParser.h +377 -0
  152. data/ext/drafter/ext/snowcrash/src/MSON.cc +272 -0
  153. data/ext/drafter/ext/snowcrash/src/MSON.h +405 -0
  154. data/ext/drafter/ext/snowcrash/src/MSONMixinParser.h +103 -0
  155. data/ext/drafter/ext/snowcrash/src/MSONNamedTypeParser.h +135 -0
  156. data/ext/drafter/ext/snowcrash/src/MSONOneOfParser.cc +132 -0
  157. data/ext/drafter/ext/snowcrash/src/MSONOneOfParser.h +80 -0
  158. data/ext/drafter/ext/snowcrash/src/MSONParameterParser.h +166 -0
  159. data/ext/drafter/ext/snowcrash/src/MSONPropertyMemberParser.h +106 -0
  160. data/ext/drafter/ext/snowcrash/src/MSONSourcemap.cc +141 -0
  161. data/ext/drafter/ext/snowcrash/src/MSONSourcemap.h +181 -0
  162. data/ext/drafter/ext/snowcrash/src/MSONTypeSectionParser.cc +209 -0
  163. data/ext/drafter/ext/snowcrash/src/MSONTypeSectionParser.h +213 -0
  164. data/ext/drafter/ext/snowcrash/src/MSONUtility.h +506 -0
  165. data/ext/drafter/ext/snowcrash/src/MSONValueMemberParser.cc +214 -0
  166. data/ext/drafter/ext/snowcrash/src/MSONValueMemberParser.h +390 -0
  167. data/ext/drafter/ext/snowcrash/src/ModelTable.h +87 -0
  168. data/ext/drafter/ext/snowcrash/src/ParameterParser.h +516 -0
  169. data/ext/drafter/ext/snowcrash/src/ParametersParser.h +222 -0
  170. data/ext/drafter/ext/snowcrash/src/PayloadParser.h +733 -0
  171. data/ext/drafter/ext/snowcrash/src/Platform.h +33 -0
  172. data/ext/drafter/ext/snowcrash/src/RegexMatch.h +32 -0
  173. data/ext/drafter/ext/snowcrash/src/RelationParser.h +87 -0
  174. data/ext/drafter/ext/snowcrash/src/ResourceGroupParser.h +297 -0
  175. data/ext/drafter/ext/snowcrash/src/ResourceParser.h +536 -0
  176. data/ext/drafter/ext/snowcrash/src/Section.cc +48 -0
  177. data/ext/drafter/ext/snowcrash/src/Section.h +60 -0
  178. data/ext/drafter/ext/snowcrash/src/SectionParser.h +246 -0
  179. data/ext/drafter/ext/snowcrash/src/SectionParserData.h +109 -0
  180. data/ext/drafter/ext/snowcrash/src/SectionProcessor.h +299 -0
  181. data/ext/drafter/ext/snowcrash/src/Signature.cc +75 -0
  182. data/ext/drafter/ext/snowcrash/src/Signature.h +103 -0
  183. data/ext/drafter/ext/snowcrash/src/SignatureSectionProcessor.h +442 -0
  184. data/ext/drafter/ext/snowcrash/src/SourceAnnotation.h +166 -0
  185. data/ext/drafter/ext/snowcrash/src/StringUtility.h +323 -0
  186. data/ext/drafter/ext/snowcrash/src/UriTemplateParser.cc +195 -0
  187. data/ext/drafter/ext/snowcrash/src/UriTemplateParser.h +240 -0
  188. data/ext/drafter/ext/snowcrash/src/ValuesParser.h +111 -0
  189. data/ext/drafter/ext/snowcrash/src/posix/RegexMatch.cc +99 -0
  190. data/ext/drafter/ext/snowcrash/src/snowcrash.cc +90 -0
  191. data/ext/drafter/ext/snowcrash/src/snowcrash.h +44 -0
  192. data/ext/drafter/ext/snowcrash/src/win/RegexMatch.cc +78 -0
  193. data/ext/drafter/ext/snowcrash/tools/gyp/AUTHORS +12 -0
  194. data/ext/drafter/ext/snowcrash/tools/gyp/DEPS +23 -0
  195. data/ext/drafter/ext/snowcrash/tools/gyp/OWNERS +1 -0
  196. data/ext/drafter/ext/snowcrash/tools/gyp/PRESUBMIT.py +137 -0
  197. data/ext/drafter/ext/snowcrash/tools/gyp/buildbot/buildbot_run.py +136 -0
  198. data/ext/drafter/ext/snowcrash/tools/gyp/buildbot/commit_queue/OWNERS +6 -0
  199. data/ext/drafter/ext/snowcrash/tools/gyp/buildbot/commit_queue/cq_config.json +15 -0
  200. data/ext/drafter/ext/snowcrash/tools/gyp/codereview.settings +10 -0
  201. data/ext/drafter/ext/snowcrash/tools/gyp/data/win/large-pdb-shim.cc +12 -0
  202. data/ext/drafter/ext/snowcrash/tools/gyp/gyp +8 -0
  203. data/ext/drafter/ext/snowcrash/tools/gyp/gyp.bat +5 -0
  204. data/ext/drafter/ext/snowcrash/tools/gyp/gyp_main.py +16 -0
  205. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSNew.py +340 -0
  206. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSProject.py +208 -0
  207. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSSettings.py +1096 -0
  208. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSToolFile.py +58 -0
  209. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUserFile.py +147 -0
  210. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUtil.py +270 -0
  211. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUtil.pyc +0 -0
  212. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSVersion.py +445 -0
  213. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/MSVSVersion.pyc +0 -0
  214. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/__init__.py +548 -0
  215. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/__init__.pyc +0 -0
  216. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/common.py +608 -0
  217. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/common.pyc +0 -0
  218. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/easy_xml.py +157 -0
  219. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/flock_tool.py +54 -0
  220. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.py +0 -0
  221. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.pyc +0 -0
  222. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/analyzer.py +741 -0
  223. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/android.py +1069 -0
  224. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/cmake.py +1248 -0
  225. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/dump_dependency_json.py +99 -0
  226. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/eclipse.py +425 -0
  227. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypd.py +94 -0
  228. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypsh.py +56 -0
  229. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.py +2218 -0
  230. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.pyc +0 -0
  231. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/msvs.py +3467 -0
  232. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/ninja.py +2427 -0
  233. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/ninja.pyc +0 -0
  234. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.py +1300 -0
  235. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.pyc +0 -0
  236. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/input.py +2899 -0
  237. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/input.pyc +0 -0
  238. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/mac_tool.py +605 -0
  239. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/msvs_emulation.py +1093 -0
  240. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/msvs_emulation.pyc +0 -0
  241. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/ninja_syntax.py +160 -0
  242. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/ninja_syntax.pyc +0 -0
  243. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/ordered_dict.py +289 -0
  244. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/simple_copy.py +46 -0
  245. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/simple_copy.pyc +0 -0
  246. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/win_tool.py +314 -0
  247. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.py +1664 -0
  248. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.pyc +0 -0
  249. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xcode_ninja.py +276 -0
  250. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xcode_ninja.pyc +0 -0
  251. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.py +2927 -0
  252. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.pyc +0 -0
  253. data/ext/drafter/ext/snowcrash/tools/gyp/pylib/gyp/xml_fix.py +69 -0
  254. data/ext/drafter/ext/snowcrash/tools/gyp/pylintrc +307 -0
  255. data/ext/drafter/ext/snowcrash/tools/gyp/samples/samples +81 -0
  256. data/ext/drafter/ext/snowcrash/tools/gyp/samples/samples.bat +5 -0
  257. data/ext/drafter/ext/snowcrash/tools/gyp/setup.py +19 -0
  258. data/ext/drafter/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.pbfilespec +27 -0
  259. data/ext/drafter/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.xclangspec +226 -0
  260. data/ext/drafter/ext/snowcrash/tools/gyp/tools/emacs/gyp.el +275 -0
  261. data/ext/drafter/ext/snowcrash/tools/gyp/tools/graphviz.py +100 -0
  262. data/ext/drafter/ext/snowcrash/tools/gyp/tools/pretty_gyp.py +155 -0
  263. data/ext/drafter/ext/snowcrash/tools/gyp/tools/pretty_sln.py +169 -0
  264. data/ext/drafter/ext/snowcrash/tools/gyp/tools/pretty_vcproj.py +329 -0
  265. data/ext/drafter/ext/snowcrash/vcbuild.bat +139 -0
  266. data/ext/drafter/ext/sos/Makefile +62 -0
  267. data/ext/drafter/ext/sos/src/sos.cc +235 -0
  268. data/ext/drafter/ext/sos/src/sos.h +188 -0
  269. data/ext/drafter/ext/sos/src/sosJSON.h +121 -0
  270. data/ext/drafter/ext/sos/src/sosYAML.h +105 -0
  271. data/ext/drafter/src/ConversionContext.cc +39 -0
  272. data/ext/drafter/src/ConversionContext.h +34 -0
  273. data/ext/drafter/src/NamedTypesRegistry.cc +405 -0
  274. data/ext/drafter/src/NamedTypesRegistry.h +28 -0
  275. data/ext/drafter/src/NodeInfo.h +143 -0
  276. data/ext/drafter/src/RefractAPI.cc +579 -0
  277. data/ext/drafter/src/RefractAPI.h +28 -0
  278. data/ext/drafter/src/RefractDataStructure.cc +1199 -0
  279. data/ext/drafter/src/RefractDataStructure.h +26 -0
  280. data/ext/drafter/src/RefractElementFactory.cc +107 -0
  281. data/ext/drafter/src/RefractElementFactory.h +67 -0
  282. data/ext/drafter/src/RefractSourceMap.cc +29 -0
  283. data/ext/drafter/src/RefractSourceMap.h +57 -0
  284. data/ext/drafter/src/Render.cc +157 -0
  285. data/ext/drafter/src/Render.h +40 -0
  286. data/ext/drafter/src/Serialize.cc +160 -0
  287. data/ext/drafter/src/Serialize.h +289 -0
  288. data/ext/drafter/src/SerializeAST.cc +507 -0
  289. data/ext/drafter/src/SerializeAST.h +29 -0
  290. data/ext/drafter/src/SerializeResult.cc +170 -0
  291. data/ext/drafter/src/SerializeResult.h +34 -0
  292. data/ext/drafter/src/SerializeSourcemap.cc +331 -0
  293. data/ext/drafter/src/SerializeSourcemap.h +21 -0
  294. data/ext/drafter/src/Version.h +40 -0
  295. data/ext/drafter/src/config.cc +91 -0
  296. data/ext/drafter/src/config.h +38 -0
  297. data/ext/drafter/src/drafter.cc +137 -0
  298. data/ext/drafter/src/drafter.h +102 -0
  299. data/ext/drafter/src/drafter_private.cc +85 -0
  300. data/ext/drafter/src/drafter_private.h +34 -0
  301. data/ext/drafter/src/main.cc +137 -0
  302. data/ext/drafter/src/refract/AppendDecorator.h +58 -0
  303. data/ext/drafter/src/refract/Build.h +67 -0
  304. data/ext/drafter/src/refract/ComparableVisitor.cc +43 -0
  305. data/ext/drafter/src/refract/ComparableVisitor.h +62 -0
  306. data/ext/drafter/src/refract/Element.cc +409 -0
  307. data/ext/drafter/src/refract/Element.h +656 -0
  308. data/ext/drafter/src/refract/ElementFwd.h +37 -0
  309. data/ext/drafter/src/refract/ElementInserter.h +59 -0
  310. data/ext/drafter/src/refract/Exception.h +31 -0
  311. data/ext/drafter/src/refract/ExpandVisitor.cc +359 -0
  312. data/ext/drafter/src/refract/ExpandVisitor.h +58 -0
  313. data/ext/drafter/src/refract/FilterVisitor.h +52 -0
  314. data/ext/drafter/src/refract/IsExpandableVisitor.cc +140 -0
  315. data/ext/drafter/src/refract/IsExpandableVisitor.h +31 -0
  316. data/ext/drafter/src/refract/Iterate.h +160 -0
  317. data/ext/drafter/src/refract/JSONSchemaVisitor.cc +675 -0
  318. data/ext/drafter/src/refract/JSONSchemaVisitor.h +73 -0
  319. data/ext/drafter/src/refract/PrintVisitor.cc +164 -0
  320. data/ext/drafter/src/refract/PrintVisitor.h +50 -0
  321. data/ext/drafter/src/refract/Query.cc +13 -0
  322. data/ext/drafter/src/refract/Query.h +38 -0
  323. data/ext/drafter/src/refract/Registry.cc +114 -0
  324. data/ext/drafter/src/refract/Registry.h +43 -0
  325. data/ext/drafter/src/refract/RenderJSONVisitor.cc +255 -0
  326. data/ext/drafter/src/refract/RenderJSONVisitor.h +51 -0
  327. data/ext/drafter/src/refract/SerializeCompactVisitor.cc +167 -0
  328. data/ext/drafter/src/refract/SerializeCompactVisitor.h +56 -0
  329. data/ext/drafter/src/refract/SerializeVisitor.cc +214 -0
  330. data/ext/drafter/src/refract/SerializeVisitor.h +55 -0
  331. data/ext/drafter/src/refract/TypeQueryVisitor.cc +46 -0
  332. data/ext/drafter/src/refract/TypeQueryVisitor.h +110 -0
  333. data/ext/drafter/src/refract/Visitor.h +126 -0
  334. data/ext/drafter/src/refract/VisitorUtils.cc +63 -0
  335. data/ext/drafter/src/refract/VisitorUtils.h +231 -0
  336. data/ext/drafter/src/reporting.cc +263 -0
  337. data/ext/drafter/src/reporting.h +39 -0
  338. data/ext/drafter/src/stream.h +148 -0
  339. data/ext/drafter/tools/homebrew/drafter.rb +18 -0
  340. data/ext/drafter/tools/make-tarball.sh +39 -0
  341. data/ext/drafter/tools/refract-filter.py +96 -0
  342. data/ext/drafter/tools/release.sh +17 -0
  343. data/ext/drafter/vcbuild.bat +203 -0
  344. data/lib/lounge_lizard/binding.rb +29 -0
  345. data/lib/lounge_lizard/version.rb +3 -0
  346. data/lib/lounge_lizard.rb +18 -0
  347. data/lounge_lizard.gemspec +37 -0
  348. metadata +547 -0
@@ -0,0 +1,2726 @@
1
+ /* markdown.c - generic markdown parser */
2
+
3
+ /*
4
+ * Copyright (c) 2009, Natacha Porté
5
+ * Copyright (c) 2011, Vicent Marti
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+
20
+ #include "markdown.h"
21
+ #include "stack.h"
22
+ #include "src_map.h"
23
+
24
+ #include <assert.h>
25
+ #include <string.h>
26
+ #include <ctype.h>
27
+ #include <stdio.h>
28
+
29
+ #if defined(_WIN32)
30
+ #define strncasecmp _strnicmp
31
+ #endif
32
+
33
+ #define REF_TABLE_SIZE 8
34
+
35
+ #define BUFFER_BLOCK 0
36
+ #define BUFFER_SPAN 1
37
+
38
+ #define MKD_LI_END 8 /* internal list flag */
39
+
40
+ #define gperf_case_strncmp(s1, s2, n) strncasecmp(s1, s2, n)
41
+ #define GPERF_DOWNCASE 1
42
+ #define GPERF_CASE_STRNCMP 1
43
+ #include "html_blocks.h"
44
+
45
+ /***************
46
+ * LOCAL TYPES *
47
+ ***************/
48
+
49
+ /* link_ref: reference to a link */
50
+ struct link_ref {
51
+ unsigned int id;
52
+
53
+ struct buf *link;
54
+ struct buf *title;
55
+
56
+ struct link_ref *next;
57
+ };
58
+
59
+ /* char_trigger: function pointer to render active chars */
60
+ /* returns the number of chars taken care of */
61
+ /* data is the pointer of the beginning of the span */
62
+ /* offset is the number of valid chars before data */
63
+ struct sd_markdown;
64
+ typedef size_t
65
+ (*char_trigger)(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
66
+
67
+ static size_t char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
68
+ static size_t char_linebreak(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
69
+ static size_t char_codespan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
70
+ static size_t char_escape(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
71
+ static size_t char_entity(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
72
+ static size_t char_langle_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
73
+ static size_t char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
74
+ static size_t char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
75
+ static size_t char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
76
+ static size_t char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
77
+ static size_t char_superscript(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size);
78
+
79
+ enum markdown_char_t {
80
+ MD_CHAR_NONE = 0,
81
+ MD_CHAR_EMPHASIS,
82
+ MD_CHAR_CODESPAN,
83
+ MD_CHAR_LINEBREAK,
84
+ MD_CHAR_LINK,
85
+ MD_CHAR_LANGLE,
86
+ MD_CHAR_ESCAPE,
87
+ MD_CHAR_ENTITITY,
88
+ MD_CHAR_AUTOLINK_URL,
89
+ MD_CHAR_AUTOLINK_EMAIL,
90
+ MD_CHAR_AUTOLINK_WWW,
91
+ MD_CHAR_SUPERSCRIPT,
92
+ };
93
+
94
+ static char_trigger markdown_char_ptrs[] = {
95
+ NULL,
96
+ &char_emphasis,
97
+ &char_codespan,
98
+ &char_linebreak,
99
+ &char_link,
100
+ &char_langle_tag,
101
+ &char_escape,
102
+ &char_entity,
103
+ &char_autolink_url,
104
+ &char_autolink_email,
105
+ &char_autolink_www,
106
+ &char_superscript,
107
+ };
108
+
109
+ /* render • structure containing one particular render */
110
+ struct sd_markdown {
111
+ struct sd_callbacks cb;
112
+ void *opaque;
113
+
114
+ struct link_ref *refs[REF_TABLE_SIZE];
115
+ uint8_t active_char[256];
116
+ struct stack work_bufs[2];
117
+ unsigned int ext_flags;
118
+ size_t max_nesting;
119
+ int in_link_body;
120
+ };
121
+
122
+ /***************************
123
+ * HELPER FUNCTIONS *
124
+ ***************************/
125
+
126
+ static inline struct buf *
127
+ rndr_newbuf(struct sd_markdown *rndr, int type)
128
+ {
129
+ static const size_t buf_size[2] = {256, 64};
130
+ struct buf *work = NULL;
131
+ struct stack *pool = &rndr->work_bufs[type];
132
+
133
+ if (pool->size < pool->asize &&
134
+ pool->item[pool->size] != NULL) {
135
+ work = pool->item[pool->size++];
136
+ work->size = 0;
137
+ } else {
138
+ work = bufnew(buf_size[type]);
139
+ stack_push(pool, work);
140
+ }
141
+
142
+ return work;
143
+ }
144
+
145
+ static inline void
146
+ rndr_popbuf(struct sd_markdown *rndr, int type)
147
+ {
148
+ rndr->work_bufs[type].size--;
149
+ }
150
+
151
+ static void
152
+ unscape_text(struct buf *ob, struct buf *src)
153
+ {
154
+ size_t i = 0, org;
155
+ while (i < src->size) {
156
+ org = i;
157
+ while (i < src->size && src->data[i] != '\\')
158
+ i++;
159
+
160
+ if (i > org)
161
+ bufput(ob, src->data + org, i - org);
162
+
163
+ if (i + 1 >= src->size)
164
+ break;
165
+
166
+ bufputc(ob, src->data[i + 1]);
167
+ i += 2;
168
+ }
169
+ }
170
+
171
+ static unsigned int
172
+ hash_link_ref(const uint8_t *link_ref, size_t length)
173
+ {
174
+ size_t i;
175
+ unsigned int hash = 0;
176
+
177
+ for (i = 0; i < length; ++i)
178
+ hash = tolower(link_ref[i]) + (hash << 6) + (hash << 16) - hash;
179
+
180
+ return hash;
181
+ }
182
+
183
+ static struct link_ref *
184
+ add_link_ref(
185
+ struct link_ref **references,
186
+ const uint8_t *name, size_t name_size)
187
+ {
188
+ struct link_ref *ref = calloc(1, sizeof(struct link_ref));
189
+
190
+ if (!ref)
191
+ return NULL;
192
+
193
+ ref->id = hash_link_ref(name, name_size);
194
+ ref->next = references[ref->id % REF_TABLE_SIZE];
195
+
196
+ references[ref->id % REF_TABLE_SIZE] = ref;
197
+ return ref;
198
+ }
199
+
200
+ static struct link_ref *
201
+ find_link_ref(struct link_ref **references, uint8_t *name, size_t length)
202
+ {
203
+ unsigned int hash = hash_link_ref(name, length);
204
+ struct link_ref *ref = NULL;
205
+
206
+ ref = references[hash % REF_TABLE_SIZE];
207
+
208
+ while (ref != NULL) {
209
+ if (ref->id == hash)
210
+ return ref;
211
+
212
+ ref = ref->next;
213
+ }
214
+
215
+ return NULL;
216
+ }
217
+
218
+ static void
219
+ free_link_refs(struct link_ref **references)
220
+ {
221
+ size_t i;
222
+
223
+ for (i = 0; i < REF_TABLE_SIZE; ++i) {
224
+ struct link_ref *r = references[i];
225
+ struct link_ref *next;
226
+
227
+ while (r) {
228
+ next = r->next;
229
+ bufrelease(r->link);
230
+ bufrelease(r->title);
231
+ free(r);
232
+ r = next;
233
+ }
234
+ }
235
+ }
236
+
237
+ /*
238
+ * Check whether a char is a Markdown space.
239
+
240
+ * Right now we only consider spaces the actual
241
+ * space and a newline: tabs and carriage returns
242
+ * are filtered out during the preprocessing phase.
243
+ *
244
+ * If we wanted to actually be UTF-8 compliant, we
245
+ * should instead extract an Unicode codepoint from
246
+ * this character and check for space properties.
247
+ */
248
+ static inline int
249
+ _isspace(int c)
250
+ {
251
+ return c == ' ' || c == '\n';
252
+ }
253
+
254
+ /****************************
255
+ * INLINE PARSING FUNCTIONS *
256
+ ****************************/
257
+
258
+ /* is_mail_autolink • looks for the address part of a mail autolink and '>' */
259
+ /* this is less strict than the original markdown e-mail address matching */
260
+ static size_t
261
+ is_mail_autolink(uint8_t *data, size_t size)
262
+ {
263
+ size_t i = 0, nb = 0;
264
+
265
+ /* address is assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@' */
266
+ for (i = 0; i < size; ++i) {
267
+ if (isalnum(data[i]))
268
+ continue;
269
+
270
+ switch (data[i]) {
271
+ case '@':
272
+ nb++;
273
+
274
+ case '-':
275
+ case '.':
276
+ case '_':
277
+ break;
278
+
279
+ case '>':
280
+ return (nb == 1) ? i + 1 : 0;
281
+
282
+ default:
283
+ return 0;
284
+ }
285
+ }
286
+
287
+ return 0;
288
+ }
289
+
290
+ /* tag_length • returns the length of the given tag, or 0 is it's not valid */
291
+ static size_t
292
+ tag_length(uint8_t *data, size_t size, enum mkd_autolink *autolink)
293
+ {
294
+ size_t i, j;
295
+
296
+ /* a valid tag can't be shorter than 3 chars */
297
+ if (size < 3) return 0;
298
+
299
+ /* begins with a '<' optionally followed by '/', followed by letter or number */
300
+ if (data[0] != '<') return 0;
301
+ i = (data[1] == '/') ? 2 : 1;
302
+
303
+ if (!isalnum(data[i]))
304
+ return 0;
305
+
306
+ /* scheme test */
307
+ *autolink = MKDA_NOT_AUTOLINK;
308
+
309
+ /* try to find the beginning of an URI */
310
+ while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-'))
311
+ i++;
312
+
313
+ if (i > 1 && data[i] == '@') {
314
+ if ((j = is_mail_autolink(data + i, size - i)) != 0) {
315
+ *autolink = MKDA_EMAIL;
316
+ return i + j;
317
+ }
318
+ }
319
+
320
+ if (i > 2 && data[i] == ':') {
321
+ *autolink = MKDA_NORMAL;
322
+ i++;
323
+ }
324
+
325
+ /* completing autolink test: no whitespace or ' or " */
326
+ if (i >= size)
327
+ *autolink = MKDA_NOT_AUTOLINK;
328
+
329
+ else if (*autolink) {
330
+ j = i;
331
+
332
+ while (i < size) {
333
+ if (data[i] == '\\') i += 2;
334
+ else if (data[i] == '>' || data[i] == '\'' ||
335
+ data[i] == '"' || data[i] == ' ' || data[i] == '\n')
336
+ break;
337
+ else i++;
338
+ }
339
+
340
+ if (i >= size) return 0;
341
+ if (i > j && data[i] == '>') return i + 1;
342
+ /* one of the forbidden chars has been found */
343
+ *autolink = MKDA_NOT_AUTOLINK;
344
+ }
345
+
346
+ /* looking for sometinhg looking like a tag end */
347
+ while (i < size && data[i] != '>') i++;
348
+ if (i >= size) return 0;
349
+ return i + 1;
350
+ }
351
+
352
+ /* parse_inline • parses inline markdown elements */
353
+ static void
354
+ parse_inline(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
355
+ {
356
+ size_t i = 0, end = 0;
357
+ uint8_t action = 0;
358
+ struct buf work = { 0, 0, 0, 0 };
359
+
360
+ if (rndr->work_bufs[BUFFER_SPAN].size +
361
+ rndr->work_bufs[BUFFER_BLOCK].size > rndr->max_nesting)
362
+ return;
363
+
364
+ while (i < size) {
365
+ /* copying inactive chars into the output */
366
+ while (end < size && (action = rndr->active_char[data[end]]) == 0) {
367
+ end++;
368
+ }
369
+
370
+ if (rndr->cb.normal_text) {
371
+ work.data = data + i;
372
+ work.size = end - i;
373
+ rndr->cb.normal_text(ob, &work, rndr->opaque);
374
+ }
375
+ else
376
+ bufput(ob, data + i, end - i);
377
+
378
+ if (end >= size) break;
379
+ i = end;
380
+
381
+ end = markdown_char_ptrs[(int)action](ob, rndr, data + i, i, size - i);
382
+ if (!end) /* no action from the callback */
383
+ end = i + 1;
384
+ else {
385
+ i += end;
386
+ end = i;
387
+ }
388
+ }
389
+ }
390
+
391
+ /* find_emph_char • looks for the next emph uint8_t, skipping other constructs */
392
+ static size_t
393
+ find_emph_char(uint8_t *data, size_t size, uint8_t c)
394
+ {
395
+ size_t i = 1;
396
+
397
+ while (i < size) {
398
+ while (i < size && data[i] != c && data[i] != '`' && data[i] != '[')
399
+ i++;
400
+
401
+ if (i == size)
402
+ return 0;
403
+
404
+ if (data[i] == c)
405
+ return i;
406
+
407
+ /* not counting escaped chars */
408
+ if (i && data[i - 1] == '\\') {
409
+ i++; continue;
410
+ }
411
+
412
+ if (data[i] == '`') {
413
+ size_t span_nb = 0, bt;
414
+ size_t tmp_i = 0;
415
+
416
+ /* counting the number of opening backticks */
417
+ while (i < size && data[i] == '`') {
418
+ i++; span_nb++;
419
+ }
420
+
421
+ if (i >= size) return 0;
422
+
423
+ /* finding the matching closing sequence */
424
+ bt = 0;
425
+ while (i < size && bt < span_nb) {
426
+ if (!tmp_i && data[i] == c) tmp_i = i;
427
+ if (data[i] == '`') bt++;
428
+ else bt = 0;
429
+ i++;
430
+ }
431
+
432
+ if (i >= size) return tmp_i;
433
+ }
434
+ /* skipping a link */
435
+ else if (data[i] == '[') {
436
+ size_t tmp_i = 0;
437
+ uint8_t cc;
438
+
439
+ i++;
440
+ while (i < size && data[i] != ']') {
441
+ if (!tmp_i && data[i] == c) tmp_i = i;
442
+ i++;
443
+ }
444
+
445
+ i++;
446
+ while (i < size && (data[i] == ' ' || data[i] == '\n'))
447
+ i++;
448
+
449
+ if (i >= size)
450
+ return tmp_i;
451
+
452
+ switch (data[i]) {
453
+ case '[':
454
+ cc = ']'; break;
455
+
456
+ case '(':
457
+ cc = ')'; break;
458
+
459
+ default:
460
+ if (tmp_i)
461
+ return tmp_i;
462
+ else
463
+ continue;
464
+ }
465
+
466
+ i++;
467
+ while (i < size && data[i] != cc) {
468
+ if (!tmp_i && data[i] == c) tmp_i = i;
469
+ i++;
470
+ }
471
+
472
+ if (i >= size)
473
+ return tmp_i;
474
+
475
+ i++;
476
+ }
477
+ }
478
+
479
+ return 0;
480
+ }
481
+
482
+ /* parse_emph1 • parsing single emphase */
483
+ /* closed by a symbol not preceded by whitespace and not followed by symbol */
484
+ static size_t
485
+ parse_emph1(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, uint8_t c)
486
+ {
487
+ size_t i = 0, len;
488
+ struct buf *work = 0;
489
+ int r;
490
+
491
+ if (!rndr->cb.emphasis) return 0;
492
+
493
+ /* skipping one symbol if coming from emph3 */
494
+ if (size > 1 && data[0] == c && data[1] == c) i = 1;
495
+
496
+ while (i < size) {
497
+ len = find_emph_char(data + i, size - i, c);
498
+ if (!len) return 0;
499
+ i += len;
500
+ if (i >= size) return 0;
501
+
502
+ if (data[i] == c && !_isspace(data[i - 1])) {
503
+
504
+ if (rndr->ext_flags & MKDEXT_NO_INTRA_EMPHASIS) {
505
+ if (i + 1 < size && isalnum(data[i + 1]))
506
+ continue;
507
+ }
508
+
509
+ work = rndr_newbuf(rndr, BUFFER_SPAN);
510
+ parse_inline(work, rndr, data, i);
511
+ r = rndr->cb.emphasis(ob, work, rndr->opaque);
512
+ rndr_popbuf(rndr, BUFFER_SPAN);
513
+ return r ? i + 1 : 0;
514
+ }
515
+ }
516
+
517
+ return 0;
518
+ }
519
+
520
+ /* parse_emph2 • parsing single emphase */
521
+ static size_t
522
+ parse_emph2(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, uint8_t c)
523
+ {
524
+ int (*render_method)(struct buf *ob, const struct buf *text, void *opaque);
525
+ size_t i = 0, len;
526
+ struct buf *work = 0;
527
+ int r;
528
+
529
+ render_method = (c == '~') ? rndr->cb.strikethrough : rndr->cb.double_emphasis;
530
+
531
+ if (!render_method)
532
+ return 0;
533
+
534
+ while (i < size) {
535
+ len = find_emph_char(data + i, size - i, c);
536
+ if (!len) return 0;
537
+ i += len;
538
+
539
+ if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !_isspace(data[i - 1])) {
540
+ work = rndr_newbuf(rndr, BUFFER_SPAN);
541
+ parse_inline(work, rndr, data, i);
542
+ r = render_method(ob, work, rndr->opaque);
543
+ rndr_popbuf(rndr, BUFFER_SPAN);
544
+ return r ? i + 2 : 0;
545
+ }
546
+ i++;
547
+ }
548
+ return 0;
549
+ }
550
+
551
+ /* parse_emph3 • parsing single emphase */
552
+ /* finds the first closing tag, and delegates to the other emph */
553
+ static size_t
554
+ parse_emph3(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, uint8_t c)
555
+ {
556
+ size_t i = 0, len;
557
+ int r;
558
+
559
+ while (i < size) {
560
+ len = find_emph_char(data + i, size - i, c);
561
+ if (!len) return 0;
562
+ i += len;
563
+
564
+ /* skip whitespace preceded symbols */
565
+ if (data[i] != c || _isspace(data[i - 1]))
566
+ continue;
567
+
568
+ if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->cb.triple_emphasis) {
569
+ /* triple symbol found */
570
+ struct buf *work = rndr_newbuf(rndr, BUFFER_SPAN);
571
+
572
+ parse_inline(work, rndr, data, i);
573
+ r = rndr->cb.triple_emphasis(ob, work, rndr->opaque);
574
+ rndr_popbuf(rndr, BUFFER_SPAN);
575
+ return r ? i + 3 : 0;
576
+
577
+ } else if (i + 1 < size && data[i + 1] == c) {
578
+ /* double symbol found, handing over to emph1 */
579
+ len = parse_emph1(ob, rndr, data - 2, size + 2, c);
580
+ if (!len) return 0;
581
+ else return len - 2;
582
+
583
+ } else {
584
+ /* single symbol found, handing over to emph2 */
585
+ len = parse_emph2(ob, rndr, data - 1, size + 1, c);
586
+ if (!len) return 0;
587
+ else return len - 1;
588
+ }
589
+ }
590
+ return 0;
591
+ }
592
+
593
+ /* char_emphasis • single and double emphasis parsing */
594
+ static size_t
595
+ char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
596
+ {
597
+ uint8_t c = data[0];
598
+ size_t ret;
599
+
600
+ if (rndr->ext_flags & MKDEXT_NO_INTRA_EMPHASIS) {
601
+ if (offset > 0 && !_isspace(data[-1]) && data[-1] != '>')
602
+ return 0;
603
+ }
604
+
605
+ if (size > 2 && data[1] != c) {
606
+ /* whitespace cannot follow an opening emphasis;
607
+ * strikethrough only takes two characters '~~' */
608
+ if (c == '~' || _isspace(data[1]) || (ret = parse_emph1(ob, rndr, data + 1, size - 1, c)) == 0)
609
+ return 0;
610
+
611
+ return ret + 1;
612
+ }
613
+
614
+ if (size > 3 && data[1] == c && data[2] != c) {
615
+ if (_isspace(data[2]) || (ret = parse_emph2(ob, rndr, data + 2, size - 2, c)) == 0)
616
+ return 0;
617
+
618
+ return ret + 2;
619
+ }
620
+
621
+ if (size > 4 && data[1] == c && data[2] == c && data[3] != c) {
622
+ if (c == '~' || _isspace(data[3]) || (ret = parse_emph3(ob, rndr, data + 3, size - 3, c)) == 0)
623
+ return 0;
624
+
625
+ return ret + 3;
626
+ }
627
+
628
+ return 0;
629
+ }
630
+
631
+
632
+ /* char_linebreak • '\n' preceded by two spaces (assuming linebreak != 0) */
633
+ static size_t
634
+ char_linebreak(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
635
+ {
636
+ if (offset < 2 || data[-1] != ' ' || data[-2] != ' ')
637
+ return 0;
638
+
639
+ /* removing the last space from ob and rendering */
640
+ while (ob->size && ob->data[ob->size - 1] == ' ')
641
+ ob->size--;
642
+
643
+ return rndr->cb.linebreak(ob, rndr->opaque) ? 1 : 0;
644
+ }
645
+
646
+
647
+ /* char_codespan • '`' parsing a code span (assuming codespan != 0) */
648
+ static size_t
649
+ char_codespan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
650
+ {
651
+ size_t end, nb = 0, i, f_begin, f_end;
652
+
653
+ /* counting the number of backticks in the delimiter */
654
+ while (nb < size && data[nb] == '`')
655
+ nb++;
656
+
657
+ /* finding the next delimiter */
658
+ i = 0;
659
+ for (end = nb; end < size && i < nb; end++) {
660
+ if (data[end] == '`') i++;
661
+ else i = 0;
662
+ }
663
+
664
+ if (i < nb && end >= size)
665
+ return 0; /* no matching delimiter */
666
+
667
+ /* trimming outside whitespaces */
668
+ f_begin = nb;
669
+ while (f_begin < end && data[f_begin] == ' ')
670
+ f_begin++;
671
+
672
+ f_end = end - nb;
673
+ while (f_end > nb && data[f_end-1] == ' ')
674
+ f_end--;
675
+
676
+ /* real code span */
677
+ if (f_begin < f_end) {
678
+ struct buf work = { data + f_begin, f_end - f_begin, 0, 0 };
679
+ if (!rndr->cb.codespan(ob, &work, rndr->opaque))
680
+ end = 0;
681
+ } else {
682
+ if (!rndr->cb.codespan(ob, 0, rndr->opaque))
683
+ end = 0;
684
+ }
685
+
686
+ return end;
687
+ }
688
+
689
+
690
+ /* char_escape • '\\' backslash escape */
691
+ static size_t
692
+ char_escape(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
693
+ {
694
+ static const char *escape_chars = "\\`*_{}[]()#+-.!:|&<>^~";
695
+ struct buf work = { 0, 0, 0, 0 };
696
+
697
+ if (size > 1) {
698
+ if (strchr(escape_chars, data[1]) == NULL)
699
+ return 0;
700
+
701
+ if (rndr->cb.normal_text) {
702
+ work.data = data + 1;
703
+ work.size = 1;
704
+ rndr->cb.normal_text(ob, &work, rndr->opaque);
705
+ }
706
+ else bufputc(ob, data[1]);
707
+ } else if (size == 1) {
708
+ bufputc(ob, data[0]);
709
+ }
710
+
711
+ return 2;
712
+ }
713
+
714
+ /* char_entity • '&' escaped when it doesn't belong to an entity */
715
+ /* valid entities are assumed to be anything matching &#?[A-Za-z0-9]+; */
716
+ static size_t
717
+ char_entity(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
718
+ {
719
+ size_t end = 1;
720
+ struct buf work = { 0, 0, 0, 0 };
721
+
722
+ if (end < size && data[end] == '#')
723
+ end++;
724
+
725
+ while (end < size && isalnum(data[end]))
726
+ end++;
727
+
728
+ if (end < size && data[end] == ';')
729
+ end++; /* real entity */
730
+ else
731
+ return 0; /* lone '&' */
732
+
733
+ if (rndr->cb.entity) {
734
+ work.data = data;
735
+ work.size = end;
736
+ rndr->cb.entity(ob, &work, rndr->opaque);
737
+ }
738
+ else bufput(ob, data, end);
739
+
740
+ return end;
741
+ }
742
+
743
+ /* char_langle_tag • '<' when tags or autolinks are allowed */
744
+ static size_t
745
+ char_langle_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
746
+ {
747
+ enum mkd_autolink altype = MKDA_NOT_AUTOLINK;
748
+ size_t end = tag_length(data, size, &altype);
749
+ struct buf work = { data, end, 0, 0 };
750
+ int ret = 0;
751
+
752
+ if (end > 2) {
753
+ if (rndr->cb.autolink && altype != MKDA_NOT_AUTOLINK) {
754
+ struct buf *u_link = rndr_newbuf(rndr, BUFFER_SPAN);
755
+ work.data = data + 1;
756
+ work.size = end - 2;
757
+ unscape_text(u_link, &work);
758
+ ret = rndr->cb.autolink(ob, u_link, altype, rndr->opaque);
759
+ rndr_popbuf(rndr, BUFFER_SPAN);
760
+ }
761
+ else if (rndr->cb.raw_html_tag)
762
+ ret = rndr->cb.raw_html_tag(ob, &work, rndr->opaque);
763
+ }
764
+
765
+ if (!ret) return 0;
766
+ else return end;
767
+ }
768
+
769
+ static size_t
770
+ char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
771
+ {
772
+ struct buf *link, *link_url, *link_text;
773
+ size_t link_len, rewind;
774
+
775
+ if (!rndr->cb.link || rndr->in_link_body)
776
+ return 0;
777
+
778
+ link = rndr_newbuf(rndr, BUFFER_SPAN);
779
+
780
+ if ((link_len = sd_autolink__www(&rewind, link, data, offset, size, 0)) > 0) {
781
+ link_url = rndr_newbuf(rndr, BUFFER_SPAN);
782
+ BUFPUTSL(link_url, "http://");
783
+ bufput(link_url, link->data, link->size);
784
+
785
+ ob->size -= rewind;
786
+ if (rndr->cb.normal_text) {
787
+ link_text = rndr_newbuf(rndr, BUFFER_SPAN);
788
+ rndr->cb.normal_text(link_text, link, rndr->opaque);
789
+ rndr->cb.link(ob, link_url, NULL, link_text, rndr->opaque);
790
+ rndr_popbuf(rndr, BUFFER_SPAN);
791
+ } else {
792
+ rndr->cb.link(ob, link_url, NULL, link, rndr->opaque);
793
+ }
794
+ rndr_popbuf(rndr, BUFFER_SPAN);
795
+ }
796
+
797
+ rndr_popbuf(rndr, BUFFER_SPAN);
798
+ return link_len;
799
+ }
800
+
801
+ static size_t
802
+ char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
803
+ {
804
+ struct buf *link;
805
+ size_t link_len, rewind;
806
+
807
+ if (!rndr->cb.autolink || rndr->in_link_body)
808
+ return 0;
809
+
810
+ link = rndr_newbuf(rndr, BUFFER_SPAN);
811
+
812
+ if ((link_len = sd_autolink__email(&rewind, link, data, offset, size, 0)) > 0) {
813
+ ob->size -= rewind;
814
+ rndr->cb.autolink(ob, link, MKDA_EMAIL, rndr->opaque);
815
+ }
816
+
817
+ rndr_popbuf(rndr, BUFFER_SPAN);
818
+ return link_len;
819
+ }
820
+
821
+ static size_t
822
+ char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
823
+ {
824
+ struct buf *link;
825
+ size_t link_len, rewind;
826
+
827
+ if (!rndr->cb.autolink || rndr->in_link_body)
828
+ return 0;
829
+
830
+ link = rndr_newbuf(rndr, BUFFER_SPAN);
831
+
832
+ if ((link_len = sd_autolink__url(&rewind, link, data, offset, size, 0)) > 0) {
833
+ ob->size -= rewind;
834
+ rndr->cb.autolink(ob, link, MKDA_NORMAL, rndr->opaque);
835
+ }
836
+
837
+ rndr_popbuf(rndr, BUFFER_SPAN);
838
+ return link_len;
839
+ }
840
+
841
+ /* char_link • '[': parsing a link or an image */
842
+ static size_t
843
+ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
844
+ {
845
+ int is_img = (offset && data[-1] == '!'), level;
846
+ size_t i = 1, txt_e, link_b = 0, link_e = 0, title_b = 0, title_e = 0;
847
+ struct buf *content = 0;
848
+ struct buf *link = 0;
849
+ struct buf *title = 0;
850
+ struct buf *u_link = 0;
851
+ size_t org_work_size = rndr->work_bufs[BUFFER_SPAN].size;
852
+ int text_has_nl = 0, ret = 0;
853
+ int in_title = 0, qtype = 0;
854
+
855
+ /* checking whether the correct renderer exists */
856
+ if ((is_img && !rndr->cb.image) || (!is_img && !rndr->cb.link))
857
+ goto cleanup;
858
+
859
+ /* looking for the matching closing bracket */
860
+ for (level = 1; i < size; i++) {
861
+ if (data[i] == '\n')
862
+ text_has_nl = 1;
863
+
864
+ else if (data[i - 1] == '\\')
865
+ continue;
866
+
867
+ else if (data[i] == '[')
868
+ level++;
869
+
870
+ else if (data[i] == ']') {
871
+ level--;
872
+ if (level <= 0)
873
+ break;
874
+ }
875
+ }
876
+
877
+ if (i >= size)
878
+ goto cleanup;
879
+
880
+ txt_e = i;
881
+ i++;
882
+
883
+ /* skip any amount of whitespace or newline */
884
+ /* (this is much more laxist than original markdown syntax) */
885
+ while (i < size && _isspace(data[i]))
886
+ i++;
887
+
888
+ /* inline style link */
889
+ if (i < size && data[i] == '(') {
890
+ /* skipping initial whitespace */
891
+ i++;
892
+
893
+ while (i < size && _isspace(data[i]))
894
+ i++;
895
+
896
+ link_b = i;
897
+
898
+ /* looking for link end: ' " ) */
899
+ while (i < size) {
900
+ if (data[i] == '\\') i += 2;
901
+ else if (data[i] == ')') break;
902
+ else if (i >= 1 && _isspace(data[i-1]) && (data[i] == '\'' || data[i] == '"')) break;
903
+ else i++;
904
+ }
905
+
906
+ if (i >= size) goto cleanup;
907
+ link_e = i;
908
+
909
+ /* looking for title end if present */
910
+ if (data[i] == '\'' || data[i] == '"') {
911
+ qtype = data[i];
912
+ in_title = 1;
913
+ i++;
914
+ title_b = i;
915
+
916
+ while (i < size) {
917
+ if (data[i] == '\\') i += 2;
918
+ else if (data[i] == qtype) {in_title = 0; i++;}
919
+ else if ((data[i] == ')') && !in_title) break;
920
+ else i++;
921
+ }
922
+
923
+ if (i >= size) goto cleanup;
924
+
925
+ /* skipping whitespaces after title */
926
+ title_e = i - 1;
927
+ while (title_e > title_b && _isspace(data[title_e]))
928
+ title_e--;
929
+
930
+ /* checking for closing quote presence */
931
+ if (data[title_e] != '\'' && data[title_e] != '"') {
932
+ title_b = title_e = 0;
933
+ link_e = i;
934
+ }
935
+ }
936
+
937
+ /* remove whitespace at the end of the link */
938
+ while (link_e > link_b && _isspace(data[link_e - 1]))
939
+ link_e--;
940
+
941
+ /* remove optional angle brackets around the link */
942
+ if (data[link_b] == '<') link_b++;
943
+ if (data[link_e - 1] == '>') link_e--;
944
+
945
+ /* building escaped link and title */
946
+ if (link_e > link_b) {
947
+ link = rndr_newbuf(rndr, BUFFER_SPAN);
948
+ bufput(link, data + link_b, link_e - link_b);
949
+ }
950
+
951
+ if (title_e > title_b) {
952
+ title = rndr_newbuf(rndr, BUFFER_SPAN);
953
+ bufput(title, data + title_b, title_e - title_b);
954
+ }
955
+
956
+ i++;
957
+ }
958
+
959
+ /* reference style link */
960
+ else if (i < size && data[i] == '[') {
961
+ struct buf id = { 0, 0, 0, 0 };
962
+ struct link_ref *lr;
963
+
964
+ /* looking for the id */
965
+ i++;
966
+ link_b = i;
967
+ while (i < size && data[i] != ']') i++;
968
+ if (i >= size) goto cleanup;
969
+ link_e = i;
970
+
971
+ /* finding the link_ref */
972
+ if (link_b == link_e) {
973
+ if (text_has_nl) {
974
+ struct buf *b = rndr_newbuf(rndr, BUFFER_SPAN);
975
+ size_t j;
976
+
977
+ for (j = 1; j < txt_e; j++) {
978
+ if (data[j] != '\n')
979
+ bufputc(b, data[j]);
980
+ else if (data[j - 1] != ' ')
981
+ bufputc(b, ' ');
982
+ }
983
+
984
+ id.data = b->data;
985
+ id.size = b->size;
986
+ } else {
987
+ id.data = data + 1;
988
+ id.size = txt_e - 1;
989
+ }
990
+ } else {
991
+ id.data = data + link_b;
992
+ id.size = link_e - link_b;
993
+ }
994
+
995
+ lr = find_link_ref(rndr->refs, id.data, id.size);
996
+ if (!lr)
997
+ goto cleanup;
998
+
999
+ /* keeping link and title from link_ref */
1000
+ link = lr->link;
1001
+ title = lr->title;
1002
+ i++;
1003
+ }
1004
+
1005
+ /* shortcut reference style link */
1006
+ else {
1007
+ struct buf id = { 0, 0, 0, 0 };
1008
+ struct link_ref *lr;
1009
+
1010
+ /* crafting the id */
1011
+ if (text_has_nl) {
1012
+ struct buf *b = rndr_newbuf(rndr, BUFFER_SPAN);
1013
+ size_t j;
1014
+
1015
+ for (j = 1; j < txt_e; j++) {
1016
+ if (data[j] != '\n')
1017
+ bufputc(b, data[j]);
1018
+ else if (data[j - 1] != ' ')
1019
+ bufputc(b, ' ');
1020
+ }
1021
+
1022
+ id.data = b->data;
1023
+ id.size = b->size;
1024
+ } else {
1025
+ id.data = data + 1;
1026
+ id.size = txt_e - 1;
1027
+ }
1028
+
1029
+ /* finding the link_ref */
1030
+ lr = find_link_ref(rndr->refs, id.data, id.size);
1031
+ if (!lr)
1032
+ goto cleanup;
1033
+
1034
+ /* keeping link and title from link_ref */
1035
+ link = lr->link;
1036
+ title = lr->title;
1037
+
1038
+ /* rewinding the whitespace */
1039
+ i = txt_e + 1;
1040
+ }
1041
+
1042
+ /* building content: img alt is escaped, link content is parsed */
1043
+ if (txt_e > 1) {
1044
+ content = rndr_newbuf(rndr, BUFFER_SPAN);
1045
+ if (is_img) {
1046
+ bufput(content, data + 1, txt_e - 1);
1047
+ } else {
1048
+ /* disable autolinking when parsing inline the
1049
+ * content of a link */
1050
+ rndr->in_link_body = 1;
1051
+ parse_inline(content, rndr, data + 1, txt_e - 1);
1052
+ rndr->in_link_body = 0;
1053
+ }
1054
+ }
1055
+
1056
+ if (link) {
1057
+ u_link = rndr_newbuf(rndr, BUFFER_SPAN);
1058
+ unscape_text(u_link, link);
1059
+ }
1060
+
1061
+ /* calling the relevant rendering function */
1062
+ if (is_img) {
1063
+ if (ob->size && ob->data[ob->size - 1] == '!')
1064
+ ob->size -= 1;
1065
+
1066
+ ret = rndr->cb.image(ob, u_link, title, content, rndr->opaque);
1067
+ } else {
1068
+ ret = rndr->cb.link(ob, u_link, title, content, rndr->opaque);
1069
+ }
1070
+
1071
+ /* cleanup */
1072
+ cleanup:
1073
+ rndr->work_bufs[BUFFER_SPAN].size = (int)org_work_size;
1074
+ return ret ? i : 0;
1075
+ }
1076
+
1077
+ static size_t
1078
+ char_superscript(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
1079
+ {
1080
+ size_t sup_start, sup_len;
1081
+ struct buf *sup;
1082
+
1083
+ if (!rndr->cb.superscript)
1084
+ return 0;
1085
+
1086
+ if (size < 2)
1087
+ return 0;
1088
+
1089
+ if (data[1] == '(') {
1090
+ sup_start = sup_len = 2;
1091
+
1092
+ while (sup_len < size && data[sup_len] != ')' && data[sup_len - 1] != '\\')
1093
+ sup_len++;
1094
+
1095
+ if (sup_len == size)
1096
+ return 0;
1097
+ } else {
1098
+ sup_start = sup_len = 1;
1099
+
1100
+ while (sup_len < size && !_isspace(data[sup_len]))
1101
+ sup_len++;
1102
+ }
1103
+
1104
+ if (sup_len - sup_start == 0)
1105
+ return (sup_start == 2) ? 3 : 0;
1106
+
1107
+ sup = rndr_newbuf(rndr, BUFFER_SPAN);
1108
+ parse_inline(sup, rndr, data + sup_start, sup_len - sup_start);
1109
+ rndr->cb.superscript(ob, sup, rndr->opaque);
1110
+ rndr_popbuf(rndr, BUFFER_SPAN);
1111
+
1112
+ return (sup_start == 2) ? sup_len + 1 : sup_len;
1113
+ }
1114
+
1115
+ /*********************************
1116
+ * BLOCK-LEVEL PARSING FUNCTIONS *
1117
+ *********************************/
1118
+
1119
+ /* is_empty • returns the line length when it is empty, 0 otherwise */
1120
+ static size_t
1121
+ is_empty(uint8_t *data, size_t size)
1122
+ {
1123
+ size_t i;
1124
+
1125
+ for (i = 0; i < size && data[i] != '\n'; i++)
1126
+ if (data[i] != ' ')
1127
+ return 0;
1128
+
1129
+ return i + 1;
1130
+ }
1131
+
1132
+ /* is_hrule • returns whether a line is a horizontal rule */
1133
+ static int
1134
+ is_hrule(uint8_t *data, size_t size)
1135
+ {
1136
+ size_t i = 0, n = 0;
1137
+ uint8_t c;
1138
+
1139
+ /* skipping initial spaces */
1140
+ if (size < 3) return 0;
1141
+ if (data[0] == ' ') { i++;
1142
+ if (data[1] == ' ') { i++;
1143
+ if (data[2] == ' ') { i++; } } }
1144
+
1145
+ /* looking at the hrule uint8_t */
1146
+ if (i + 2 >= size
1147
+ || (data[i] != '*' && data[i] != '-' && data[i] != '_'))
1148
+ return 0;
1149
+ c = data[i];
1150
+
1151
+ /* the whole line must be the char or whitespace */
1152
+ while (i < size && data[i] != '\n') {
1153
+ if (data[i] == c) n++;
1154
+ else if (data[i] != ' ')
1155
+ return 0;
1156
+
1157
+ i++;
1158
+ }
1159
+
1160
+ return n >= 3;
1161
+ }
1162
+
1163
+ /* check if a line begins with a code fence; return the
1164
+ * width of the code fence */
1165
+ static size_t
1166
+ prefix_codefence(uint8_t *data, size_t size)
1167
+ {
1168
+ size_t i = 0, n = 0;
1169
+ uint8_t c;
1170
+
1171
+ /* skipping initial spaces */
1172
+ if (size < 3) return 0;
1173
+ if (data[0] == ' ') { i++;
1174
+ if (data[1] == ' ') { i++;
1175
+ if (data[2] == ' ') { i++; } } }
1176
+
1177
+ /* looking at the hrule uint8_t */
1178
+ if (i + 2 >= size || !(data[i] == '~' || data[i] == '`'))
1179
+ return 0;
1180
+
1181
+ c = data[i];
1182
+
1183
+ /* the whole line must be the uint8_t or whitespace */
1184
+ while (i < size && data[i] == c) {
1185
+ n++; i++;
1186
+ }
1187
+
1188
+ if (n < 3)
1189
+ return 0;
1190
+
1191
+ return i;
1192
+ }
1193
+
1194
+ /* check if a line is a code fence; return its size if it is */
1195
+ static size_t
1196
+ is_codefence(uint8_t *data, size_t size, struct buf *syntax)
1197
+ {
1198
+ size_t i = 0, syn_len = 0;
1199
+ uint8_t *syn_start;
1200
+
1201
+ i = prefix_codefence(data, size);
1202
+ if (i == 0)
1203
+ return 0;
1204
+
1205
+ while (i < size && data[i] == ' ')
1206
+ i++;
1207
+
1208
+ syn_start = data + i;
1209
+
1210
+ if (i < size && data[i] == '{') {
1211
+ i++; syn_start++;
1212
+
1213
+ while (i < size && data[i] != '}' && data[i] != '\n') {
1214
+ syn_len++; i++;
1215
+ }
1216
+
1217
+ if (i == size || data[i] != '}')
1218
+ return 0;
1219
+
1220
+ /* strip all whitespace at the beginning and the end
1221
+ * of the {} block */
1222
+ while (syn_len > 0 && _isspace(syn_start[0])) {
1223
+ syn_start++; syn_len--;
1224
+ }
1225
+
1226
+ while (syn_len > 0 && _isspace(syn_start[syn_len - 1]))
1227
+ syn_len--;
1228
+
1229
+ i++;
1230
+ } else {
1231
+ while (i < size && !_isspace(data[i])) {
1232
+ syn_len++; i++;
1233
+ }
1234
+ }
1235
+
1236
+ if (syntax) {
1237
+ syntax->data = syn_start;
1238
+ syntax->size = syn_len;
1239
+ }
1240
+
1241
+ while (i < size && data[i] != '\n') {
1242
+ if (!_isspace(data[i]))
1243
+ return 0;
1244
+
1245
+ i++;
1246
+ }
1247
+
1248
+ return i + 1;
1249
+ }
1250
+
1251
+ /* is_atxheader • returns whether the line is a hash-prefixed header */
1252
+ static int
1253
+ is_atxheader(struct sd_markdown *rndr, uint8_t *data, size_t size)
1254
+ {
1255
+ if (data[0] != '#')
1256
+ return 0;
1257
+
1258
+ if (rndr->ext_flags & MKDEXT_SPACE_HEADERS) {
1259
+ size_t level = 0;
1260
+
1261
+ while (level < size && level < 6 && data[level] == '#')
1262
+ level++;
1263
+
1264
+ if (level < size && data[level] != ' ')
1265
+ return 0;
1266
+ }
1267
+
1268
+ return 1;
1269
+ }
1270
+
1271
+ /* is_headerline • returns whether the line is a setext-style hdr underline */
1272
+ static int
1273
+ is_headerline(uint8_t *data, size_t size)
1274
+ {
1275
+ size_t i = 0;
1276
+
1277
+ /* test of level 1 header */
1278
+ if (data[i] == '=') {
1279
+ for (i = 1; i < size && data[i] == '='; i++);
1280
+ while (i < size && data[i] == ' ') i++;
1281
+ return (i >= size || data[i] == '\n') ? 1 : 0; }
1282
+
1283
+ /* test of level 2 header */
1284
+ if (data[i] == '-') {
1285
+ for (i = 1; i < size && data[i] == '-'; i++);
1286
+ while (i < size && data[i] == ' ') i++;
1287
+ return (i >= size || data[i] == '\n') ? 2 : 0; }
1288
+
1289
+ return 0;
1290
+ }
1291
+
1292
+ static int
1293
+ is_next_headerline(uint8_t *data, size_t size)
1294
+ {
1295
+ size_t i = 0;
1296
+
1297
+ while (i < size && data[i] != '\n')
1298
+ i++;
1299
+
1300
+ if (++i >= size)
1301
+ return 0;
1302
+
1303
+ return is_headerline(data + i, size - i);
1304
+ }
1305
+
1306
+ /* prefix_quote • returns blockquote prefix length */
1307
+ static size_t
1308
+ prefix_quote(uint8_t *data, size_t size)
1309
+ {
1310
+ size_t i = 0;
1311
+ if (i < size && data[i] == ' ') i++;
1312
+ if (i < size && data[i] == ' ') i++;
1313
+ if (i < size && data[i] == ' ') i++;
1314
+
1315
+ if (i < size && data[i] == '>') {
1316
+ if (i + 1 < size && data[i + 1] == ' ')
1317
+ return i + 2;
1318
+
1319
+ return i + 1;
1320
+ }
1321
+
1322
+ return 0;
1323
+ }
1324
+
1325
+ /* prefix_code • returns prefix length for block code*/
1326
+ static size_t
1327
+ prefix_code(uint8_t *data, size_t size)
1328
+ {
1329
+ if (size > 3 && data[0] == ' ' && data[1] == ' '
1330
+ && data[2] == ' ' && data[3] == ' ') return 4;
1331
+
1332
+ return 0;
1333
+ }
1334
+
1335
+ /* prefix_oli • returns ordered list item prefix */
1336
+ static size_t
1337
+ prefix_oli(uint8_t *data, size_t size)
1338
+ {
1339
+ size_t i = 0;
1340
+
1341
+ if (i < size && data[i] == ' ') i++;
1342
+ if (i < size && data[i] == ' ') i++;
1343
+ if (i < size && data[i] == ' ') i++;
1344
+
1345
+ if (i >= size || data[i] < '0' || data[i] > '9')
1346
+ return 0;
1347
+
1348
+ while (i < size && data[i] >= '0' && data[i] <= '9')
1349
+ i++;
1350
+
1351
+ if (i + 1 >= size || data[i] != '.' || data[i + 1] != ' ')
1352
+ return 0;
1353
+
1354
+ if (is_next_headerline(data + i, size - i))
1355
+ return 0;
1356
+
1357
+ return i + 2;
1358
+ }
1359
+
1360
+ /* prefix_uli • returns ordered list item prefix */
1361
+ static size_t
1362
+ prefix_uli(uint8_t *data, size_t size)
1363
+ {
1364
+ size_t i = 0;
1365
+
1366
+ if (i < size && data[i] == ' ') i++;
1367
+ if (i < size && data[i] == ' ') i++;
1368
+ if (i < size && data[i] == ' ') i++;
1369
+
1370
+ if (i + 1 >= size ||
1371
+ (data[i] != '*' && data[i] != '+' && data[i] != '-') ||
1372
+ data[i + 1] != ' ')
1373
+ return 0;
1374
+
1375
+ if (is_next_headerline(data + i, size - i))
1376
+ return 0;
1377
+
1378
+ return i + 2;
1379
+ }
1380
+
1381
+
1382
+ /* parse_block • parsing of one block, returning next uint8_t to parse */
1383
+ static void parse_block(struct buf *ob, struct sd_markdown *rndr,
1384
+ uint8_t *data, size_t size, const src_map *map);
1385
+
1386
+
1387
+ /* parse_blockquote • handles parsing of a blockquote fragment */
1388
+ static size_t
1389
+ parse_blockquote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, const src_map *map)
1390
+ {
1391
+ size_t beg, end = 0, pre, work_size = 0;
1392
+ uint8_t *work_data = 0;
1393
+ struct buf *out = 0;
1394
+
1395
+ /* source map */
1396
+ src_map *block_map = NULL;
1397
+
1398
+ /* AST construction */
1399
+ if (rndr->cb.blockquote_begin)
1400
+ rndr->cb.blockquote_begin(rndr->opaque);
1401
+
1402
+ out = rndr_newbuf(rndr, BUFFER_BLOCK);
1403
+
1404
+ beg = 0;
1405
+ while (beg < size) {
1406
+ for (end = beg + 1; end < size && data[end - 1] != '\n'; end++);
1407
+
1408
+ pre = prefix_quote(data + beg, end - beg);
1409
+
1410
+ if (pre)
1411
+ beg += pre; /* skipping prefix */
1412
+
1413
+ /* empty line followed by non-quote line */
1414
+ else if (is_empty(data + beg, end - beg) &&
1415
+ (end >= size || (prefix_quote(data + end, size - end) == 0 &&
1416
+ !is_empty(data + end, size - end))))
1417
+ break;
1418
+
1419
+ if (beg < end) { /* copy into the in-place working buffer */
1420
+ /* bufput(work, data + beg, end - beg); */
1421
+ if (!work_data)
1422
+ work_data = data + beg;
1423
+ else if (data + beg != work_data + work_size)
1424
+ memmove(work_data + work_size, data + beg, end - beg);
1425
+
1426
+ /* source map */
1427
+ if (map) {
1428
+ size_t cur = src_map_location(map, beg);
1429
+ range actual_range = { cur, end - beg };
1430
+ if (!block_map) {
1431
+ block_map = src_map_new_submap(map, &actual_range);
1432
+ }
1433
+ else {
1434
+ src_map_append(block_map, &actual_range);
1435
+ }
1436
+ }
1437
+
1438
+ work_size += end - beg;
1439
+ }
1440
+ beg = end;
1441
+ }
1442
+
1443
+ parse_block(out, rndr, work_data, work_size, block_map);
1444
+
1445
+ /* source map */
1446
+ if (block_map)
1447
+ src_map_release(block_map);
1448
+
1449
+ if (rndr->cb.blockquote)
1450
+ rndr->cb.blockquote(ob, out, rndr->opaque);
1451
+ rndr_popbuf(rndr, BUFFER_BLOCK);
1452
+ return end;
1453
+ }
1454
+
1455
+ static size_t
1456
+ parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int do_render);
1457
+
1458
+ /* parse_blockquote • handles parsing of a regular paragraph */
1459
+ static size_t
1460
+ parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
1461
+ {
1462
+ size_t i = 0, end = 0;
1463
+ int level = 0;
1464
+ struct buf work = { data, 0, 0, 0 };
1465
+
1466
+ while (i < size) {
1467
+ for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */;
1468
+
1469
+ if (is_empty(data + i, size - i))
1470
+ break;
1471
+
1472
+ if ((level = is_headerline(data + i, size - i)) != 0)
1473
+ break;
1474
+
1475
+ if (is_atxheader(rndr, data + i, size - i) ||
1476
+ is_hrule(data + i, size - i) ||
1477
+ prefix_quote(data + i, size - i)) {
1478
+ end = i;
1479
+ break;
1480
+ }
1481
+
1482
+ /*
1483
+ * Early termination of a paragraph with the same logic
1484
+ * as Markdown 1.0.0. If this logic is applied, the
1485
+ * Markdown 1.0.3 test suite won't pass cleanly
1486
+ *
1487
+ * :: If the first character in a new line is not a letter,
1488
+ * let's check to see if there's some kind of block starting
1489
+ * here
1490
+ */
1491
+ if ((rndr->ext_flags & MKDEXT_LAX_SPACING) && !isalnum(data[i])) {
1492
+ if (prefix_oli(data + i, size - i) ||
1493
+ prefix_uli(data + i, size - i)) {
1494
+ end = i;
1495
+ break;
1496
+ }
1497
+
1498
+ /* see if an html block starts here */
1499
+ if (data[i] == '<' && rndr->cb.blockhtml &&
1500
+ parse_htmlblock(ob, rndr, data + i, size - i, 0)) {
1501
+ end = i;
1502
+ break;
1503
+ }
1504
+
1505
+ /* see if a code fence starts here */
1506
+ if ((rndr->ext_flags & MKDEXT_FENCED_CODE) != 0 &&
1507
+ is_codefence(data + i, size - i, NULL) != 0) {
1508
+ end = i;
1509
+ break;
1510
+ }
1511
+ }
1512
+
1513
+ i = end;
1514
+ }
1515
+
1516
+ work.size = i;
1517
+ while (work.size && data[work.size - 1] == '\n')
1518
+ work.size--;
1519
+
1520
+ if (!level) {
1521
+ struct buf *tmp = rndr_newbuf(rndr, BUFFER_BLOCK);
1522
+ parse_inline(tmp, rndr, work.data, work.size);
1523
+ if (rndr->cb.paragraph)
1524
+ rndr->cb.paragraph(ob, tmp, rndr->opaque);
1525
+ rndr_popbuf(rndr, BUFFER_BLOCK);
1526
+ } else {
1527
+ struct buf *header_work;
1528
+
1529
+ if (work.size) {
1530
+ size_t beg;
1531
+ i = work.size;
1532
+ work.size -= 1;
1533
+
1534
+ while (work.size && data[work.size] != '\n')
1535
+ work.size -= 1;
1536
+
1537
+ beg = work.size + 1;
1538
+ while (work.size && data[work.size - 1] == '\n')
1539
+ work.size -= 1;
1540
+
1541
+ if (work.size > 0) {
1542
+ struct buf *tmp = rndr_newbuf(rndr, BUFFER_BLOCK);
1543
+ parse_inline(tmp, rndr, work.data, work.size);
1544
+
1545
+ if (rndr->cb.paragraph)
1546
+ rndr->cb.paragraph(ob, tmp, rndr->opaque);
1547
+
1548
+ rndr_popbuf(rndr, BUFFER_BLOCK);
1549
+ work.data += beg;
1550
+ work.size = i - beg;
1551
+ }
1552
+ else work.size = i;
1553
+ }
1554
+
1555
+ header_work = rndr_newbuf(rndr, BUFFER_SPAN);
1556
+ parse_inline(header_work, rndr, work.data, work.size);
1557
+
1558
+ if (rndr->cb.header)
1559
+ rndr->cb.header(ob, header_work, (int)level, rndr->opaque);
1560
+
1561
+ rndr_popbuf(rndr, BUFFER_SPAN);
1562
+ }
1563
+
1564
+ return end;
1565
+ }
1566
+
1567
+ /* parse_fencedcode • handles parsing of a block-level code fragment */
1568
+ static size_t
1569
+ parse_fencedcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
1570
+ {
1571
+ size_t beg, end;
1572
+ struct buf *work = 0;
1573
+ struct buf lang = { 0, 0, 0, 0 };
1574
+
1575
+ beg = is_codefence(data, size, &lang);
1576
+ if (beg == 0) return 0;
1577
+
1578
+ work = rndr_newbuf(rndr, BUFFER_BLOCK);
1579
+
1580
+ while (beg < size) {
1581
+ size_t fence_end;
1582
+ struct buf fence_trail = { 0, 0, 0, 0 };
1583
+
1584
+ fence_end = is_codefence(data + beg, size - beg, &fence_trail);
1585
+ if (fence_end != 0 && fence_trail.size == 0) {
1586
+ beg += fence_end;
1587
+ break;
1588
+ }
1589
+
1590
+ for (end = beg + 1; end < size && data[end - 1] != '\n'; end++);
1591
+
1592
+ if (beg < end) {
1593
+ /* verbatim copy to the working buffer,
1594
+ escaping entities */
1595
+ if (is_empty(data + beg, end - beg))
1596
+ bufputc(work, '\n');
1597
+ else bufput(work, data + beg, end - beg);
1598
+ }
1599
+ beg = end;
1600
+ }
1601
+
1602
+ if (work->size && work->data[work->size - 1] != '\n')
1603
+ bufputc(work, '\n');
1604
+
1605
+ if (rndr->cb.blockcode)
1606
+ rndr->cb.blockcode(ob, work, lang.size ? &lang : NULL, rndr->opaque);
1607
+
1608
+ rndr_popbuf(rndr, BUFFER_BLOCK);
1609
+ return beg;
1610
+ }
1611
+
1612
+ static size_t
1613
+ parse_blockcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
1614
+ {
1615
+ size_t beg, end, pre;
1616
+ struct buf *work = 0;
1617
+
1618
+ work = rndr_newbuf(rndr, BUFFER_BLOCK);
1619
+
1620
+ beg = 0;
1621
+ while (beg < size) {
1622
+ for (end = beg + 1; end < size && data[end - 1] != '\n'; end++) {};
1623
+ pre = prefix_code(data + beg, end - beg);
1624
+
1625
+ if (pre)
1626
+ beg += pre; /* skipping prefix */
1627
+ else if (!is_empty(data + beg, end - beg))
1628
+ /* non-empty non-prefixed line breaks the pre */
1629
+ break;
1630
+
1631
+ if (beg < end) {
1632
+ /* verbatim copy to the working buffer,
1633
+ escaping entities */
1634
+ if (is_empty(data + beg, end - beg))
1635
+ bufputc(work, '\n');
1636
+ else bufput(work, data + beg, end - beg);
1637
+ }
1638
+ beg = end;
1639
+ }
1640
+
1641
+ while (work->size && work->data[work->size - 1] == '\n')
1642
+ work->size -= 1;
1643
+
1644
+ bufputc(work, '\n');
1645
+
1646
+ if (rndr->cb.blockcode)
1647
+ rndr->cb.blockcode(ob, work, NULL, rndr->opaque);
1648
+
1649
+ rndr_popbuf(rndr, BUFFER_BLOCK);
1650
+ return beg;
1651
+ }
1652
+
1653
+ /* parse_listitem • parsing of a single list item */
1654
+ /* assuming initial prefix is already removed */
1655
+ static size_t
1656
+ parse_listitem(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int *flags, const src_map *map)
1657
+ {
1658
+ struct buf *work = 0, *inter = 0;
1659
+ size_t beg = 0, end, sublist = 0, orgpre = 0, i = 0;
1660
+ int in_empty = 0, has_inside_empty = 0, in_fence = 0;
1661
+ src_map *item_map = NULL;
1662
+ src_map *whole_item_map = NULL;
1663
+
1664
+ /* keeping track of the first indentation prefix */
1665
+ while (orgpre < 3 && orgpre < size && data[orgpre] == ' ')
1666
+ orgpre++;
1667
+
1668
+ beg = prefix_uli(data, size);
1669
+ if (!beg)
1670
+ beg = prefix_oli(data, size);
1671
+
1672
+ if (!beg)
1673
+ return 0;
1674
+
1675
+ /* AST construction */
1676
+ if (rndr->cb.listitem_begin)
1677
+ rndr->cb.listitem_begin(*flags, rndr->opaque);
1678
+
1679
+ /* skipping to the beginning of the following line */
1680
+ end = beg;
1681
+ while (end < size && data[end - 1] != '\n')
1682
+ end++;
1683
+
1684
+ /* getting working buffers */
1685
+ work = rndr_newbuf(rndr, BUFFER_SPAN);
1686
+ inter = rndr_newbuf(rndr, BUFFER_SPAN);
1687
+
1688
+ /* source map */
1689
+ if (map) {
1690
+ size_t cur = src_map_location(map, beg);
1691
+ range item_range = { cur, end - beg };
1692
+ item_map = src_map_new_submap(map, &item_range);
1693
+ }
1694
+
1695
+ /* putting the first line into the working buffer */
1696
+ bufput(work, data + beg, end - beg);
1697
+ beg = end;
1698
+
1699
+ /* process the following lines */
1700
+ while (beg < size) {
1701
+ size_t has_next_uli = 0, has_next_oli = 0;
1702
+
1703
+ end++;
1704
+
1705
+ while (end < size && data[end - 1] != '\n')
1706
+ end++;
1707
+
1708
+ /* process an empty line */
1709
+ if (is_empty(data + beg, end - beg)) {
1710
+ in_empty = 1;
1711
+ beg = end;
1712
+ continue;
1713
+ }
1714
+
1715
+ /* calculating the indentation */
1716
+ i = 0;
1717
+ while (i < 4 && beg + i < end && data[beg + i] == ' ')
1718
+ i++;
1719
+
1720
+ if (rndr->ext_flags & MKDEXT_FENCED_CODE) {
1721
+ if (is_codefence(data + beg + i, end - beg - i, NULL) != 0)
1722
+ in_fence = !in_fence;
1723
+ }
1724
+
1725
+ /* Only check for new list items if we are **not** inside
1726
+ * a fenced code block */
1727
+ if (!in_fence) {
1728
+ has_next_uli = prefix_uli(data + beg + i, end - beg - i);
1729
+ has_next_oli = prefix_oli(data + beg + i, end - beg - i);
1730
+ }
1731
+
1732
+ /* checking for ul/ol switch */
1733
+ if (in_empty && (
1734
+ ((*flags & MKD_LIST_ORDERED) && has_next_uli) ||
1735
+ (!(*flags & MKD_LIST_ORDERED) && has_next_oli))){
1736
+ *flags |= MKD_LI_END;
1737
+ break; /* the following item must have same list type */
1738
+ }
1739
+
1740
+ /* checking for a new item */
1741
+ if ((has_next_uli && !is_hrule(data + beg + i, end - beg - i)) || has_next_oli) {
1742
+ if (in_empty)
1743
+ has_inside_empty = 1;
1744
+
1745
+ if (i <= orgpre) /* the following item must have */
1746
+ break; /* the same indentation */
1747
+
1748
+ if (!sublist)
1749
+ sublist = work->size;
1750
+ }
1751
+ /* joining only indented stuff after empty lines;
1752
+ * note that now we only require 1 space of indentation
1753
+ * to continue a list */
1754
+ else if (in_empty && i == 0) {
1755
+ *flags |= MKD_LI_END;
1756
+ break;
1757
+ }
1758
+ else if (in_empty) {
1759
+ /* source map */
1760
+ if (item_map && item_map->size) {
1761
+ ((range *)item_map->item[item_map->size - 1])->len += 1;
1762
+ }
1763
+
1764
+ bufputc(work, '\n');
1765
+ has_inside_empty = 1;
1766
+ }
1767
+
1768
+ in_empty = 0;
1769
+
1770
+ /* If there is a line which is a heading, it isn't a line item. */
1771
+ if (end > beg && (data[beg] == '#' || is_next_headerline(data + beg, end - beg) != 0)) {
1772
+ *flags |= MKD_LI_END;
1773
+ break;
1774
+ }
1775
+
1776
+ /* source map */
1777
+ if (map) {
1778
+ size_t line_cur = src_map_location(map, beg + i);
1779
+ range line_range = { line_cur, end - beg - i };
1780
+ src_map_append(item_map, &line_range);
1781
+ }
1782
+
1783
+ /* adding the line without prefix into the working buffer */
1784
+ bufput(work, data + beg + i, end - beg - i);
1785
+ beg = end;
1786
+ }
1787
+
1788
+ /* render of li contents */
1789
+
1790
+ /* There were following condition in original code
1791
+ * we need different result of parsing (instead of span we need block)
1792
+ *
1793
+ if (has_inside_empty)
1794
+ *flags |= MKD_LI_BLOCK;
1795
+ *
1796
+ * So we replace condition directly by set flag.
1797
+ *
1798
+ * It should be better make some refactoring of this code,
1799
+ * but we wold like to avoid bigger changes in code
1800
+ * for future backporting of changes from upstream repo
1801
+ */
1802
+ *flags |= MKD_LI_BLOCK;
1803
+
1804
+ if (*flags & MKD_LI_BLOCK) {
1805
+ /* intermediate render of block li */
1806
+ if (sublist && sublist < work->size) {
1807
+ src_map *sublist_map = NULL;
1808
+
1809
+ parse_block(inter, rndr, work->data, sublist, item_map);
1810
+
1811
+ /* source map */
1812
+ if (item_map)
1813
+ sublist_map = src_map_new_tail(item_map, sublist, -1);
1814
+
1815
+ parse_block(inter, rndr, work->data + sublist, work->size - sublist, sublist_map);
1816
+
1817
+ if (sublist_map)
1818
+ src_map_release(sublist_map);
1819
+ }
1820
+ else
1821
+ parse_block(inter, rndr, work->data, work->size, item_map);
1822
+ } else {
1823
+ /* intermediate render of inline li */
1824
+ if (sublist && sublist < work->size) {
1825
+ src_map *sublist_map = NULL;
1826
+
1827
+ parse_inline(inter, rndr, work->data, sublist);
1828
+
1829
+ /* source map */
1830
+ if (item_map)
1831
+ sublist_map = src_map_new_tail(item_map, sublist, -1);
1832
+
1833
+ parse_block(inter, rndr, work->data + sublist, work->size - sublist, sublist_map);
1834
+
1835
+ if (sublist_map)
1836
+ src_map_release(sublist_map);
1837
+ }
1838
+ else
1839
+ parse_inline(inter, rndr, work->data, work->size);
1840
+ }
1841
+
1842
+ /* render of li itself */
1843
+ if (rndr->cb.listitem)
1844
+ rndr->cb.listitem(ob, inter, *flags, rndr->opaque);
1845
+
1846
+ /* source map */
1847
+ if (item_map) {
1848
+ range r = { orgpre, beg };
1849
+
1850
+ whole_item_map = src_map_new_tail(map, r.loc, r.len);
1851
+
1852
+ if (rndr->cb.block_did_parse)
1853
+ rndr->cb.block_did_parse(whole_item_map, data + i, size - i, rndr->opaque);
1854
+
1855
+ src_map_release(item_map);
1856
+ src_map_release(whole_item_map);
1857
+ }
1858
+
1859
+ rndr_popbuf(rndr, BUFFER_SPAN);
1860
+ rndr_popbuf(rndr, BUFFER_SPAN);
1861
+ return beg;
1862
+ }
1863
+
1864
+
1865
+ /* parse_list • parsing ordered or unordered list block */
1866
+ static size_t
1867
+ parse_list(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int flags, const src_map *map)
1868
+ {
1869
+ struct buf *work = 0;
1870
+ size_t i = 0, j;
1871
+
1872
+ /* AST construction */
1873
+ if (rndr->cb.list_begin)
1874
+ rndr->cb.list_begin(flags, rndr->opaque);
1875
+
1876
+ work = rndr_newbuf(rndr, BUFFER_BLOCK);
1877
+
1878
+ while (i < size) {
1879
+
1880
+ /* source map */
1881
+ src_map *block_map = NULL;
1882
+
1883
+ if (map) {
1884
+ size_t cur = src_map_location(map, i);
1885
+ range actual_range = { cur, size - i };
1886
+ block_map = src_map_new_submap(map, &actual_range);
1887
+ }
1888
+
1889
+ j = parse_listitem(work, rndr, data + i, size - i, &flags, block_map);
1890
+
1891
+ /* source map */
1892
+ if (block_map) {
1893
+ src_map_release(block_map);
1894
+ }
1895
+
1896
+ i += j;
1897
+ if (!j || (flags & MKD_LI_END))
1898
+ break;
1899
+ }
1900
+
1901
+ if (rndr->cb.list)
1902
+ rndr->cb.list(ob, work, flags, rndr->opaque);
1903
+ rndr_popbuf(rndr, BUFFER_BLOCK);
1904
+ return i;
1905
+ }
1906
+
1907
+ /* parse_atxheader • parsing of atx-style headers */
1908
+ static size_t
1909
+ parse_atxheader(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
1910
+ {
1911
+ size_t level = 0;
1912
+ size_t i, end, skip;
1913
+
1914
+ while (level < size && level < 6 && data[level] == '#')
1915
+ level++;
1916
+
1917
+ for (i = level; i < size && data[i] == ' '; i++);
1918
+
1919
+ for (end = i; end < size && data[end] != '\n'; end++);
1920
+ skip = end;
1921
+
1922
+ while (end && data[end - 1] == '#')
1923
+ end--;
1924
+
1925
+ while (end && data[end - 1] == ' ')
1926
+ end--;
1927
+
1928
+ if (end > i) {
1929
+ struct buf *work = rndr_newbuf(rndr, BUFFER_SPAN);
1930
+
1931
+ parse_inline(work, rndr, data + i, end - i);
1932
+
1933
+ if (rndr->cb.header)
1934
+ rndr->cb.header(ob, work, (int)level, rndr->opaque);
1935
+
1936
+ rndr_popbuf(rndr, BUFFER_SPAN);
1937
+ }
1938
+
1939
+ return skip;
1940
+ }
1941
+
1942
+
1943
+ /* htmlblock_end • checking end of HTML block : </tag>[ \t]*\n[ \t*]\n */
1944
+ /* returns the length on match, 0 otherwise */
1945
+ static size_t
1946
+ htmlblock_end_tag(
1947
+ const char *tag,
1948
+ size_t tag_len,
1949
+ struct sd_markdown *rndr,
1950
+ uint8_t *data,
1951
+ size_t size)
1952
+ {
1953
+ size_t i, w;
1954
+
1955
+ /* checking if tag is a match */
1956
+ if (tag_len + 3 >= size ||
1957
+ strncasecmp((char *)data + 2, tag, tag_len) != 0 ||
1958
+ data[tag_len + 2] != '>')
1959
+ return 0;
1960
+
1961
+ /* checking white lines */
1962
+ i = tag_len + 3;
1963
+ w = 0;
1964
+ if (i < size && (w = is_empty(data + i, size - i)) == 0)
1965
+ return 0; /* non-blank after tag */
1966
+ i += w;
1967
+ w = 0;
1968
+
1969
+ if (i < size)
1970
+ w = is_empty(data + i, size - i);
1971
+
1972
+ return i + w;
1973
+ }
1974
+
1975
+ static size_t
1976
+ htmlblock_end(const char *curtag,
1977
+ struct sd_markdown *rndr,
1978
+ uint8_t *data,
1979
+ size_t size,
1980
+ int start_of_line)
1981
+ {
1982
+ size_t tag_size = strlen(curtag);
1983
+ size_t i = 1, end_tag;
1984
+ int block_lines = 0;
1985
+
1986
+ while (i < size) {
1987
+ i++;
1988
+ while (i < size && !(data[i - 1] == '<' && data[i] == '/')) {
1989
+ if (data[i] == '\n')
1990
+ block_lines++;
1991
+
1992
+ i++;
1993
+ }
1994
+
1995
+ /* If we are only looking for unindented tags, skip the tag
1996
+ * if it doesn't follow a newline.
1997
+ *
1998
+ * The only exception to this is if the tag is still on the
1999
+ * initial line; in that case it still counts as a closing
2000
+ * tag
2001
+ */
2002
+ if (start_of_line && block_lines > 0 && data[i - 2] != '\n')
2003
+ continue;
2004
+
2005
+ if (i + 2 + tag_size >= size)
2006
+ break;
2007
+
2008
+ end_tag = htmlblock_end_tag(curtag, tag_size, rndr, data + i - 1, size - i + 1);
2009
+ if (end_tag)
2010
+ return i + end_tag - 1;
2011
+ }
2012
+
2013
+ return 0;
2014
+ }
2015
+
2016
+
2017
+ /* parse_htmlblock • parsing of inline HTML block */
2018
+ static size_t
2019
+ parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int do_render)
2020
+ {
2021
+ size_t i, j = 0, tag_end;
2022
+ const char *curtag = NULL;
2023
+ struct buf work = { data, 0, 0, 0 };
2024
+
2025
+ /* identification of the opening tag */
2026
+ if (size < 2 || data[0] != '<')
2027
+ return 0;
2028
+
2029
+ i = 1;
2030
+ while (i < size && data[i] != '>' && data[i] != ' ')
2031
+ i++;
2032
+
2033
+ if (i < size)
2034
+ curtag = find_block_tag((char *)data + 1, (int)i - 1);
2035
+
2036
+ /* handling of special cases */
2037
+ if (!curtag) {
2038
+
2039
+ /* HTML comment, laxist form */
2040
+ if (size > 5 && data[1] == '!' && data[2] == '-' && data[3] == '-') {
2041
+ i = 5;
2042
+
2043
+ while (i < size && !(data[i - 2] == '-' && data[i - 1] == '-' && data[i] == '>'))
2044
+ i++;
2045
+
2046
+ i++;
2047
+
2048
+ if (i < size)
2049
+ j = is_empty(data + i, size - i);
2050
+
2051
+ if (j) {
2052
+ work.size = i + j;
2053
+ if (do_render && rndr->cb.blockhtml)
2054
+ rndr->cb.blockhtml(ob, &work, rndr->opaque);
2055
+ return work.size;
2056
+ }
2057
+ }
2058
+
2059
+ /* HR, which is the only self-closing block tag considered */
2060
+ if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) {
2061
+ i = 3;
2062
+ while (i < size && data[i] != '>')
2063
+ i++;
2064
+
2065
+ if (i + 1 < size) {
2066
+ i++;
2067
+ j = is_empty(data + i, size - i);
2068
+ if (j) {
2069
+ work.size = i + j;
2070
+ if (do_render && rndr->cb.blockhtml)
2071
+ rndr->cb.blockhtml(ob, &work, rndr->opaque);
2072
+ return work.size;
2073
+ }
2074
+ }
2075
+ }
2076
+
2077
+ /* no special case recognised */
2078
+ return 0;
2079
+ }
2080
+
2081
+ /* looking for an unindented matching closing tag */
2082
+ /* followed by a blank line */
2083
+ tag_end = htmlblock_end(curtag, rndr, data, size, 1);
2084
+
2085
+ /* if not found, trying a second pass looking for indented match */
2086
+ /* but not if tag is "ins" or "del" (following original Markdown.pl) */
2087
+ if (!tag_end && strcmp(curtag, "ins") != 0 && strcmp(curtag, "del") != 0) {
2088
+ tag_end = htmlblock_end(curtag, rndr, data, size, 0);
2089
+ }
2090
+
2091
+ if (!tag_end)
2092
+ return 0;
2093
+
2094
+ /* the end of the block has been found */
2095
+ work.size = tag_end;
2096
+ if (do_render && rndr->cb.blockhtml)
2097
+ rndr->cb.blockhtml(ob, &work, rndr->opaque);
2098
+
2099
+ return tag_end;
2100
+ }
2101
+
2102
+ static void
2103
+ parse_table_row(
2104
+ struct buf *ob,
2105
+ struct sd_markdown *rndr,
2106
+ uint8_t *data,
2107
+ size_t size,
2108
+ size_t columns,
2109
+ int *col_data,
2110
+ int header_flag)
2111
+ {
2112
+ size_t i = 0, col;
2113
+ struct buf *row_work = 0;
2114
+
2115
+ if (!rndr->cb.table_cell || !rndr->cb.table_row)
2116
+ return;
2117
+
2118
+ row_work = rndr_newbuf(rndr, BUFFER_SPAN);
2119
+
2120
+ if (i < size && data[i] == '|')
2121
+ i++;
2122
+
2123
+ for (col = 0; col < columns && i < size; ++col) {
2124
+ size_t cell_start, cell_end;
2125
+ struct buf *cell_work;
2126
+
2127
+ cell_work = rndr_newbuf(rndr, BUFFER_SPAN);
2128
+
2129
+ while (i < size && _isspace(data[i]))
2130
+ i++;
2131
+
2132
+ cell_start = i;
2133
+
2134
+ while (i < size && data[i] != '|')
2135
+ i++;
2136
+
2137
+ cell_end = i - 1;
2138
+
2139
+ while (cell_end > cell_start && _isspace(data[cell_end]))
2140
+ cell_end--;
2141
+
2142
+ parse_inline(cell_work, rndr, data + cell_start, 1 + cell_end - cell_start);
2143
+ rndr->cb.table_cell(row_work, cell_work, col_data[col] | header_flag, rndr->opaque);
2144
+
2145
+ rndr_popbuf(rndr, BUFFER_SPAN);
2146
+ i++;
2147
+ }
2148
+
2149
+ for (; col < columns; ++col) {
2150
+ struct buf empty_cell = { 0, 0, 0, 0 };
2151
+ rndr->cb.table_cell(row_work, &empty_cell, col_data[col] | header_flag, rndr->opaque);
2152
+ }
2153
+
2154
+ rndr->cb.table_row(ob, row_work, rndr->opaque);
2155
+
2156
+ rndr_popbuf(rndr, BUFFER_SPAN);
2157
+ }
2158
+
2159
+ static size_t
2160
+ parse_table_header(
2161
+ struct buf *ob,
2162
+ struct sd_markdown *rndr,
2163
+ uint8_t *data,
2164
+ size_t size,
2165
+ size_t *columns,
2166
+ int **column_data)
2167
+ {
2168
+ int pipes;
2169
+ size_t i = 0, col, header_end, under_end;
2170
+
2171
+ pipes = 0;
2172
+ while (i < size && data[i] != '\n')
2173
+ if (data[i++] == '|')
2174
+ pipes++;
2175
+
2176
+ if (i == size || pipes == 0)
2177
+ return 0;
2178
+
2179
+ header_end = i;
2180
+
2181
+ while (header_end > 0 && _isspace(data[header_end - 1]))
2182
+ header_end--;
2183
+
2184
+ if (data[0] == '|')
2185
+ pipes--;
2186
+
2187
+ if (header_end && data[header_end - 1] == '|')
2188
+ pipes--;
2189
+
2190
+ if (pipes < 0)
2191
+ return 0;
2192
+
2193
+ *columns = pipes + 1;
2194
+ *column_data = calloc(*columns, sizeof(int));
2195
+
2196
+ /* Parse the header underline */
2197
+ i++;
2198
+ if (i < size && data[i] == '|')
2199
+ i++;
2200
+
2201
+ under_end = i;
2202
+ while (under_end < size && data[under_end] != '\n')
2203
+ under_end++;
2204
+
2205
+ for (col = 0; col < *columns && i < under_end; ++col) {
2206
+ size_t dashes = 0;
2207
+
2208
+ while (i < under_end && data[i] == ' ')
2209
+ i++;
2210
+
2211
+ if (data[i] == ':') {
2212
+ i++; (*column_data)[col] |= MKD_TABLE_ALIGN_L;
2213
+ dashes++;
2214
+ }
2215
+
2216
+ while (i < under_end && data[i] == '-') {
2217
+ i++; dashes++;
2218
+ }
2219
+
2220
+ if (i < under_end && data[i] == ':') {
2221
+ i++; (*column_data)[col] |= MKD_TABLE_ALIGN_R;
2222
+ dashes++;
2223
+ }
2224
+
2225
+ while (i < under_end && data[i] == ' ')
2226
+ i++;
2227
+
2228
+ if (i < under_end && data[i] != '|')
2229
+ break;
2230
+
2231
+ if (dashes < 3)
2232
+ break;
2233
+
2234
+ i++;
2235
+ }
2236
+
2237
+ if (col < *columns)
2238
+ return 0;
2239
+
2240
+ parse_table_row(
2241
+ ob, rndr, data,
2242
+ header_end,
2243
+ *columns,
2244
+ *column_data,
2245
+ MKD_TABLE_HEADER
2246
+ );
2247
+
2248
+ return under_end + 1;
2249
+ }
2250
+
2251
+ static size_t
2252
+ parse_table(
2253
+ struct buf *ob,
2254
+ struct sd_markdown *rndr,
2255
+ uint8_t *data,
2256
+ size_t size)
2257
+ {
2258
+ size_t i;
2259
+
2260
+ struct buf *header_work = 0;
2261
+ struct buf *body_work = 0;
2262
+
2263
+ size_t columns;
2264
+ int *col_data = NULL;
2265
+
2266
+ header_work = rndr_newbuf(rndr, BUFFER_SPAN);
2267
+ body_work = rndr_newbuf(rndr, BUFFER_BLOCK);
2268
+
2269
+ i = parse_table_header(header_work, rndr, data, size, &columns, &col_data);
2270
+ if (i > 0) {
2271
+
2272
+ while (i < size) {
2273
+ size_t row_start;
2274
+ int pipes = 0;
2275
+
2276
+ row_start = i;
2277
+
2278
+ while (i < size && data[i] != '\n')
2279
+ if (data[i++] == '|')
2280
+ pipes++;
2281
+
2282
+ if (pipes == 0 || i == size) {
2283
+ i = row_start;
2284
+ break;
2285
+ }
2286
+
2287
+ parse_table_row(
2288
+ body_work,
2289
+ rndr,
2290
+ data + row_start,
2291
+ i - row_start,
2292
+ columns,
2293
+ col_data, 0
2294
+ );
2295
+
2296
+ i++;
2297
+ }
2298
+
2299
+ if (rndr->cb.table)
2300
+ rndr->cb.table(ob, header_work, body_work, rndr->opaque);
2301
+ }
2302
+
2303
+ free(col_data);
2304
+ rndr_popbuf(rndr, BUFFER_SPAN);
2305
+ rndr_popbuf(rndr, BUFFER_BLOCK);
2306
+ return i;
2307
+ }
2308
+
2309
+ /* parse_block • parsing of one block, returning next uint8_t to parse */
2310
+ static void
2311
+ parse_block(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, const src_map *map)
2312
+ {
2313
+ size_t beg, end, i, block_beg;
2314
+ uint8_t *txt_data;
2315
+ beg = 0;
2316
+
2317
+ if (rndr->work_bufs[BUFFER_SPAN].size +
2318
+ rndr->work_bufs[BUFFER_BLOCK].size > rndr->max_nesting)
2319
+ return;
2320
+
2321
+ while (beg < size) {
2322
+ size_t cur = 0;
2323
+ src_map *block_map = NULL;
2324
+
2325
+ block_beg = beg;
2326
+ txt_data = data + beg;
2327
+ end = size - beg;
2328
+
2329
+ /* source map */
2330
+ if (map) {
2331
+ range actual_range;
2332
+
2333
+ cur = src_map_location(map, beg);
2334
+ actual_range.loc = cur;
2335
+ actual_range.len = end;
2336
+
2337
+ block_map = src_map_new_submap(map, &actual_range);
2338
+ }
2339
+
2340
+ if (is_atxheader(rndr, txt_data, end))
2341
+ beg += parse_atxheader(ob, rndr, txt_data, end);
2342
+
2343
+ else if (data[beg] == '<' && rndr->cb.blockhtml &&
2344
+ (i = parse_htmlblock(ob, rndr, txt_data, end, 1)) != 0)
2345
+ beg += i;
2346
+
2347
+ else if ((i = is_empty(txt_data, end)) != 0) {
2348
+ beg += i;
2349
+ }
2350
+
2351
+ else if (is_hrule(txt_data, end)) {
2352
+ if (rndr->cb.hrule)
2353
+ rndr->cb.hrule(ob, rndr->opaque);
2354
+
2355
+ while (beg < size && data[beg] != '\n')
2356
+ beg++;
2357
+
2358
+ beg++;
2359
+ }
2360
+
2361
+ else if ((rndr->ext_flags & MKDEXT_FENCED_CODE) != 0 &&
2362
+ (i = parse_fencedcode(ob, rndr, txt_data, end)) != 0)
2363
+ beg += i;
2364
+
2365
+ else if ((rndr->ext_flags & MKDEXT_TABLES) != 0 &&
2366
+ (i = parse_table(ob, rndr, txt_data, end)) != 0)
2367
+ beg += i;
2368
+
2369
+ else if (prefix_quote(txt_data, end))
2370
+ beg += parse_blockquote(ob, rndr, txt_data, end, block_map);
2371
+
2372
+ else if (prefix_code(txt_data, end))
2373
+ beg += parse_blockcode(ob, rndr, txt_data, end);
2374
+
2375
+ else if (prefix_uli(txt_data, end))
2376
+ beg += parse_list(ob, rndr, txt_data, end, 0, block_map);
2377
+
2378
+ else if (prefix_oli(txt_data, end))
2379
+ beg += parse_list(ob, rndr, txt_data, end, MKD_LIST_ORDERED, block_map);
2380
+
2381
+ else
2382
+ beg += parse_paragraph(ob, rndr, txt_data, end);
2383
+
2384
+ /* source map */
2385
+ if (block_map) {
2386
+ if (rndr->cb.block_did_parse) {
2387
+ range parse_range = { cur, beg - block_beg };
2388
+ src_map *parse_map = src_map_new_submap(block_map, &parse_range);
2389
+
2390
+ rndr->cb.block_did_parse(parse_map, txt_data, beg - block_beg, rndr->opaque);
2391
+
2392
+ src_map_release(parse_map);
2393
+ }
2394
+ src_map_release(block_map);
2395
+ }
2396
+ }
2397
+ }
2398
+
2399
+ /*********************
2400
+ * REFERENCE PARSING *
2401
+ *********************/
2402
+
2403
+ /* is_ref • returns whether a line is a reference or not */
2404
+ static int
2405
+ is_ref(const uint8_t *data, size_t beg, size_t end, size_t *last, struct link_ref **refs)
2406
+ {
2407
+ /* int n; */
2408
+ size_t i = 0;
2409
+ size_t id_offset, id_end;
2410
+ size_t link_offset, link_end;
2411
+ size_t title_offset, title_end;
2412
+ size_t line_end;
2413
+
2414
+ /* up to 3 optional leading spaces */
2415
+ if (beg + 3 >= end) return 0;
2416
+ if (data[beg] == ' ') { i = 1;
2417
+ if (data[beg + 1] == ' ') { i = 2;
2418
+ if (data[beg + 2] == ' ') { i = 3;
2419
+ if (data[beg + 3] == ' ') return 0; } } }
2420
+ i += beg;
2421
+
2422
+ /* id part: anything but a newline between brackets */
2423
+ if (data[i] != '[') return 0;
2424
+ i++;
2425
+ id_offset = i;
2426
+ while (i < end && data[i] != '\n' && data[i] != '\r' && data[i] != ']')
2427
+ i++;
2428
+ if (i >= end || data[i] != ']') return 0;
2429
+ id_end = i;
2430
+
2431
+ /* spacer: colon (space | tab)* newline? (space | tab)* */
2432
+ i++;
2433
+ if (i >= end || data[i] != ':') return 0;
2434
+ i++;
2435
+ while (i < end && data[i] == ' ') i++;
2436
+ if (i < end && (data[i] == '\n' || data[i] == '\r')) {
2437
+ i++;
2438
+ if (i < end && data[i] == '\r' && data[i - 1] == '\n') i++; }
2439
+ while (i < end && data[i] == ' ') i++;
2440
+ if (i >= end) return 0;
2441
+
2442
+ /* link: whitespace-free sequence, optionally between angle brackets */
2443
+ if (data[i] == '<')
2444
+ i++;
2445
+
2446
+ link_offset = i;
2447
+
2448
+ while (i < end && data[i] != ' ' && data[i] != '\n' && data[i] != '\r')
2449
+ i++;
2450
+
2451
+ if (data[i - 1] == '>') link_end = i - 1;
2452
+ else link_end = i;
2453
+
2454
+ /* optional spacer: (space | tab)* (newline | '\'' | '"' | '(' ) */
2455
+ while (i < end && data[i] == ' ') i++;
2456
+ if (i < end && data[i] != '\n' && data[i] != '\r'
2457
+ && data[i] != '\'' && data[i] != '"' && data[i] != '(')
2458
+ return 0;
2459
+ line_end = 0;
2460
+ /* computing end-of-line */
2461
+ if (i >= end || data[i] == '\r' || data[i] == '\n') line_end = i;
2462
+ if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r')
2463
+ line_end = i + 1;
2464
+
2465
+ /* optional (space|tab)* spacer after a newline */
2466
+ if (line_end) {
2467
+ i = line_end + 1;
2468
+ while (i < end && data[i] == ' ') i++; }
2469
+
2470
+ /* optional title: any non-newline sequence enclosed in '"()
2471
+ alone on its line */
2472
+ title_offset = title_end = 0;
2473
+ if (i + 1 < end
2474
+ && (data[i] == '\'' || data[i] == '"' || data[i] == '(')) {
2475
+ i++;
2476
+ title_offset = i;
2477
+ /* looking for EOL */
2478
+ while (i < end && data[i] != '\n' && data[i] != '\r') i++;
2479
+ if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r')
2480
+ title_end = i + 1;
2481
+ else title_end = i;
2482
+ /* stepping back */
2483
+ i -= 1;
2484
+ while (i > title_offset && data[i] == ' ')
2485
+ i -= 1;
2486
+ if (i > title_offset
2487
+ && (data[i] == '\'' || data[i] == '"' || data[i] == ')')) {
2488
+ line_end = title_end;
2489
+ title_end = i; } }
2490
+
2491
+ if (!line_end || link_end == link_offset)
2492
+ return 0; /* garbage after the link empty link */
2493
+
2494
+ /* a valid ref has been found, filling-in return structures */
2495
+ if (last)
2496
+ *last = line_end;
2497
+
2498
+ if (refs) {
2499
+ struct link_ref *ref;
2500
+
2501
+ ref = add_link_ref(refs, data + id_offset, id_end - id_offset);
2502
+ if (!ref)
2503
+ return 0;
2504
+
2505
+ ref->link = bufnew(link_end - link_offset);
2506
+ bufput(ref->link, data + link_offset, link_end - link_offset);
2507
+
2508
+ if (title_end > title_offset) {
2509
+ ref->title = bufnew(title_end - title_offset);
2510
+ bufput(ref->title, data + title_offset, title_end - title_offset);
2511
+ }
2512
+ }
2513
+
2514
+ return 1;
2515
+ }
2516
+
2517
+ static void expand_tabs(struct buf *ob, const uint8_t *line, size_t size)
2518
+ {
2519
+ size_t i = 0, tab = 0;
2520
+
2521
+ while (i < size) {
2522
+ size_t org = i;
2523
+
2524
+ while (i < size && line[i] != '\t') {
2525
+ i++; tab++;
2526
+ }
2527
+
2528
+ if (i > org)
2529
+ bufput(ob, line + org, i - org);
2530
+
2531
+ if (i >= size)
2532
+ break;
2533
+
2534
+ do {
2535
+ bufputc(ob, ' '); tab++;
2536
+ } while (tab % 4);
2537
+
2538
+ i++;
2539
+ }
2540
+ }
2541
+
2542
+ /**********************
2543
+ * EXPORTED FUNCTIONS *
2544
+ **********************/
2545
+
2546
+ struct sd_markdown *
2547
+ sd_markdown_new(
2548
+ unsigned int extensions,
2549
+ size_t max_nesting,
2550
+ const struct sd_callbacks *callbacks,
2551
+ void *opaque)
2552
+ {
2553
+ struct sd_markdown *md = NULL;
2554
+
2555
+ assert(max_nesting > 0 && callbacks);
2556
+
2557
+ md = malloc(sizeof(struct sd_markdown));
2558
+ if (!md)
2559
+ return NULL;
2560
+
2561
+ memcpy(&md->cb, callbacks, sizeof(struct sd_callbacks));
2562
+
2563
+ stack_init(&md->work_bufs[BUFFER_BLOCK], 4);
2564
+ stack_init(&md->work_bufs[BUFFER_SPAN], 8);
2565
+
2566
+ memset(md->active_char, 0x0, 256);
2567
+
2568
+ if (md->cb.emphasis || md->cb.double_emphasis || md->cb.triple_emphasis) {
2569
+ md->active_char['*'] = MD_CHAR_EMPHASIS;
2570
+ md->active_char['_'] = MD_CHAR_EMPHASIS;
2571
+ if (extensions & MKDEXT_STRIKETHROUGH)
2572
+ md->active_char['~'] = MD_CHAR_EMPHASIS;
2573
+ }
2574
+
2575
+ if (md->cb.codespan)
2576
+ md->active_char['`'] = MD_CHAR_CODESPAN;
2577
+
2578
+ if (md->cb.linebreak)
2579
+ md->active_char['\n'] = MD_CHAR_LINEBREAK;
2580
+
2581
+ if (md->cb.image || md->cb.link)
2582
+ md->active_char['['] = MD_CHAR_LINK;
2583
+
2584
+ md->active_char['<'] = MD_CHAR_LANGLE;
2585
+ md->active_char['\\'] = MD_CHAR_ESCAPE;
2586
+ md->active_char['&'] = MD_CHAR_ENTITITY;
2587
+
2588
+ if (extensions & MKDEXT_AUTOLINK) {
2589
+ md->active_char[':'] = MD_CHAR_AUTOLINK_URL;
2590
+ md->active_char['@'] = MD_CHAR_AUTOLINK_EMAIL;
2591
+ md->active_char['w'] = MD_CHAR_AUTOLINK_WWW;
2592
+ }
2593
+
2594
+ if (extensions & MKDEXT_SUPERSCRIPT)
2595
+ md->active_char['^'] = MD_CHAR_SUPERSCRIPT;
2596
+
2597
+ /* Extension data */
2598
+ md->ext_flags = extensions;
2599
+ md->opaque = opaque;
2600
+ md->max_nesting = max_nesting;
2601
+ md->in_link_body = 0;
2602
+
2603
+ return md;
2604
+ }
2605
+
2606
+ void
2607
+ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md)
2608
+ {
2609
+ #define MARKDOWN_GROW(x) ((x) + ((x) >> 1))
2610
+ static const char UTF8_BOM[] = {0xEF, 0xBB, 0xBF};
2611
+
2612
+ struct buf *text;
2613
+ size_t beg, end;
2614
+ src_map *map;
2615
+
2616
+ /* source map */
2617
+ if (md->cb.block_did_parse)
2618
+ map = src_map_new();
2619
+ else
2620
+ map = NULL;
2621
+
2622
+ text = bufnew(64);
2623
+ if (!text)
2624
+ return;
2625
+
2626
+ /* Preallocate enough space for our buffer to avoid expanding while copying */
2627
+ bufgrow(text, doc_size);
2628
+
2629
+ /* reset the references table */
2630
+ memset(&md->refs, 0x0, REF_TABLE_SIZE * sizeof(void *));
2631
+
2632
+ /* first pass: looking for references, copying everything else */
2633
+ beg = 0;
2634
+
2635
+ /* Skip a possible UTF-8 BOM, even though the Unicode standard
2636
+ * discourages having these in UTF-8 documents */
2637
+ if (doc_size >= 3 && memcmp(document, UTF8_BOM, 3) == 0)
2638
+ beg += 3;
2639
+
2640
+ while (beg < doc_size) /* iterating over lines */
2641
+ if (is_ref(document, beg, doc_size, &end, md->refs)) {
2642
+ if (end > beg)
2643
+ expand_tabs(text, document + beg, end - beg);
2644
+
2645
+ beg = end;
2646
+ } else { /* skipping to the next line */
2647
+ end = beg;
2648
+ while (end < doc_size && document[end] != '\n' && document[end] != '\r')
2649
+ end++;
2650
+
2651
+ /* adding the line body if present */
2652
+ if (end > beg)
2653
+ expand_tabs(text, document + beg, end - beg);
2654
+
2655
+ while (end < doc_size && (document[end] == '\n' || document[end] == '\r')) {
2656
+ /* add one \n per newline */
2657
+ if (document[end] == '\n' || (end + 1 < doc_size && document[end + 1] != '\n'))
2658
+ bufputc(text, '\n');
2659
+ end++;
2660
+ }
2661
+
2662
+ beg = end;
2663
+ }
2664
+
2665
+ /* pre-grow the output buffer to minimize allocations */
2666
+ bufgrow(ob, MARKDOWN_GROW(text->size));
2667
+
2668
+ /* second pass: actual rendering */
2669
+ if (md->cb.doc_header)
2670
+ md->cb.doc_header(ob, md->opaque);
2671
+
2672
+ if (text->size) {
2673
+
2674
+ /* adding a final newline if not already present */
2675
+ if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r')
2676
+ bufputc(text, '\n');
2677
+
2678
+ /* source map */
2679
+ if (map) {
2680
+ range rng = {0, text->size};
2681
+ src_map_append(map, &rng);
2682
+ }
2683
+
2684
+ parse_block(ob, md, text->data, text->size, map);
2685
+
2686
+ /* source map */
2687
+ src_map_release(map);
2688
+ }
2689
+
2690
+ if (md->cb.doc_footer)
2691
+ md->cb.doc_footer(ob, md->opaque);
2692
+
2693
+ /* clean-up */
2694
+ bufrelease(text);
2695
+ free_link_refs(md->refs);
2696
+
2697
+ assert(md->work_bufs[BUFFER_SPAN].size == 0);
2698
+ assert(md->work_bufs[BUFFER_BLOCK].size == 0);
2699
+ }
2700
+
2701
+ void
2702
+ sd_markdown_free(struct sd_markdown *md)
2703
+ {
2704
+ size_t i;
2705
+
2706
+ for (i = 0; i < (size_t)md->work_bufs[BUFFER_SPAN].asize; ++i)
2707
+ bufrelease(md->work_bufs[BUFFER_SPAN].item[i]);
2708
+
2709
+ for (i = 0; i < (size_t)md->work_bufs[BUFFER_BLOCK].asize; ++i)
2710
+ bufrelease(md->work_bufs[BUFFER_BLOCK].item[i]);
2711
+
2712
+ stack_free(&md->work_bufs[BUFFER_SPAN]);
2713
+ stack_free(&md->work_bufs[BUFFER_BLOCK]);
2714
+
2715
+ free(md);
2716
+ }
2717
+
2718
+ void
2719
+ sd_version(int *ver_major, int *ver_minor, int *ver_revision)
2720
+ {
2721
+ *ver_major = SUNDOWN_VER_MAJOR;
2722
+ *ver_minor = SUNDOWN_VER_MINOR;
2723
+ *ver_revision = SUNDOWN_VER_REVISION;
2724
+ }
2725
+
2726
+ /* vim: set filetype=c: */