deps/kb_meme/meme-5.0.1/src/html-monolith.c
/*
 * htmlwr_output
 * Replaces each <script>file.js</script> with the contents of file.js.
 * Writes until encountering the next script to be replaced with JSON or the end
 * of the primary source. Returns the JSON writer for the next replacement or
 * NULL on EOF.
 */
htmlwr_output
Replaces each  with the contents of file.js.
Writes until encountering the next script to be replaced with JSON or the end
of the primary source. Returns the JSON writer for the next replacement or
NULL on EOF.
htmlwr_output Replaces each with the contents of file . js . Writes until encountering the next script to be replaced with JSON or the end of the primary source . Returns the JSON writer for the next replacement or NULL on EOF .
JSONWR_T* htmlwr_output(HTMLWR_T* htmlwr) {
  int which;
  bool bad_name;
  char *name;
  const char *before, *after;
  if (htmlwr->src == NULL) die("Source file already closed.\n");
  if (htmlwr->dest == NULL) die("Destination file not set.\n");
  if (htmlwr->json_writer) {
    jsonwr_close(htmlwr->json_writer);
    htmlwr->json_writer = NULL;
    htmlwr->variable = NULL;
    fprintf(htmlwr->dest, ";\n    %s", SCRIPT_CLOSE);
  }
  while ((which = buf_fread_until(htmlwr->buffer, htmlwr->src, htmlwr->dest, 3, htmlwr->landmarks))) {
    if (which == -1) die("Error reading source\n");
    if (which == 1) {
      before = SCRIPT_START;
      after = SCRIPT_END;
    } else if (which == 2) {
      before = LINK_START;
      after = LINK_END;
    } else if (which == 3) {
      before = IMG_START;
      after = IMG_END;
    } else {
      die("Impossible state");
      return NULL; 
    }
    assert(!buf_unexpected(htmlwr->buffer, before, false));
    name = buf_fread_token(htmlwr->buffer, htmlwr->src, is_quote, NULL, false, NULL, 500, NULL);
    bad_name = !regexec_or_die("source name", &(htmlwr->source_re), name, 0, NULL, 0);
    buf_compact(htmlwr->buffer);
    while (buf_position(htmlwr->buffer) < strlen(after)) {
      if (buf_fread(htmlwr->buffer, htmlwr->src) == 0) break;
    }
    buf_flip(htmlwr->buffer);
    if (bad_name || buf_unexpected(htmlwr->buffer, after, false)) {
      if (bad_name) {
        fprintf(stderr, "Bad file name.\n");
      } else {
        fprintf(stderr, "Could not find trailing letters.\n");
      }
      fputs(before, htmlwr->dest);
      fputs(name, htmlwr->dest);
      free(name);
      continue;
    }
    if (which == 1 && (htmlwr->variable = rbtree_get(htmlwr->replace, name))) {
      fprintf(htmlwr->dest, "%s\n      %s %s\n      var %s = ", 
          SCRIPT_OPEN, JSON_VAR, htmlwr->variable, htmlwr->variable);
      htmlwr->json_writer = jsonwr_open(htmlwr->dest, htmlwr->minimize, 6, 2, 80);
      free(name);
      return htmlwr->json_writer;
    } else if (which == 3) {
      fputs(IMG_START, htmlwr->dest);
      base64enc_file(htmlwr, "image/png", name);
      fputs(IMG_END, htmlwr->dest);
      free(name);
    } else {
      echo_file(htmlwr, which-1, name);
      free(name);
    }
  }
  return NULL;
}
JSONWR_T * htmlwr_output ( HTMLWR_T * htmlwr ) { int which ; bool bad_name ; char * name ; const char * before , * after ; if ( htmlwr -> src == NULL ) die ( " \n " ) ; if ( htmlwr -> dest == NULL ) die ( " \n " ) ; if ( htmlwr -> json_writer ) { jsonwr_close ( htmlwr -> json_writer ) ; htmlwr -> json_writer = NULL ; htmlwr -> variable = NULL ; fprintf ( htmlwr -> dest , " \n " , SCRIPT_CLOSE ) ; } while ( ( which = buf_fread_until ( htmlwr -> buffer , htmlwr -> src , htmlwr -> dest , 3 , htmlwr -> landmarks ) ) ) { if ( which == -1 ) die ( " \n " ) ; if ( which == 1 ) { before = SCRIPT_START ; after = SCRIPT_END ; } else if ( which == 2 ) { before = LINK_START ; after = LINK_END ; } else if ( which == 3 ) { before = IMG_START ; after = IMG_END ; } else { die ( " " ) ; return NULL ; } assert ( ! buf_unexpected ( htmlwr -> buffer , before , false ) ) ; name = buf_fread_token ( htmlwr -> buffer , htmlwr -> src , is_quote , NULL , false , NULL , 500 , NULL ) ; bad_name = ! regexec_or_die ( " " , & ( htmlwr -> source_re ) , name , 0 , NULL , 0 ) ; buf_compact ( htmlwr -> buffer ) ; while ( buf_position ( htmlwr -> buffer ) < strlen ( after ) ) { if ( buf_fread ( htmlwr -> buffer , htmlwr -> src ) == 0 ) break ; } buf_flip ( htmlwr -> buffer ) ; if ( bad_name || buf_unexpected ( htmlwr -> buffer , after , false ) ) { if ( bad_name ) { fprintf ( stderr , " \n " ) ; } else { fprintf ( stderr , " \n " ) ; } fputs ( before , htmlwr -> dest ) ; fputs ( name , htmlwr -> dest ) ; free ( name ) ; continue ; } if ( which == 1 && ( htmlwr -> variable = rbtree_get ( htmlwr -> replace , name ) ) ) { fprintf ( htmlwr -> dest , " \n \n " , SCRIPT_OPEN , JSON_VAR , htmlwr -> variable , htmlwr -> variable ) ; htmlwr -> json_writer = jsonwr_open ( htmlwr -> dest , htmlwr -> minimize , 6 , 2 , 80 ) ; free ( name ) ; return htmlwr -> json_writer ; } else if ( which == 3 ) { fputs ( IMG_START , htmlwr -> dest ) ; base64enc_file ( htmlwr , " " , name ) ; fputs ( IMG_END , htmlwr -> dest ) ; free ( name ) ; } else { echo_file ( htmlwr , which - 1 , name ) ; free ( name ) ; } } return NULL ; }
htmlwr_output
 Replaces each <script>file.js</script> with the contents of file.js.
htmlwr_output Replaces each <script > file . js< / script > with the contents of file . js .
// this may be a secondary invocation, so check if we were replacing with JSON // stop compiler complaining // ensure we have read enough to check the after chars // check everything matches