[ncman] lex out comments from troff data

This commit is contained in:
nick black 2021-12-04 14:53:05 -05:00 committed by nick black
parent e9cc8d0b2e
commit c32e9b3791

View File

@ -124,39 +124,73 @@ get_troff_data(const char *arg, size_t* len){
return buf; return buf;
} }
typedef enum {
LINE_UNKNOWN,
LINE_COMMENT,
LINE_B, LINE_BI, LINE_BR, LINE_I, LINE_IB, LINE_IR,
LINE_RB, LINE_RI, LINE_SB, LINE_SM,
LINE_EE, LINE_EX, LINE_RE, LINE_RS,
LINE_SH, LINE_SS, LINE_TH,
LINE_IP, LINE_LP, LINE_P, LINE_PP,
LINE_TP, LINE_TQ,
LINE_ME, LINE_MT, LINE_UE, LINE_UR,
LINE_OP, LINE_SY, LINE_YS,
} ltypes;
// get the linetype from the leader terminating at |ws|. we are guaranteed to
// have a period prior to |ws| within the commonly addressable area, so we
// match backwards until that period.
static ltypes
get_type(const unsigned char* ws){
(void)ws;
return LINE_B; // FIXME
}
// calculate the number of rows necessary to display the troff data, // calculate the number of rows necessary to display the troff data,
// assuming the specified width |dimx|. // assuming the specified width |dimx|.
static int static int
troff_height(unsigned dimx, const unsigned char* map, size_t mlen){ troff_height(unsigned dimx, const unsigned char* map, size_t mlen){
// FIXME for now we assume infinitely wide lines // FIXME for now we assume infinitely wide lines
int lines = 0; int lines = 0;
ltypes linetype = LINE_UNKNOWN;
enum { enum {
LINE_UNKNOWN, GOT_NULL, // line has started
LINE_COMMENT, GOT_DOT, // got period to start line
LINE_B, LINE_BI, LINE_BR, LINE_I, LINE_IB, LINE_IR, GOT_LETTER, // got letter following dot
LINE_RB, LINE_RI, LINE_SB, LINE_SM, GOT_SLASH, // got slash following dot (possible comment)
LINE_EE, LINE_EX, LINE_RE, LINE_RS, GOT_INVALID, // line is bad
LINE_SH, LINE_SS, LINE_TH, } state = GOT_NULL;
LINE_IP, LINE_LP, LINE_P, LINE_PP,
LINE_TP, LINE_TQ,
LINE_ME, LINE_MT, LINE_UE, LINE_UR,
LINE_OP, LINE_SY, LINE_YS,
} linetype = LINE_UNKNOWN;
const unsigned char comment[] = ".\\\""; // dot slash quot
const unsigned char* comiter = comment;
for(size_t off = 0 ; off < mlen ; ++off){ for(size_t off = 0 ; off < mlen ; ++off){
if(map[off] == '\n'){ if(map[off] == '\n'){
if(linetype != LINE_UNKNOWN && linetype != LINE_COMMENT){ if(linetype != LINE_UNKNOWN && linetype != LINE_COMMENT){
++lines; ++lines;
} }
linetype = LINE_UNKNOWN; linetype = LINE_UNKNOWN;
comiter = comment; state = GOT_NULL;
}else if(linetype == LINE_UNKNOWN){ }else if(linetype == LINE_UNKNOWN){
if(comiter && map[off] == *comiter){ if(state == GOT_NULL){
if(*++comiter == '\0'){ if(map[off] != '.'){
state = GOT_INVALID;
}else{
state = GOT_DOT;
}
}else if(state == GOT_DOT){
if(map[off] == '\\'){
state = GOT_SLASH;
}else if(!isalpha(map[off])){
state = GOT_INVALID;
}else{
state = GOT_LETTER;
}
}else if(state == GOT_LETTER){
if(isspace(map[off])){
linetype = get_type(map + off);
}
}else if(state == GOT_SLASH){
if(map[off] == '"'){
linetype = LINE_COMMENT; linetype = LINE_COMMENT;
}else{ }else{
comiter = NULL; state = GOT_INVALID;
} }
} }
} }
@ -174,6 +208,7 @@ render_troff(struct notcurses* nc, const unsigned char* map, size_t mlen){
unsigned dimy, dimx; unsigned dimy, dimx;
struct ncplane* stdn = notcurses_stddim_yx(nc, &dimy, &dimx); struct ncplane* stdn = notcurses_stddim_yx(nc, &dimy, &dimx);
int rows = troff_height(dimx, map, mlen); int rows = troff_height(dimx, map, mlen);
fprintf(stderr, "ROWS: %d\n", rows);
struct ncplane_options popts = { struct ncplane_options popts = {
.rows = rows, .rows = rows,
.cols = dimx, .cols = dimx,