Skip to content

Instantly share code, notes, and snippets.

@Orc
Last active May 15, 2017 21:19
Show Gist options
  • Save Orc/ff0222915e5f9b3a13b3d786982b6122 to your computer and use it in GitHub Desktop.
Save Orc/ff0222915e5f9b3a13b3d786982b6122 to your computer and use it in GitHub Desktop.
Q&D html stripper for mutt
#include <stdio.h>
#include <string.h>
actualspace(register c)
{
return (c=='\r') || (c=='\n') || isspace(c);
}
int
thesame(char *a, char *b, int len)
{
return (strlen(a) == len) && (strncasecmp(a,b,len) == 0);
}
eatuntil(char *block)
{
char end[80];
int i = 0;
register c;
do {
while ( (c=getchar()) != EOF && c != '<' )
;
if ( c == EOF )
return;
i = 0;
while ( (c=getchar()) != EOF && c != '>' ) {
if ( actualspace(c) || i >= sizeof end-1 )
break;
else
end[i++] = c;
}
while ( c != '>' && (c=getchar()) != EOF )
;
if ( thesame(block, end, i) )
return;
} while (1);
}
comment()
{
register c;
int cd = 0;
while ( (c=getchar()) != EOF ) {
if ( c == '>' ) {
if ( cd == 2 )
return;
cd = 0;
}
else if ( c == '-')
cd++;
else
cd = 0;
}
}
mimeblock()
{
char tag[80];
int i = 0;
register c;
while ( (c=getchar()) != EOF && c != '>' ) {
if ( actualspace(c) || i >= sizeof tag-1 )
break;
else
tag[i++] = c;
}
if ( thesame("!--", tag, i ) ) {
comment();
return;
}
while ( c != '>' && (c=getchar()) != EOF )
;
if ( thesame("head", tag, i) )
eatuntil("/head");
else if ( thesame("style", tag, i) )
eatuntil("/style");
}
entity()
{
register c;
while ( (c=getchar()) != EOF && c != ';' && !isspace(c) )
;
if ( c != EOF && c != ';' )
ungetc(c, stdin);
}
main()
{
register c;
int squashing = 0;
while ( (c=getchar()) != EOF ) {
if ( c == '<' )
mimeblock();
else if ( c == '&' )
entity();
else if ( actualspace(c) ) {
if ( !squashing )
putchar(c);
squashing=1;
}
else {
squashing=0;
putchar(c);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment