1 /* Information about current document and current link for Python. */
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\
22 If a document is being viewed, return its body; otherwise return None.\n");
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
);
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\
44 If a document is being viewed and it has a header, return the header;\n\
45 otherwise return None.\n");
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
);
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\
67 If a link is selected, return its URL; otherwise return None.\n");
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
);
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\
86 If a document is being viewed, return its title; otherwise return None.\n");
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
);
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");
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
);
119 static PyMethodDef document_methods
[] = {
120 {"current_document", python_current_document
,
122 python_current_document_doc
},
124 {"current_header", python_current_header
,
126 python_current_header_doc
},
128 {"current_link_url", python_current_link_url
,
130 python_current_link_url_doc
},
132 {"current_title", python_current_title
,
134 python_current_title_doc
},
136 {"current_url", python_current_url
,
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
);