Comment detail

改行をBRタグに置き換える (Nested Flatten)

#3638 をベースに作成しました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 1024
char in[BUFSIZE];

int main( void ){
   char *p, *tmp;
   char q, attr=0, s='<';
   int len=0, remain, br=0, othertags=0;

   while( (remain = fgets( in + len, BUFSIZE - len, stdin )) || *p ) {
      for(p=in;*p;p++){
         if( remain && (len = strlen( p )) < strlen("<string") ){
            tmp = (char*)malloc( sizeof(char) * len );
            strcpy( tmp, p );
            strcpy( in, tmp );
            free(tmp);
            break;
         }
         len = 0;
         if( s == '<' ){
            if( *p == '>' ){ printf("&gt;");
               if(othertags==1) othertags=0;
            }
            else if( *p == '\n' || (*p == '\r' && (*(p+1) == '\n'?p++:1)) ){
               printf("<br/>");
            }
            else if( *p == '\'' || *p == '"' ){
               putchar(*p);
               if(othertags == 1 ) othertags=*p;
               else if(othertags==*p) othertags=0;
            }
            else if( *p != '<' ) putchar(*p);
            else if( othertags > 1 ) printf("&lt;");
            else if( !strncasecmp( p, "</a>", 4) ){
               othertags=0;
               printf("</a>"), attr=0; p+=3;
            }
            else if( !strncasecmp( p, "<br", 3 ) ){
               othertags=0;
               if( *(p+3) == '>' ){ printf("<br/>"); p+=4; }
               else if( isspace(*(p+3)) ){ printf("<br");  p+=3; s='>'; br=1; }
            }
            else if( !strncasecmp( p, "<strong", 7 ) ){
               othertags=0;
               if( *(p+7) == '>' ){ printf("<strong>"); p+=7; }
               else if( isspace(*(p+7)) ){ printf("<strong");  p+=7; s='>'; }
            }
            else if( !strncasecmp( p, "<a", 2 ) ){
               othertags=0;
               if( *(p+2) == '>' ){ printf("<a>"); p+=2; }
               else if( isspace(*(p+2)) ){ printf("<a");  p+=2; s='@'; }
            }
            else{ printf("&lt;"); othertags=1; }
         }
         else if( s == '>' && *p == '>' ){
            if( br ) putchar('/');
            putchar(*p); s='<'; br=0;
         }
         else if( s == '@' ){
            q = *(p+5);
            if( *p == '>' ){ putchar(*p); s='<'; }
            else if( (!strncasecmp( p, "href=", 5 ) && (q == '\'' || q == '"') && (attr = 'h'))
                  || (!strncasecmp( p, "name=", 5 ) && (q == '\'' || q == '"') && (attr = 'n')) ){
               printf(" %s=%c", (*(p+4)='\0',p) , s=q ); p+=5;
            }
         }
         else if( s == '"' || s == '\'' ){
            if( !attr ){
               if (*p == '<' ){ printf("&lt;"); }
               else if( *p == '>' ){  printf("&gt;"); }
               else{ putchar(*p); }
            }
            else{
               if( *p == s ){ putchar(s); s='@'; }
               else if( *p == '\\' && *(p+1) == s ){ printf("\\%c",s), p+=1; }
               else if( attr=='h' && (tmp = strchr("!#$%'=|^\\[]`{}+<>", *p )) ){ printf("%%%X", *tmp); }
               else{ putchar(*p); }
            }
         }
      }
   }

   return 0;

}

Index

Feed

Other

Link

Pathtraq

loading...