Exemplo n.º 1
0
YelpDocument *
yelp_document_get_for_uri (YelpUri *uri)
{
    static GHashTable *documents = NULL;
    gchar *docuri = NULL;
    gchar *page_id, *tmp;
    YelpDocument *document = NULL;

    if (documents == NULL)
        documents = g_hash_table_new_full (g_str_hash, g_str_equal,
                                           g_free, g_object_unref);

    g_return_val_if_fail (yelp_uri_is_resolved (uri), NULL);

    switch (yelp_uri_get_document_type (uri)) {
    case YELP_URI_DOCUMENT_TYPE_TEXT:
    case YELP_URI_DOCUMENT_TYPE_HTML:
    case YELP_URI_DOCUMENT_TYPE_XHTML:
        /* We use YelpSimpleDocument for these, which is a single-file
         * responder. But the document URI may be set to the directory
         * holding the file, to allow a directory of HTML files to act
         * as a single document. So we cache these by a fuller URI.
         */
        docuri = yelp_uri_get_document_uri (uri);
        page_id = yelp_uri_get_page_id (uri);
        tmp = g_strconcat (docuri, "/", page_id, NULL);
        g_free (docuri);
        g_free (page_id);
        docuri = tmp;
        break;
    case YELP_URI_DOCUMENT_TYPE_MAN:
        /* The document URI for man pages is just man:, so we use the
         * full canonical URI to look these up.
         */
        docuri = yelp_uri_get_canonical_uri (uri);
        break;
    default:
        docuri = yelp_uri_get_document_uri (uri);
        break;
    }

    if (docuri == NULL)
        return NULL;
    document = g_hash_table_lookup (documents, docuri);

    if (document != NULL) {
        g_free (docuri);
        return g_object_ref (document);
    }

    switch (yelp_uri_get_document_type (uri)) {
    case YELP_URI_DOCUMENT_TYPE_TEXT:
    case YELP_URI_DOCUMENT_TYPE_HTML:
    case YELP_URI_DOCUMENT_TYPE_XHTML:
        document = yelp_simple_document_new (uri);
        break;
    case YELP_URI_DOCUMENT_TYPE_DOCBOOK:
        document = yelp_docbook_document_new (uri);
        break;
    case YELP_URI_DOCUMENT_TYPE_MALLARD:
        document = yelp_mallard_document_new (uri);
        break;
    case YELP_URI_DOCUMENT_TYPE_MAN:
        document = yelp_man_document_new (uri);
        break;
    case YELP_URI_DOCUMENT_TYPE_INFO:
        document = yelp_info_document_new (uri);
        break;
    case YELP_URI_DOCUMENT_TYPE_HELP_LIST:
        document = yelp_help_list_new (uri);
        break;
    case YELP_URI_DOCUMENT_TYPE_NOT_FOUND:
    case YELP_URI_DOCUMENT_TYPE_EXTERNAL:
    case YELP_URI_DOCUMENT_TYPE_ERROR:
        break;
    }

    if (document != NULL) {
        g_hash_table_insert (documents, docuri, document);
        return g_object_ref (document);
    }

    g_free (docuri);
    return NULL;
}
Exemplo n.º 2
0
static void
print_uri (gchar *orig, YelpUri *uri, GOutputStream *stream)
{
    GFile *file;
    const gchar *type = NULL;
    gchar *tmp, **tmpv, *out;

    g_output_stream_write (stream, orig, strlen (orig), NULL, NULL);
    g_output_stream_write (stream, "\n", 1, NULL, NULL);

    switch (yelp_uri_get_document_type (uri)) {
    case YELP_URI_DOCUMENT_TYPE_DOCBOOK:
        type = "DOCBOOK";
        break;
    case YELP_URI_DOCUMENT_TYPE_MALLARD:
        type = "MALLARD";
        break;
    case YELP_URI_DOCUMENT_TYPE_MAN:
        type = "MAN";
        break;
    case YELP_URI_DOCUMENT_TYPE_INFO:
        type = "INFO";
        break;
    case YELP_URI_DOCUMENT_TYPE_TEXT:
        type = "TEXT";
        break;
    case YELP_URI_DOCUMENT_TYPE_HTML:
        type = "HTML";
        break;
    case YELP_URI_DOCUMENT_TYPE_XHTML:
        type = "XHTML";
        break;
    case YELP_URI_DOCUMENT_TYPE_HELP_LIST:
        type = "TOC";
        break;
    case YELP_URI_DOCUMENT_TYPE_NOT_FOUND:
        type = "NOT FOUND";
        break;
    case YELP_URI_DOCUMENT_TYPE_EXTERNAL:
        type = "EXTERNAL";
        break;
    case YELP_URI_DOCUMENT_TYPE_ERROR:
        type = "ERROR";
        break;
    case YELP_URI_DOCUMENT_TYPE_UNRESOLVED:
        type = "UNRESOLVED";
        break;
    default:
        g_assert_not_reached ();
        break;
    }

    out = g_strdup_printf ("DOCUMENT TYPE: %s\n", type);
    g_output_stream_write (stream, out, strlen (out), NULL, NULL);
    g_free (out);

    tmp = yelp_uri_get_document_uri (uri);
    if (tmp) {
        out = g_strdup_printf ("DOCUMENT URI:  %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    tmp = yelp_uri_get_canonical_uri (uri);
    if (tmp) {
        out = g_strdup_printf ("CANONICAL URI: %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    file = yelp_uri_get_file (uri);
    if (file) {
        tmp = g_file_get_uri (file);
        out = g_strdup_printf ("FILE URI:      %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
        g_object_unref (file);
    }

    tmpv = yelp_uri_get_search_path (uri);
    if (tmpv) {
        int i;
        for (i = 0; tmpv[i]; i++) {
            if (i == 0)
                out = g_strdup_printf ("SEARCH PATH:   %s\n", tmpv[i]);
            else
                out = g_strdup_printf ("               %s\n", tmpv[i]);
            g_output_stream_write (stream, out, strlen (out), NULL, NULL);
            g_free (out);
        }
        g_strfreev (tmpv);
    }

    tmp = yelp_uri_get_page_id (uri);
    if (tmp) {
        out = g_strdup_printf ("PAGE ID:       %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    tmp = yelp_uri_get_frag_id (uri);
    if (tmp) {
        out = g_strdup_printf ("FRAG ID:       %s\n", tmp);
        g_output_stream_write (stream, out, strlen (out), NULL, NULL);
        g_free (out);
        g_free (tmp);
    }

    g_output_stream_write (stream, "\0", 1, NULL, NULL);
}