Retry only for https protocol
[elinks.git] / src / scripting / python / document.c
blob76decbefb582edbc892617cf2794a12cf09e1edb
1 /* Information about current document and current link for Python. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <Python.h>
9 #include "elinks.h"
11 #include "cache/cache.h"
12 #include "document/document.h"
13 #include "document/view.h"
14 #include "scripting/python/core.h"
15 #include "session/session.h"
17 /* Python interface to get the current document's body. */
19 static char python_current_document_doc[] =
20 PYTHON_DOCSTRING("current_document() -> string or None\n\
21 \n\
22 If a document is being viewed, return its body; otherwise return None.\n");
24 static PyObject *
25 python_current_document(PyObject *self, PyObject *args)
27 if (python_ses && python_ses->doc_view
28 && python_ses->doc_view->document) {
29 struct cache_entry *cached = python_ses->doc_view->document->cached;
30 struct fragment *f = cached ? cached->frag.next : NULL;
32 if (f) return PyString_FromStringAndSize(f->data, f->length);
35 Py_INCREF(Py_None);
36 return Py_None;
39 /* Python interface to get the current document's header. */
41 static char python_current_header_doc[] =
42 PYTHON_DOCSTRING("current_header() -> string or None\n\
43 \n\
44 If a document is being viewed and it has a header, return the header;\n\
45 otherwise return None.\n");
47 static PyObject *
48 python_current_header(PyObject *self, PyObject *args)
50 if (python_ses && python_ses->doc_view
51 && python_ses->doc_view->document) {
52 struct cache_entry *cached = python_ses->doc_view->document->cached;
54 if (cached && cached->head)
55 return PyString_FromString(cached->head);
58 Py_INCREF(Py_None);
59 return Py_None;
62 /* Python interface to get the currently-selected link's URL. */
64 static char python_current_link_url_doc[] =
65 PYTHON_DOCSTRING("current_link_url() -> string or None\n\
66 \n\
67 If a link is selected, return its URL; otherwise return None.\n");
69 static PyObject *
70 python_current_link_url(PyObject *self, PyObject *args)
72 unsigned char url[MAX_STR_LEN];
74 if (python_ses && get_current_link_url(python_ses, url, MAX_STR_LEN))
75 return PyString_FromString(url);
77 Py_INCREF(Py_None);
78 return Py_None;
81 /* Python interface to get the current document's title. */
83 static char python_current_title_doc[] =
84 PYTHON_DOCSTRING("current_title() -> string or None\n\
85 \n\
86 If a document is being viewed, return its title; otherwise return None.\n");
88 static PyObject *
89 python_current_title(PyObject *self, PyObject *args)
91 unsigned char title[MAX_STR_LEN];
93 if (python_ses && get_current_title(python_ses, title, MAX_STR_LEN))
94 return PyString_FromString(title);
96 Py_INCREF(Py_None);
97 return Py_None;
100 /* Python interface to get the current document's URL. */
102 static char python_current_url_doc[] =
103 PYTHON_DOCSTRING("current_url() -> string or None\n\
105 If a document is being viewed, return its URL; otherwise return None.\n");
107 static PyObject *
108 python_current_url(PyObject *self, PyObject *args)
110 unsigned char url[MAX_STR_LEN];
112 if (python_ses && get_current_url(python_ses, url, MAX_STR_LEN))
113 return PyString_FromString(url);
115 Py_INCREF(Py_None);
116 return Py_None;
119 static PyMethodDef document_methods[] = {
120 {"current_document", python_current_document,
121 METH_NOARGS,
122 python_current_document_doc},
124 {"current_header", python_current_header,
125 METH_NOARGS,
126 python_current_header_doc},
128 {"current_link_url", python_current_link_url,
129 METH_NOARGS,
130 python_current_link_url_doc},
132 {"current_title", python_current_title,
133 METH_NOARGS,
134 python_current_title_doc},
136 {"current_url", python_current_url,
137 METH_NOARGS,
138 python_current_url_doc},
140 {NULL, NULL, 0, NULL}
144 python_init_document_interface(PyObject *dict, PyObject *name)
146 return add_python_methods(dict, name, document_methods);