PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/markedoc.sed

https://github.com/seth/Emysql
sed | 378 lines | 112 code | 42 blank | 224 comment | 1 complexity | 4656c560c4f54addb1dff27c491e94d7 MD5 | raw file
  1. # markedoc 0.3.1 - 02/05/11 - H. Diedrich <hd2010@eonblast.com>
  2. # ----------------------------------------------------------
  3. # sed command file to convert markdown format to edoc format
  4. # Linux, FreeBSD and Mac OS X. -- Windows must install sed.
  5. # ----------------------------------------------------------
  6. # Use it to make a markdown readme file part of an edoc file:
  7. # FrBSD: sed -E -f <this file> <markdown file> > <edoc file>
  8. # MacOS: sed -E -f <this file> <markdown file> > <edoc file>
  9. # Linux: sed -r -f <this file> <markdown file> > <edoc file>
  10. # Only difference, Linux uses -r where the others use -E.
  11. # ----------------------------------------------------------
  12. # SAMPLE for FreeBSD / Mac OS X:
  13. # sed -E -f markedoc.sed README.markdown > overview.edoc
  14. # SAMPLE for Linux:
  15. # sed -r -f markedoc.sed README.markdown > overview.edoc
  16. # ----------------------------------------------------------
  17. # SAMPLE FILES:
  18. # https://github.com/hdiedrich/markedoc/tree/master/samples
  19. # SAMPLE RESULTS:
  20. # samples/what-you-should-see/ & samples/what-you-could-see/
  21. # ----------------------------------------------------------
  22. # SAMPLE WORKFLOW (change -r to -E for FreeBSD / Mac OS X):
  23. # sed -r -f markedoc.sed README.md > doc/README.edoc
  24. # erl -noshell -run edoc_run application "'myapp'" '"."' '[]'
  25. # ----------------------------------------------------------
  26. # REQUIREMENTS: sed, Erlang. sed is in all *x distros.
  27. # Windows: http://gnuwin32.sourceforge.net/packages/sed.htm
  28. # ----------------------------------------------------------
  29. # STATUS: Pre-Beta.
  30. # It can reliably do nice things but likes to trip up EDoc.
  31. # With a bit of patience, and mostly with pretty clean md
  32. # markup, and some blank lines sometimes, most things work.
  33. # ----------------------------------------------------------
  34. # LICENSE: Free software, no warranties.
  35. # ----------------------------------------------------------
  36. # edown: http://www.erlang.org/doc/apps/edoc/
  37. # Markdown: http://daringfireball.net/projects/markdown/
  38. # Edoc: http://www.erlang.org/doc/apps/edoc/
  39. # sed: http://www.gnu.org/software/sed/manual/sed.html
  40. # ----------------------------------------------------------
  41. # Repository: https://github.com/hdiedrich/markedoc/
  42. # Issues: https://github.com/hdiedrich/markedoc/issues
  43. # Please experiment and push your fixes. - Thanks!
  44. # ----------------------------------------------------------
  45. # **********************************************************
  46. # SCRIPT
  47. # **********************************************************
  48. # Ach, da kommt der Meister! Herr, die Not ist gross! ~~~
  49. # ~~~ Die ich rief, die Geister, Werd ich nun nicht los.
  50. # ----------------------------------------------------------
  51. # This is a sed script for use with -E/-r regexes & NOT -n.
  52. # s/<find>/<replace>/<flag> is the basic sed regex replace
  53. # command. sed normally works strictly line by line. 'N'
  54. # is used to join lines. 't' is a conditional branch, ':'
  55. # is a label. The order of replacement functions matters.
  56. # There are tabs in some patterns that may look like spaces.
  57. # See 'man sed' for more info. If you are a sed master,
  58. # your help making this better is much appreciated.
  59. # **********************************************************
  60. # as first line, make the @doc tag
  61. # --------------------------------
  62. 1 i\
  63. @doc\
  64. # code sample blocks, trying to get them into one <pre> block
  65. # -----------------------------------------------------------
  66. # tabs are consumed for 'navigation'. sed is Turing complete.
  67. # inserted space is needed by edocs.
  68. # There are tabs in this pattern.
  69. /^ / {
  70. # break ... on last line ('N' would exit)
  71. $ b end_collect_with_last_line_hit
  72. s/^ (.*)$/ \1/
  73. # do ...
  74. : do_collect
  75. # append next line
  76. N
  77. # break ... if we are now into the last line
  78. # (or the test below will eat the tab away.)
  79. $ b end_collect_with_last_line_hit
  80. # does the current last line start with a tab, too?
  81. s/(\n) (.*)$/\1 \2/
  82. # while: ... yes, then loop
  83. t do_collect
  84. # normal end of collect: got all indendet lines, plus one too many.
  85. # -----------------------------------------------------------------
  86. b normal_course
  87. #
  88. # Run into file end while looping
  89. # -------------------------------
  90. : end_collect_with_last_line_hit
  91. # and does that last line start with a tab, too?
  92. s/(\n) (.*)$/\1 \2/
  93. s/^ (.*)$/ \1/
  94. # yes, then we're done actually
  95. t wrap_rest_and_done
  96. # else, cut it off and such, as normal
  97. # debug i\
  98. # debug normal
  99. #
  100. : normal_course
  101. # ... ok, we have multiple lines, and we have one line too much, back it all up.
  102. h
  103. # Handle the <pre> block to be (*):
  104. # ---------------------------------
  105. # cut off the last line, that doesn't belong and insert newlines
  106. s/^(.*)(\n)(.*)$/\2\1\2/
  107. # wrap all in the docs code tags ```...'''
  108. s/^(.*)$/```\1'''/
  109. # protect @ (for edoc related texts that explain @-tags). There is a tab in [].
  110. s/([ \"\'\`]+@)/\1@/g
  111. # send result to stdout
  112. p
  113. # Now make sure that that last line is not lost:
  114. # ----------------------------------------------
  115. # get stored back
  116. g
  117. # this time discard all but the last line, which is processed further
  118. s/^.*\n(.*)$/\1/
  119. # jump to end
  120. b end_of_code_blocks_handling
  121. #
  122. # File End Remedy: wrap all to end and done.
  123. # ------------------------------------------
  124. : wrap_rest_and_done
  125. # debug i\
  126. # debug rest and done
  127. # wrap all in the docs code tags ```...'''
  128. s/^(.*)$/```\1'''/
  129. # protect @ (for edoc related texts that explain @-tags). There is a tab in [].
  130. s/([ \"\'\`]+@)/\1@/g
  131. b end
  132. #
  133. }
  134. :end_of_code_blocks_handling
  135. # robust alternate for code blocks: each tabbed line
  136. # --------------------------------------------------
  137. # If the above keeps being difficult, use this more robust
  138. # version. The main difference is merely that it will tag each
  139. # line separately. If you work with very small margins and
  140. # paddings for <pre> in your css file, that might give just as
  141. # nice a result as the above. There are tabs in this pattern.
  142. # (Really, delete all of the above from '# code sample blocks ...'
  143. # to # :end_of_code_blocks_handling)
  144. # s/^ (.+)$/``` \1'''/
  145. # edoc sugar
  146. # -----------
  147. # won't help the markdown source but make the edoc prettier
  148. # footnote signs
  149. # ..............
  150. # superscript 1
  151. s/\(\*1\)/\&#185;/g
  152. # superscript 2
  153. s/\(\*2\)/\&#178;/g
  154. # superscript 3
  155. s/\(\*3\)/\&#179;/g
  156. # dagger
  157. s/\(\+\)/\&#134;/g
  158. # double dagger
  159. s/\(\+\+\)/\&#135;/g
  160. # star
  161. s/\(\*\)/\&#42;/g
  162. # double star
  163. s/\(\*\*\)/\&#42;\&#42;/g
  164. # triple star
  165. s/\(\*\*\*\)/\&#42;\&#42;\&#42;/g
  166. # special chars
  167. # .............
  168. # middle dot
  169. s/::/\&#183;/g
  170. # guillemot
  171. s/<</\&#171;/g
  172. s/>>/\&#187;/g
  173. # copyright
  174. # .........
  175. s/\(c)/\&#169;/g
  176. s/\(C)/\&#169;/g
  177. s/\(R)/\&#174;/g
  178. s/\(r)/\&#174;/g
  179. s/\(tm)/\&#153;/g
  180. s/\(TM)/\&#153;/g
  181. # atone for markdown \_
  182. # ---------------------
  183. s/\\_/_/g
  184. # links
  185. # -----
  186. # external links
  187. s/\[([^]]+)\]\(([^)]+)\)/<a href=\"\2\">\1<\/a>/
  188. # references, '[..]:...'-style
  189. # ----------------------------
  190. # take out markdown anchor relay tricks (see README)
  191. # ..................................................
  192. s/\[[^]]+\]:[ ]*#.*//
  193. # real urls (normal case)
  194. # .......................
  195. s/(\[([^]]+)\]): +\[?(http[s]?:\/\/[^.>" ]+\.[^>" ]+)\]? * *("([^"]+)") * *$/<li class="ref url"> \5:<a name="\2" id="\2" href="\3" target="_parent">\3<\/a><\/li>/
  196. # check next line "..." description
  197. /(\[([^]]+)\]): +\[?(http[s]?:\/\/[^.>" ]+\.[^>" ]+)\]? *$/ {
  198. # get next line, if the current is not the last
  199. $!N
  200. # try two line spanning, or single (last) line
  201. s/(\[([^]]+)\]): +\[?(http[s]?:\/\/[^.>" ]+\.[^>" ]+)\]? * *\n * *("([^"]*)") * *$/<li class="ref url"> \5:<a name="\2" id="\2" href="\3" target="_parent">\3<\/a><\/li>/
  202. t double_line_url_done
  203. # try one line only, rest to be saved
  204. s/(\[([^]]+)\]): +\[?(http[s]?:\/\/[^.>" ]+\.[^>" ]+)\]? * *(\n)/<li class="ref url"> <a name="\2" id="\2" href="\3" target="_parent">\3<\/a><\/li>\4/
  205. t double_line_url_done
  206. # case of last line, single, no "..." description
  207. s/(\[([^]]+)\]): +\[?(http[s]?:\/\/[^.>" ]+\.[^>" ]+)\]? * *$/<li class="ref url"> <a name="\2" id="\2" href="\3" target="_parent">\3<\/a><\/li>/
  208. : double_line_url_done
  209. # print out up to first \n, delete, start from top with the rest
  210. P
  211. D
  212. }
  213. # email addresses
  214. # ...............
  215. s/(\[([^]]+)\]): +<?([^@>" ]+@[^.>" ]+\.[^>" ]+)>? * *("([^"]+)") * *$/<li class="ref email"> \5:<a name="\2" id="\2" href="mailto:\3">\3<\/a><\/li>/
  216. # check next line "..." description
  217. /(\[([^]]+)\]): +<?([^@>" ]+@[^.>" ]+\.[^>" ]+)>? * *("([^"]+)")? * *$/ {
  218. # get next line, if the current is not the last
  219. $!N
  220. # try two line spanning, or single (last) line
  221. s/(\[([^]]+)\]): +<?([^@>" ]+@[^.>" ]+\.[^>" ]+)>? * *\n * *("([^"]+)") * *$/<li class="ref email"> \5:<a name="\2" id="\2" href="mailto:\3">\3<\/a><\/li>/
  222. t double_line_mail_done
  223. # try one line only, rest to be saved
  224. s/(\[([^]]+)\]): +<?([^@>" ]+@[^.>" ]+\.[^>" ]+)>? * *(\n)/<li class="ref email"> <a name="\2" id="\2" href="mailto:\3">\3<\/a><\/li>\4/
  225. t double_line_mail_done
  226. # case of last line, single, no "..." description
  227. s/(\[([^]]+)\]): +<?([^@>" ]+@[^.>" ]+\.[^>" ]+)>? * *$/<li class="ref email"> <a name="\2" id="\2" href="mailto:\3">\3<\/a><\/li>/
  228. : double_line_mail_done
  229. # print out up to first \n, delete, start from top with the rest
  230. P
  231. D
  232. }
  233. # smart reference for the [x]: ... format, jumping right to the referenced page.
  234. # ------------------------------------------------------------------------------
  235. # s/\[([^]]+)\]\[\]/<a href="javascript:goto('\1')" onMouseOver="this.title=url('\1')">\1<\/a>/g
  236. # s/\[([^]]+)\]\[([^]]+)\]/<a href="javascript:goto('\2')" onMouseOver="this.title=url('\2')">\1<\/a>/g
  237. # robust alternate reference for the [x]: ... format, jumping to footnote.
  238. # ------------------------------------------------------------------------
  239. # If you don't like the 'smart' javascript tags, comment out the previous 'smart'
  240. # reference patterns and uncomment these. The behavior then becomes, not to
  241. # directly jump to a given url when clicked, but to the line where that link
  242. # is listed that then can be clicked. So it's one click more, but it works.
  243. s/\[([^]]+)\]\[\]/<a href="#\1">\1<\/a>/g
  244. s/\[([^]]+)\]\[([^]]+)\]/<a href="#\2">\1<\/a>/g
  245. # headlines by #
  246. # --------------
  247. # h1 demoted to h2 as h1 is reserved in edoc,
  248. # in passing weed out explicit anchors and formatting
  249. s/^####([^<]+).*/====\1 ====/
  250. s/^###([^<]+).*/===\1 ===/
  251. s/^##([^<]+).*/==\1 ==/
  252. s/^#([^<]+).*/==\1 ==/
  253. # italics, bold
  254. # -------------
  255. s/\*\*(([^*]+\*?)+)\*\*/<b>\1<\/b>/g
  256. s/\*([^*]+)\*/<em>\1<\/em>/g
  257. # bullet points
  258. # -------------
  259. # edoc must see closing </li>
  260. s/^\*(.+)$/<li>\1<\/li>/
  261. # emails, urls
  262. # ------------
  263. s/<([^aA][^@>]+@[^.>]+.[^>]+)>/<a href=\"mailto:\1\">\1<\/a>/
  264. s/<(http[s]?:\/\/[^.>]+.[^>]+)>/<a href=\"\1\">\1<\/a>/
  265. # line breaks
  266. # -----------
  267. s/ $/<br \/>/
  268. # single backticks
  269. # ----------------
  270. # make code quotes
  271. s/`([^`]+)`/<code>\1<\/code>/g
  272. # protect @
  273. # ---------
  274. # leading space or tab indicates use as code sample for, well, edoc
  275. # itself most likely, so escape it.
  276. s/([ \"\'\`]+@)/\1@/g
  277. # protect &
  278. # ---------
  279. # still edoc won't understand code names like &rarr;
  280. s/&#/:::AMPERSAND:::#/g
  281. s/&([a-z]{1,7});/:::AMPERSAND:::\1;/g
  282. s/&/\&amp;/g
  283. s/:::AMPERSAND:::/\&/g
  284. # headlines by underline === or ---
  285. # ---------------------------------
  286. # demoted to h2 and h3, as h1 is reserved in edoc
  287. {
  288. # don't check this for the last line ('N' would exit)
  289. $ b skip_alt_headlines
  290. # get next line
  291. N
  292. # contract === with previous to headline h2
  293. # and in passing weed out explicit anchors and formatting
  294. s/^([^<]+)(<.*>)?[ ]*\n=+ *$/== \1 ==/
  295. # if substitution took place, goto ...
  296. t substi
  297. # contract --- with previous to headline h2
  298. # and in passing weed out explicit anchors and formatting
  299. s/^([^<]+)(<.*>)?[ ]*\n-+ *$/=== \1 ===/
  300. # if substitution took place, goto ...
  301. t substi
  302. # no substitution: print the previous line and start with latest from top
  303. # -----------------------------------------------------------------------
  304. # store the two lines we have now, one is the one formatting is done with
  305. # the next is the fresh one we just pulled.
  306. h
  307. # cut off the last line, print the ready formatted one
  308. P
  309. D
  310. # and this is the goto for successful headline substitutions above:
  311. :substi
  312. }
  313. :skip_alt_headlines
  314. :end
  315. # at the bottom, add JS for the 'smart' direct jump
  316. # -------------------------------------------------
  317. # to a reference url in trailing '[]:...'-notation
  318. $ a\
  319. <script>\
  320. // Jump directly to a referenced url given in trailing '[]:...'-notation\
  321. function goto(tag) { parent.document.location.href = url(tag); }\
  322. function url(tag) { var o=document.getElementById(tag); return o ? o.href : '#'+tag; }\
  323. </script>
  324. # debugger stable
  325. # ---------------
  326. # i\
  327. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  328. # p
  329. # i\
  330. # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  331. # -----------------------------------------------------------------
  332. # t,b: "In most cases, use of these commands indicates that you are
  333. # probably better off programming in something like awk or Perl."
  334. # sed manual: http://www.gnu.org/software/sed/manual/sed.html
  335. # -----------------------------------------------------------------
  336. # 'powered by Eonblast' http://www.eonblast.com - all the new tech