The repository formerly known as dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttpre.c 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright (C) 2018 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <wchar.h>
  20. static void put(wchar_t ch) {
  21. switch (ch) {
  22. break; case L'&': printf("&amp;");
  23. break; case L'<': printf("&lt;");
  24. break; case L'>': printf("&gt;");
  25. break; default: printf("%lc", ch);
  26. }
  27. }
  28. static void tag(const char *open) {
  29. static const char *close = NULL;
  30. if (close == open) return;
  31. if (close) printf("</%s>", close);
  32. if (open) printf("<%s>", open);
  33. close = open;
  34. }
  35. static void push(wchar_t ch) {
  36. static wchar_t q[3];
  37. if (q[1] == L'\b' && q[0] == L'_') {
  38. tag("i");
  39. put(q[2]);
  40. q[0] = q[1] = q[2] = 0;
  41. } else if (q[1] == L'\b' && q[0] == q[2]) {
  42. tag("b");
  43. put(q[2]);
  44. q[0] = q[1] = q[2] = 0;
  45. } else if (q[0]) {
  46. tag(NULL);
  47. put(q[0]);
  48. }
  49. q[0] = q[1];
  50. q[1] = q[2];
  51. q[2] = ch;
  52. }
  53. int main(void) {
  54. setlocale(LC_CTYPE, "");
  55. printf("<pre>");
  56. wchar_t ch;
  57. while (WEOF != (ch = getwchar())) push(ch);
  58. push(0); push(0); push(0);
  59. printf("</pre>\n");
  60. return EXIT_SUCCESS;
  61. }