2007-08-28 11:31:43 -04:00
|
|
|
/* DOM-based RSS renderer */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
2007-08-28 13:38:13 -04:00
|
|
|
#include "document/css/css.h"
|
|
|
|
#include "document/css/parser.h"
|
|
|
|
#include "document/css/stylesheet.h"
|
2007-08-28 11:31:43 -04:00
|
|
|
#include "document/document.h"
|
|
|
|
#include "document/dom/util.h"
|
|
|
|
#include "document/dom/rss.h"
|
|
|
|
#include "dom/sgml/rss/rss.h"
|
|
|
|
#include "dom/node.h"
|
|
|
|
#include "dom/stack.h"
|
|
|
|
#include "intl/charsets.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
|
|
|
|
|
2007-08-28 13:38:13 -04:00
|
|
|
enum rss_style {
|
|
|
|
RSS_STYLE_TITLE,
|
2007-08-28 15:34:32 -04:00
|
|
|
RSS_STYLE_AUTHOR,
|
|
|
|
RSS_STYLE_AUTHOR_DATE_SEP,
|
|
|
|
RSS_STYLE_DATE,
|
2007-08-28 13:38:13 -04:00
|
|
|
RSS_STYLES,
|
|
|
|
};
|
2007-08-28 11:31:43 -04:00
|
|
|
|
2007-08-28 13:57:45 -04:00
|
|
|
struct rss_renderer {
|
2007-08-29 06:02:36 -04:00
|
|
|
/* The current item being processed; can be either a channel or
|
|
|
|
* item element. */
|
2007-08-28 13:57:45 -04:00
|
|
|
struct dom_node *item;
|
2007-08-29 06:02:36 -04:00
|
|
|
|
|
|
|
/* One style per node type. */
|
|
|
|
struct screen_char styles[RSS_STYLES];
|
2007-08-28 13:57:45 -04:00
|
|
|
};
|
|
|
|
|
2007-08-28 11:31:43 -04:00
|
|
|
|
2007-08-28 20:29:58 -04:00
|
|
|
static struct dom_string *
|
|
|
|
get_rss_text(struct dom_node *node, enum rss_element_type type)
|
|
|
|
{
|
|
|
|
node = get_dom_node_child(node, DOM_NODE_ELEMENT, type);
|
|
|
|
|
|
|
|
if (!node) return NULL;
|
|
|
|
|
|
|
|
node = get_dom_node_child(node, DOM_NODE_TEXT, 0);
|
|
|
|
|
|
|
|
return node ? &node->string: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
render_rss_item(struct dom_renderer *renderer, struct dom_node *item)
|
|
|
|
{
|
|
|
|
struct rss_renderer *rss = renderer->data;
|
|
|
|
struct dom_string *title = get_rss_text(item, RSS_ELEMENT_TITLE);
|
|
|
|
struct dom_string *link = get_rss_text(item, RSS_ELEMENT_LINK);
|
|
|
|
struct dom_string *author = get_rss_text(item, RSS_ELEMENT_AUTHOR);
|
|
|
|
struct dom_string *date = get_rss_text(item, RSS_ELEMENT_PUBDATE);
|
|
|
|
|
2007-08-29 05:07:02 -04:00
|
|
|
if (item->data.element.type == RSS_ELEMENT_ITEM) {
|
|
|
|
Y(renderer)++;
|
|
|
|
X(renderer) = 0;
|
|
|
|
}
|
|
|
|
|
2007-08-28 20:29:58 -04:00
|
|
|
if (title && is_dom_string_set(title)) {
|
2007-08-29 05:07:02 -04:00
|
|
|
if (item->data.element.type == RSS_ELEMENT_CHANNEL) {
|
2021-01-02 10:20:27 -05:00
|
|
|
char *str;
|
2007-08-28 20:29:58 -04:00
|
|
|
|
|
|
|
str = convert_string(renderer->convert_table,
|
|
|
|
title->string, title->length,
|
|
|
|
renderer->document->options.cp,
|
|
|
|
CSM_DEFAULT, NULL, NULL, NULL);
|
|
|
|
if (str)
|
|
|
|
renderer->document->title = str;
|
|
|
|
}
|
|
|
|
render_dom_text(renderer, &rss->styles[RSS_STYLE_TITLE],
|
|
|
|
title->string, title->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link && is_dom_string_set(link)) {
|
|
|
|
X(renderer)++;
|
|
|
|
add_dom_link(renderer, "[link]", 6, link->string, link->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* New line, and indent */
|
|
|
|
Y(renderer)++;
|
|
|
|
X(renderer) = 0;
|
|
|
|
|
|
|
|
if (author && is_dom_string_set(author)) {
|
|
|
|
render_dom_text(renderer, &rss->styles[RSS_STYLE_AUTHOR],
|
|
|
|
author->string, author->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (date && is_dom_string_set(date)) {
|
|
|
|
if (author && is_dom_string_set(author)) {
|
|
|
|
render_dom_text(renderer, &rss->styles[RSS_STYLE_AUTHOR_DATE_SEP],
|
|
|
|
" - ", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
render_dom_text(renderer, &rss->styles[RSS_STYLE_DATE],
|
|
|
|
date->string, date->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((author && is_dom_string_set(author))
|
|
|
|
|| (date && is_dom_string_set(date))) {
|
|
|
|
/* New line, and indent */
|
|
|
|
Y(renderer)++;
|
|
|
|
X(renderer) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-29 05:07:02 -04:00
|
|
|
static void
|
|
|
|
flush_rss_item(struct dom_renderer *renderer, struct rss_renderer *rss)
|
|
|
|
{
|
|
|
|
if (rss->item) {
|
|
|
|
render_rss_item(renderer, rss->item);
|
|
|
|
rss->item = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-28 20:29:58 -04:00
|
|
|
|
2007-08-28 11:37:06 -04:00
|
|
|
static enum dom_code
|
2007-08-28 13:57:45 -04:00
|
|
|
dom_rss_push_element(struct dom_stack *stack, struct dom_node *node, void *xxx)
|
2007-08-28 11:31:43 -04:00
|
|
|
{
|
|
|
|
struct dom_renderer *renderer = stack->current->data;
|
2007-08-28 20:26:53 -04:00
|
|
|
struct rss_renderer *rss = renderer->data;
|
2007-08-28 11:31:43 -04:00
|
|
|
|
2007-08-28 20:03:08 -04:00
|
|
|
assert(node && node->parent && renderer && renderer->document);
|
2007-08-28 11:31:43 -04:00
|
|
|
|
|
|
|
switch (node->data.element.type) {
|
|
|
|
case RSS_ELEMENT_CHANNEL:
|
|
|
|
/* The stack should have: #document * channel */
|
2007-08-29 05:07:02 -04:00
|
|
|
if (stack->depth == 3)
|
|
|
|
rss->item = node;
|
2007-08-28 11:31:43 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RSS_ELEMENT_ITEM:
|
2007-08-29 05:07:02 -04:00
|
|
|
flush_rss_item(renderer, rss);
|
|
|
|
rss->item = node;
|
2007-08-28 11:31:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return DOM_CODE_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-28 11:37:06 -04:00
|
|
|
static enum dom_code
|
2007-08-28 13:57:45 -04:00
|
|
|
dom_rss_pop_element(struct dom_stack *stack, struct dom_node *node, void *xxx)
|
2007-08-28 11:31:43 -04:00
|
|
|
{
|
|
|
|
struct dom_renderer *renderer = stack->current->data;
|
2007-08-28 20:26:53 -04:00
|
|
|
struct rss_renderer *rss = renderer->data;
|
2007-08-28 11:31:43 -04:00
|
|
|
|
2007-08-28 20:03:08 -04:00
|
|
|
assert(node && node->parent && renderer && renderer->document);
|
2007-08-28 11:31:43 -04:00
|
|
|
|
|
|
|
switch (node->data.element.type) {
|
2007-08-29 05:07:02 -04:00
|
|
|
case RSS_ELEMENT_CHANNEL:
|
|
|
|
flush_rss_item(renderer, rss);
|
|
|
|
break;
|
2007-08-28 11:31:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return DOM_CODE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-28 13:38:13 -04:00
|
|
|
static enum dom_code
|
2007-08-28 13:57:45 -04:00
|
|
|
dom_rss_push_document(struct dom_stack *stack, struct dom_node *root, void *xxx)
|
2007-08-28 13:38:13 -04:00
|
|
|
{
|
|
|
|
struct dom_renderer *renderer = stack->current->data;
|
|
|
|
struct document *document = renderer->document;
|
2007-08-28 20:26:53 -04:00
|
|
|
struct rss_renderer *rss;
|
2016-04-20 15:23:55 -04:00
|
|
|
int type;
|
2007-08-28 13:38:13 -04:00
|
|
|
|
2007-08-28 15:31:25 -04:00
|
|
|
struct css_stylesheet *css = &default_stylesheet;
|
|
|
|
{
|
2007-08-28 13:38:13 -04:00
|
|
|
static int i_want_struct_module_for_dom;
|
|
|
|
|
|
|
|
if (!i_want_struct_module_for_dom) {
|
2021-01-02 10:20:27 -05:00
|
|
|
static const char default_colors[] =
|
2007-08-28 13:38:13 -04:00
|
|
|
"title { color: lightgreen } "
|
2007-08-28 15:34:32 -04:00
|
|
|
"author { color: aqua }"
|
|
|
|
"author-date-sep{ color: aqua }"
|
|
|
|
"date { color: aqua }";
|
2021-01-02 10:20:27 -05:00
|
|
|
char *styles = (char *) default_colors;
|
2007-08-28 13:38:13 -04:00
|
|
|
|
|
|
|
i_want_struct_module_for_dom = 1;
|
|
|
|
/* When someone will get here earlier than at 4am,
|
|
|
|
* this will be done in some init function, perhaps
|
|
|
|
* not overriding the user's default stylesheet. */
|
|
|
|
css_parse_stylesheet(css, NULL, styles, styles + sizeof(default_colors));
|
|
|
|
}
|
2007-08-28 15:31:25 -04:00
|
|
|
}
|
|
|
|
|
2007-08-28 20:26:53 -04:00
|
|
|
rss = renderer->data = mem_calloc(1, sizeof(*rss));
|
2007-08-30 20:08:19 -04:00
|
|
|
if (!rss)
|
2007-08-29 06:05:10 -04:00
|
|
|
return DOM_CODE_ALLOC_ERR;
|
2007-08-28 15:31:25 -04:00
|
|
|
|
|
|
|
/* Initialize styles. */
|
|
|
|
|
|
|
|
for (type = 0; type < RSS_STYLES; type++) {
|
2016-04-20 12:05:00 -04:00
|
|
|
struct screen_char *template_ = &rss->styles[type];
|
2021-01-02 10:20:27 -05:00
|
|
|
static const char *names[RSS_STYLES] =
|
2007-08-28 15:34:32 -04:00
|
|
|
{ "title", "author", "author-date-sep", "date" };
|
2007-08-28 15:31:25 -04:00
|
|
|
struct css_selector *selector = NULL;
|
2007-08-28 13:38:13 -04:00
|
|
|
|
|
|
|
selector = find_css_selector(&css->selectors,
|
|
|
|
CST_ELEMENT, CSR_ROOT,
|
|
|
|
names[type], strlen(names[type]));
|
2016-04-20 12:05:00 -04:00
|
|
|
init_template_by_style(template_, &document->options,
|
2007-08-28 13:38:13 -04:00
|
|
|
selector ? &selector->properties : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return DOM_CODE_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-28 11:37:06 -04:00
|
|
|
static enum dom_code
|
2007-08-28 13:57:45 -04:00
|
|
|
dom_rss_pop_document(struct dom_stack *stack, struct dom_node *root, void *xxx)
|
2007-08-28 11:31:43 -04:00
|
|
|
{
|
|
|
|
struct dom_renderer *renderer = stack->current->data;
|
2007-08-28 20:26:53 -04:00
|
|
|
struct rss_renderer *rss = renderer->data;
|
2007-08-28 11:31:43 -04:00
|
|
|
|
2007-08-28 20:26:53 -04:00
|
|
|
mem_free(rss);
|
2007-08-28 13:57:45 -04:00
|
|
|
|
2009-02-14 22:08:06 -05:00
|
|
|
/* ELinks does not provide any sort of DOM access to the RSS
|
|
|
|
* document after it has been rendered. Tell the caller to
|
|
|
|
* free the document node and all of its children. Otherwise,
|
|
|
|
* they would leak. */
|
2009-02-12 04:05:35 -05:00
|
|
|
return DOM_CODE_FREE_NODE;
|
2007-08-28 11:31:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct dom_stack_context_info dom_rss_renderer_context_info = {
|
|
|
|
/* Object size: */ 0,
|
|
|
|
/* Push: */
|
|
|
|
{
|
|
|
|
/* */ NULL,
|
|
|
|
/* DOM_NODE_ELEMENT */ dom_rss_push_element,
|
|
|
|
/* DOM_NODE_ATTRIBUTE */ NULL,
|
|
|
|
/* DOM_NODE_TEXT */ NULL,
|
|
|
|
/* DOM_NODE_CDATA_SECTION */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY_REFERENCE */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY */ NULL,
|
|
|
|
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
|
|
|
|
/* DOM_NODE_COMMENT */ NULL,
|
2007-08-28 13:38:13 -04:00
|
|
|
/* DOM_NODE_DOCUMENT */ dom_rss_push_document,
|
2007-08-28 11:31:43 -04:00
|
|
|
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
|
|
|
|
/* DOM_NODE_NOTATION */ NULL,
|
|
|
|
},
|
|
|
|
/* Pop: */
|
|
|
|
{
|
|
|
|
/* */ NULL,
|
|
|
|
/* DOM_NODE_ELEMENT */ dom_rss_pop_element,
|
|
|
|
/* DOM_NODE_ATTRIBUTE */ NULL,
|
|
|
|
/* DOM_NODE_TEXT */ NULL,
|
|
|
|
/* DOM_NODE_CDATA_SECTION */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY_REFERENCE */ NULL,
|
|
|
|
/* DOM_NODE_ENTITY */ NULL,
|
|
|
|
/* DOM_NODE_PROC_INSTRUCTION */ NULL,
|
|
|
|
/* DOM_NODE_COMMENT */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT */ dom_rss_pop_document,
|
|
|
|
/* DOM_NODE_DOCUMENT_TYPE */ NULL,
|
|
|
|
/* DOM_NODE_DOCUMENT_FRAGMENT */ NULL,
|
|
|
|
/* DOM_NODE_NOTATION */ NULL,
|
|
|
|
}
|
|
|
|
};
|