plain2html.mll 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. (* https://ohama.github.io/ocaml/ocamllex-tutorial/simpleexample/ *)
  27. { }
  28. rule url buf = parse
  29. | "http" 's'? "://" [^' ']+ as lxm {
  30. let buf =
  31. let u = lxm |> Uri.of_string in
  32. let off = match Uri.scheme u with
  33. | None -> 0
  34. | Some s -> 3 + (String.length s) in
  35. let len = lxm |> String.length in
  36. let ( |^ ) b s = Buffer.add_string b s; b in
  37. buf
  38. |^ "<a href=\""
  39. |^ (u |> Uri.to_string)
  40. |^ "\">"
  41. |^ (String.sub lxm off (len - off))
  42. |^ "</a>" in
  43. url buf lexbuf
  44. }
  45. | '\n' {
  46. Buffer.add_string buf "<br/>\n";
  47. url buf lexbuf
  48. }
  49. | _ as c {
  50. Buffer.add_char buf c;
  51. url buf lexbuf
  52. }
  53. | eof { buf }