html.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "html.h"
  4. #include "utils.h"
  5. /* globals */
  6. extern char *line;
  7. extern char *params[9];
  8. extern int nparams;
  9. extern int inparagraph;
  10. int
  11. spitheader(void)
  12. {
  13. const char htmlheader[] =
  14. "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
  15. "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
  16. "<html>\n"
  17. "<head>\n"
  18. "<meta name=\"generator\" content=\"webgen, see domonkos.tk\">\n"
  19. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=US-ASCII\">\n"
  20. "<meta name=\"Content-Style\" content=\"text/css\">\n"
  21. "<title>Personal website</title>\n"
  22. "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n"
  23. "</head>\n"
  24. "<body>\n";
  25. return fputs(htmlheader, stdout);
  26. }
  27. int spitfooter(void)
  28. {
  29. const char htmlfooter[] =
  30. "<hr>\n"
  31. "</body>\n"
  32. "</html>\n";
  33. return fputs(htmlfooter, stdout);
  34. }
  35. int
  36. spithref(void)
  37. {
  38. const char htmlrefstring[] = "<a href=\"%s\">%s</a>";
  39. if (nparams < 1) {
  40. err("href requires at least one parameter!");
  41. return 1;
  42. } else if (nparams == 1) {
  43. printf(htmlrefstring, params[0], params[0]);
  44. } else if (nparams == 2) {
  45. printf(htmlrefstring, params[0], params[1]);
  46. } else if (nparams > 2) {
  47. err("href takes two parameters at max!");
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. int
  53. spitimg(void)
  54. {
  55. const char htmlimgstring[] = "<img src=\"%s\" alt=\"Image %s\">";
  56. if (nparams < 1) {
  57. err("img requires at least one parameter!");
  58. return 1;
  59. } else if (nparams == 1) {
  60. printf(htmlimgstring, params[0], params[0]);
  61. } else if (nparams == 2) {
  62. printf(htmlimgstring, params[0], params[1]);
  63. } else if (nparams > 2) {
  64. err("img takes two parameters at max!");
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. int
  70. spititalic(void)
  71. {
  72. if (nparams < 1) {
  73. err("italic requires at least one parameter!");
  74. return 1;
  75. } else if (nparams == 1) {
  76. printf("<em>%s</em>\n", params[0]);
  77. } else if (nparams == 2) {
  78. printf("<em>%s</em>%s\n", params[0], params[1]);
  79. } else if (nparams == 3) {
  80. printf("%s<em>%s</em>%s\n", params[2], params[0], params[1]);
  81. } else if (nparams > 3) {
  82. err("italic takes three parameters at max!");
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. int
  88. spitbold(void)
  89. {
  90. if (nparams < 1) {
  91. err("bold requires at least one parameter!");
  92. return 1;
  93. } else if (nparams == 1) {
  94. printf("<b>%s</b>\n", params[0]);
  95. } else if (nparams == 2) {
  96. printf("<b>%s</b>%s\n", params[0], params[1]);
  97. } else if (nparams == 3) {
  98. printf("%s<b>%s</b>%s\n", params[2], params[0], params[1]);
  99. } else if (nparams > 3) {
  100. err("bold takes three parameters at max!");
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. int
  106. spitmonospace(void)
  107. {
  108. if (nparams < 1) {
  109. err("monospace requires at least one parameter!");
  110. return 1;
  111. } else if (nparams == 1) {
  112. printf("<tt>%s</tt>\n", params[0]);
  113. } else if (nparams == 2) {
  114. printf("<tt>%s</tt>%s\n", params[0], params[1]);
  115. } else if (nparams == 3) {
  116. printf("%s<tt>%s</tt>%s\n", params[2], params[0], params[1]);
  117. } else if (nparams > 3) {
  118. err("monospace takes three parameters at max!");
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. int
  124. spitlinebreak(void)
  125. {
  126. if (nparams >= 1) {
  127. err("br takes no params!");
  128. return 1;
  129. } else {
  130. puts("<br>");
  131. }
  132. return 0;
  133. }
  134. int
  135. newparagraph(void)
  136. {
  137. /* if (nparams >= 1) { //TODO
  138. err("paragraph takes no params!");
  139. return 1;
  140. } */
  141. if (inparagraph) {
  142. puts("</p>\n<p>");
  143. } else {
  144. puts("<p>");
  145. }
  146. inparagraph = 1;
  147. return 0;
  148. }
  149. int
  150. spithorizontalrule(void)
  151. {
  152. if (nparams >= 1) {
  153. err("hr takes no params!");
  154. return 1;
  155. } else {
  156. puts("<hr>");
  157. }
  158. return 0;
  159. }
  160. int
  161. subheading(void)
  162. {
  163. int level = 1;
  164. if (nparams < 2) {
  165. printf("<h1>%s</h1>\n", params[0]);
  166. } else if (nparams == 2) {
  167. level = atoi(params[0]);
  168. if (level < 1 || level > 6) {
  169. err("subheading level must be between [1 6]!");
  170. return 1;
  171. }
  172. printf("<h%d>%s</h%d>\n", level, params[1], level);
  173. } else { // nparams > 2
  174. err("subheading takes two parameters at max!");
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. int
  180. codestart(void)
  181. {
  182. if (nparams >= 1) {
  183. err(".cs takes no params!");
  184. return 1;
  185. } else {
  186. puts("<pre><code>");
  187. }
  188. return 0;
  189. }
  190. int
  191. codeend(void)
  192. {
  193. if (nparams >= 1) {
  194. err(".ce takes no params!");
  195. return 1;
  196. } else {
  197. puts("</pre></code>");
  198. }
  199. return 0;
  200. }