📄 pprint.c
字号:
/* pprint.c -- pretty print parse tree (c) 1998-2000 (W3C) MIT, INRIA, Keio University See tidy.c for the copyright notice.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "platform.h"#include "html.h"/* Block-level and unknown elements are printed on new lines and their contents indented 2 spaces Inline elements are printed inline. Inline content is wrapped on spaces (except in attribute values or preformatted text, after start tags and before end tags*/#define NORMAL 0#define PREFORMATTED 1#define COMMENT 2#define ATTRIBVALUE 4#define NOWRAP 8#define CDATA 16extern int CharEncoding;static uint *linebuf;static uint lbufsize;static uint linelen;static uint wraphere;static Bool InAttVal;static Bool InString;static int slide, count;static Node *slidecontent;int foo; /* debug *//* 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F*//* return one less that the number of bytes used by UTF-8 char *//* str points to 1st byte, *ch initialized to 1st byte */uint GetUTF8(unsigned char *str, uint *ch){ uint c, n, i, count; c = str[0]; if ((c & 0xE0) == 0xC0) /* 110X XXXX two bytes */ { n = c & 31; count = 1; } else if ((c & 0xF0) == 0xE0) /* 1110 XXXX three bytes */ { n = c & 15; count = 2; } else if ((c & 0xF8) == 0xF0) /* 1111 0XXX four bytes */ { n = c & 7; count = 3; } else if ((c & 0xFC) == 0xF8) /* 1111 10XX five bytes */ { n = c & 3; count = 4; } else if ((c & 0xFE) == 0xFC) /* 1111 110X six bytes */ { n = c & 1; count = 5; } else /* 0XXX XXXX one byte */ { *ch = c; return 0; } /* successor bytes should have the form 10XX XXXX */ for (i = 1; i <= count; ++i) { c = str[i]; n = (n << 6) | (c & 0x3F); } *ch = n; return count;}void FreePrintBuf(void){ if (linebuf) MemFree(linebuf);}void AddC(uint c, uint index){ if (index + 1 >= lbufsize) { while (index + 1 >= lbufsize) { if (lbufsize == 0) lbufsize = 256; else lbufsize = lbufsize * 2; } linebuf = (uint *)MemRealloc(linebuf, lbufsize*sizeof(uint)); } linebuf[index] = (uint)c;}void WrapLine(Out *fout, uint indent){ uint i, *p, *q; if (wraphere == 0) return; for (i = 0; i < indent; ++i) outc(' ', fout); for (i = 0; i < wraphere; ++i) outc(linebuf[i], fout); if (InString) { outc(' ', fout); outc('\\', fout); } outc('\n', fout); if (linelen > wraphere) { p = linebuf; if (linebuf[wraphere] == ' ') ++wraphere; q = linebuf + wraphere; AddC('\0', linelen); while ((*p++ = *q++)); linelen -= wraphere; } else linelen = 0; wraphere = 0;}void WrapAttrVal(Out *fout, uint indent, Bool inString){ uint i, *p, *q; for (i = 0; i < indent; ++i) outc(' ', fout); for (i = 0; i < wraphere; ++i) outc(linebuf[i], fout); outc(' ', fout); if (inString) outc('\\', fout); outc('\n', fout); if (linelen > wraphere) { p = linebuf; if (linebuf[wraphere] == ' ') ++wraphere; q = linebuf + wraphere; AddC('\0', linelen); while ((*p++ = *q++)); linelen -= wraphere; } else linelen = 0; wraphere = 0;}void PFlushLine(Out *fout, uint indent){ uint i; if (linelen > 0) { if (indent + linelen >= wraplen) WrapLine(fout, indent); if (!InAttVal || IndentAttributes) { for (i = 0; i < indent; ++i) outc(' ', fout); } for (i = 0; i < linelen; ++i) outc(linebuf[i], fout); } outc('\n', fout); linelen = wraphere = 0; InAttVal = no;}void PCondFlushLine(Out *fout, uint indent){ uint i; if (linelen > 0) { if (indent + linelen >= wraplen) WrapLine(fout, indent); if (!InAttVal || IndentAttributes) { for (i = 0; i < indent; ++i) outc(' ', fout); } for (i = 0; i < linelen; ++i) outc(linebuf[i], fout); outc('\n', fout); linelen = wraphere = 0; InAttVal = no; }}void PPrintChar(uint c, uint mode){ char *p, entity[128]; if (c == ' ' && !(mode & (PREFORMATTED | COMMENT | ATTRIBVALUE))) { /* coerce a space character to a non-breaking space */ if (mode & NOWRAP) { /* by default XML doesn't define */ if (NumEntities || XmlTags) { AddC('&', linelen++); AddC('#', linelen++); AddC('1', linelen++); AddC('6', linelen++); AddC('0', linelen++); AddC(';', linelen++); } else /* otherwise use named entity */ { AddC('&', linelen++); AddC('n', linelen++); AddC('b', linelen++); AddC('s', linelen++); AddC('p', linelen++); AddC(';', linelen++); } return; } else wraphere = linelen; } /* comment characters are passed raw */ if (mode & COMMENT) { AddC(c, linelen++); return; } /* except in CDATA map < to < etc. */ if (! (mode & CDATA) ) { if (c == '<') { AddC('&', linelen++); AddC('l', linelen++); AddC('t', linelen++); AddC(';', linelen++); return; } if (c == '>') { AddC('&', linelen++); AddC('g', linelen++); AddC('t', linelen++); AddC(';', linelen++); return; } /* naked '&' chars can be left alone or quoted as & The latter is required for XML where naked '&' are illegal. */ if (c == '&' && QuoteAmpersand) { AddC('&', linelen++); AddC('a', linelen++); AddC('m', linelen++); AddC('p', linelen++); AddC(';', linelen++); return; } if (c == '"' && QuoteMarks) { AddC('&', linelen++); AddC('q', linelen++); AddC('u', linelen++); AddC('o', linelen++); AddC('t', linelen++); AddC(';', linelen++); return; } if (c == '\'' && QuoteMarks) { AddC('&', linelen++); AddC('#', linelen++); AddC('3', linelen++); AddC('9', linelen++); AddC(';', linelen++); return; } if (c == 160 && CharEncoding != RAW) { if (QuoteNbsp) { AddC('&', linelen++); if (NumEntities) { AddC('#', linelen++); AddC('1', linelen++); AddC('6', linelen++); AddC('0', linelen++); } else { AddC('n', linelen++); AddC('b', linelen++); AddC('s', linelen++); AddC('p', linelen++); } AddC(';', linelen++); } else AddC(c, linelen++); return; } } /* otherwise ISO 2022 characters are passed raw */ if (CharEncoding == ISO2022 || CharEncoding == RAW) { AddC(c, linelen++); return; } /* if preformatted text, map to space */ if (c == 160 && (mode & PREFORMATTED)) { AddC(' ', linelen++); return; } /* Filters from Word and PowerPoint often use smart quotes resulting in character codes between 128 and 159. Unfortunately, the corresponding HTML 4.0 entities for these are not widely supported. The following converts dashes and quotation marks to the nearest ASCII equivalent. */ if (MakeClean) { if (c > 126 && c < 160) { if (c == 8211) c = '-'; /* en dash */ else if (c == 8212) c = '-'; /* em dash */ else if (c == 8216) c = '\''; /* single left quotation mark */ else if (c == 8217) c = '\''; /* single right quotation mark */ else if (c == 8220) c = '"'; /* double left quotation mark */ else if (c == 8221) c = '"'; /* double right quotation mark */ } } /* don't map latin-1 chars to entities */ if (CharEncoding == LATIN1) { if (c > 255) /* multi byte chars */ { if (!NumEntities && (p = EntityName(c)) != null) sprintf(entity, "&%s;", p); else sprintf(entity, "&#%u;", c); for (p = entity; *p; ++p) AddC(*p, linelen++); return; } if (c > 126 && c < 160) { sprintf(entity, "&#%d;", c); for (p = entity; *p; ++p) AddC(*p, linelen++); return; } AddC(c, linelen++); return; } /* don't map utf8 chars to entities */ if (CharEncoding == UTF8) { AddC(c, linelen++); return; } /* use numeric entities only for XML */ if (XmlTags) { /* if ASCII use numeric entities for chars > 127 */ if (c > 127 && CharEncoding == ASCII) { sprintf(entity, "&#%u;", c); for (p = entity; *p; ++p) AddC(*p, linelen++); return; } /* otherwise output char raw */ AddC(c, linelen++); return; } /* default treatment for ASCII */ if (c > 126 || (c < ' ' && c != '\t')) { if (!NumEntities && (p = EntityName(c)) != null) sprintf(entity, "&%s;", p); else sprintf(entity, "&#%u;", c); for (p = entity; *p; ++p) AddC(*p, linelen++); return; } AddC(c, linelen++);}/* The line buffer is uint not char so we can hold Unicode values unencoded. The translation to UTF-8 is deferred to the outc routine called to flush the line buffer.*/void PPrintText(Out *fout, uint mode, uint indent, Lexer *lexer, uint start, uint end){ uint i, c; for (i = start; i < end; ++i) { if (indent + linelen >= wraplen) WrapLine(fout, indent); c = (unsigned char)lexer->lexbuf[i]; /* look for UTF-8 multibyte character */ if (c > 0x7F) i += GetUTF8((unsigned char *)lexer->lexbuf + i, &c); if (c == '\n') { PFlushLine(fout, indent); continue; } PPrintChar(c, mode); }}void PPrintString(Out *fout, uint indent, char *str){ while (*str != '\0') AddC(*str++, linelen++);}void PPrintAttrValue(Out *fout, uint indent, char *value, int delim, Bool wrappable){ uint c; Bool wasinstring = no; int mode = (wrappable ? (NORMAL | ATTRIBVALUE) : (PREFORMATTED | ATTRIBVALUE)); /* look for ASP, Tango or PHP instructions for computed attribute value */ if (value && value[0] == '<') { if (value[1] == '%' || value[1] == '@'|| wstrncmp(value, "<?php", 5) == 0) mode |= CDATA; } if (delim == null) delim = '"'; AddC('=', linelen++); /* don't wrap after "=" for xml documents */ if (!XmlOut) { if (indent + linelen < wraplen) wraphere = linelen; if (indent + linelen >= wraplen) WrapLine(fout, indent);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -