Logo Search packages:      
Sourcecode: rapple version File versions  Download package

rpl_wk_status rpl_mod_index_process ( rpl_c_str_t  filename,
struct stat  statbuf 
)

Parameters:
filename name of file to be registered.
st_buf stat of file.

Definition at line 146 of file mod_index.c.

References rpl_digest_get_summary(), rpl_digest_get_title(), rpl_digest_parse(), rpl_fs_resolve_paths(), rpl_fs_str2f(), rpl_html_create_hyperlink(), rpl_html_create_more_hyperlink(), rpl_index_do_index(), rpl_index_escape_ampersands(), rpl_log_error(), rpl_log_fatal(), rpl_log_info(), rpl_me_free(), rpl_me_malloc(), rpl_message_get(), rpl_mime_get_type(), rpl_mime_is_transformable(), rpl_reg_insert(), rpl_str_concat(), rpl_wa_init(), rpl_wa_set_filename(), rpl_wa_set_key(), rpl_wa_set_mime_type(), and rpl_wa_set_rel_dir().

Referenced by rpl_mod_index_configure().

{
      rpl_wk_status status = RPL_WK_OK;
      rpl_web_asset *wa;
      rpl_str_t msg, rdp, fp, idx_path, dgst_filename, rel_path, out_filename;
      rpl_str_t idx_html, title, list_html;
    struct dirent *dirp;
    DIR *dp;
    struct stat sub_statbuf;

    assert(filename != NULL);

      msg = rpl_str_concat(rpl_message_get("WK_PROCESSING", RPL_EOM), "index ", filename, RPL_STR_EOC);
      rpl_log_info(msg);
      rpl_me_free(msg);

      /* if not configured for indexing then don't even bother processing dirs */
      if(enable_indexing)
      {
            if(S_ISREG(statbuf.st_mode))
            {
                  /* processing directories to be indexed rather than files that are to be
                     included in directory indexes negates the need maintain state 
                     information during processing. */
            } else if(S_ISDIR(statbuf.st_mode)) {

                  if(rpl_fs_resolve_paths(filename, rpl_index_base_dir, &rdp, &fp))
                        return RPL_WK_ERR;
                  idx_path = rpl_reg_create_key(rdp, fp);

                  /* dirs configured for indexing are stated relative to the website root */
                  if((title = rpl_index_do_index(idx_path)) != RPL_STR_NUL)
                  {
                        /* TODO: contributor is the process owner */
                        wa = (rpl_web_asset *)rpl_me_malloc(sizeof(rpl_web_asset));
                        rpl_wa_init(wa);

                        /* set mime type based on filename extension */
                        rpl_wa_set_mime_type("text/html", wa);
                        rpl_wa_set_rel_dir(idx_path, wa);
                        rpl_wa_set_filename(RPL_INDEX_INDEXNM, wa);
                        rpl_wa_set_key(wa);

                        /* digest each file in the directory */
                        idx_html = rpl_str_concat(RPL_INDEX_HTML_ST_1, title, 
                                    RPL_INDEX_HTML_ST_2, title, RPL_INDEX_HTML_ST_3, RPL_STR_EOC);

                        /* read and digest directory contents */
                        dp=opendir(filename);
                        while ((dirp = readdir (dp)) != NULL) {
                              /* ignore the usual suspects (incl. the generated index if it already exists!) */
                              if (strcmp (dirp->d_name, ".") == 0 || 
                                          strcmp (dirp->d_name, "..") == 0 || 
                                          strcmp(dirp->d_name, RPL_INDEX_INDEXNM) == 0)
                                    continue;
                              dgst_filename = rpl_str_concat(filename, "/", dirp->d_name, RPL_STR_EOC);
                              /* perform digest parsing (but check first that we are not dealing with another directory */
                              if(stat(dgst_filename,&sub_statbuf) == -1) {
                                    msg = rpl_message_get("DIGEST_INDEX_FAILED", filename, " (", strerror(errno), ")", RPL_EOM);
                                    rpl_log_error(msg);
                                    rpl_me_free(dgst_filename);
                                    return RPL_WK_ERR;
                              }
                              /* only attempt to digest assets that are regular transformable files */
                              if(S_ISREG(sub_statbuf.st_mode) && rpl_mime_is_transformable(rpl_mime_get_type(dgst_filename))) {
                                    rpl_digest_parse(dgst_filename);
                                    rel_path = rpl_str_concat(idx_path, "/", dirp->d_name, RPL_STR_EOC);
                                    list_html = rpl_str_concat("<li><b>",
                                                rpl_html_create_hyperlink(rel_path, rpl_digest_get_title()),
                                                "</b><br /><i>",
                                                rpl_digest_get_summary(), "...",
                                                rpl_html_create_more_hyperlink(rel_path),
                                                "</i></li>", 
                                                RPL_STR_EOC);
                                    idx_html = rpl_str_concat(idx_html, list_html, RPL_STR_EOC);
                                    rpl_me_free(list_html);
                                    rpl_me_free(rel_path);
                              }
                              rpl_me_free(dgst_filename);
                        }

                        idx_html = rpl_str_concat(idx_html, RPL_INDEX_HTML_END, RPL_STR_EOC);

                        /* output generated index file */
                        out_filename = rpl_str_concat(out_dir, "/", idx_path, "/", RPL_INDEX_INDEXNM, RPL_STR_EOC);
                        rpl_index_escape_ampersands(&idx_html);
                        rpl_fs_str2f(idx_html, out_filename);

                        /* tidy up */
                        if (closedir (dp) < 0) {
                              msg = rpl_message_get("DIGEST_DIR_CLOSE_FAILED", filename, RPL_EOM);
                              fprintf(stderr, rpl_str_concat("Fatal: ", msg, RPL_STR_EOC));
                              rpl_log_fatal(msg);
                        }

                        /* register newly generated asset */
                        rpl_reg_insert(wa);

                        rpl_me_free(idx_html);
                        rpl_me_free(out_filename);
                  }
                  
                  if(strlen(rdp) > 0)
                        rpl_me_free(rdp);
                  if(strlen(fp) > 0)
                        rpl_me_free(fp);
                  rpl_me_free(idx_path);
            }
      }

      return status; 
}


Generated by  Doxygen 1.6.0   Back to index