1#!/usr/bin/env python
2
3r"""
4Implementation of the hyperref package
5
6TO DO:
7- \autoref doesn't look for \*autorefname, it only looks for \*name
8- Layouts
9- Forms optional parameters
10
11"""
12
13from plasTeX import Command, Environment
14from plasTeX.Base.LaTeX.Crossref import ref, pageref
15import urllib.parse
16
17def addBaseURL(self, urlarg):
18    try:
19        baseurl = self.ownerDocument.userdata['packages']['hyperref']['baseurl']
20        return urllib.parse.urljoin(baseurl, self.attributes[urlarg])
21    except KeyError: pass
22    return self.attributes[urlarg]
23
24# Basic macros
25
26ref.args = '* %s' % ref.args
27pageref.args = '* %s' % pageref.args
28
29class href(Command):
30    args = 'url:url self'
31    def invoke(self, tex):
32        res = Command.invoke(self, tex)
33        self.attributes['url'] = addBaseURL(self, 'url')
34        return res
35
36class url(Command):
37    args = 'url:url'
38    def invoke(self, tex):
39        res = Command.invoke(self, tex)
40        self.attributes['url'] = addBaseURL(self, 'url')
41        return res
42
43class nolinkurl(Command):
44    args = 'url:url'
45    def invoke(self, tex):
46        res = Command.invoke(self, tex)
47        self.attributes['url'] = addBaseURL(self, 'url')
48        return res
49
50class hyperbaseurl(Command):
51    args = 'base:url'
52    def invoke(self, tex):
53        res = Command.invoke(self, tex)
54        data = self.ownerDocument.userdata
55        if 'packages' not in list(data.keys()):
56            data['packages'] = {}
57        if 'hyperref' not in list(data['packages'].keys()):
58            data['packages']['hyperref'] = {}
59        self.ownerDocument.userdata['packages']['hyperref']['baseurl'] = self.attributes['base']
60        return res
61
62class hyperimage(Command):
63    args = 'url:url self'
64    def invoke(self, tex):
65        res = Command.invoke(self, tex)
66        self.attributes['url'] = addBaseURL(self, 'url')
67        return res
68
69class hyperdef(Command):
70    args = 'category name self'
71
72class hyperref(Command):
73    '''
74    hyperref has a dual personality depending on whether or not
75    the first argument is square-bracketed. We only support the
76    square bracket version for now.
77    '''
78    #args = 'url:url category name self'
79    args = '[label:idref] self'
80    #def invoke(self, tex):
81    #    res = Command.invoke(self, tex)
82    #    self.attributes['url'] = addBaseURL(self, 'url')
83    #    return res
84
85class hyperlink(Command):
86    args = 'label:idref self'
87
88class hypertarget(Command):
89    counter = 'hypertarget'  # so we can link to it
90    args = 'label:id self'
91
92class hypertargetname(Command):
93    """ Dummy class for hypertarget macro """
94    str = ''
95
96class thehypertarget(Command):
97    """ Dummy class for hypertarget macro """
98    str = ''
99
100class phantomsection(Command):
101    pass
102
103class autoref(Command):
104    args = 'label:idref'
105
106class pdfstringdef(Command):
107    args = 'macroname:string tex:string'
108
109class textorpdfstring(Command):
110    args = 'tex:string pdf:string'
111
112class pdfstringdefDisableCommands(Command):
113    args = 'tex:string'
114
115class hypercalcbp(Command):
116    args = 'size:string'
117
118
119# Forms
120
121class Form(Environment):
122    args = '[ parameters:dict ]'
123
124class TextField(Command):
125    args = '[ parameters:dict ] label'
126
127class CheckBox(Command):
128    args = '[ parameters:dict ] label'
129
130class ChoiceMenu(Command):
131    args = '[ parameters:dict ] label choices:list'
132
133class PushButton(Command):
134    args = '[ parameters:dict ] label'
135
136class Submit(Command):
137    args = '[ parameters:dict ] label'
138
139class Reset(Command):
140    args = '[ parameters:dict ] label'
141
142
143class LayoutTextField(Command):
144    args = 'label field'
145
146class LayoutChoiceField(Command):
147    args = 'label field'
148
149class LayoutCheckField(Command):
150    args = 'label field'
151
152
153class MakeRadioField(Command):
154    args = 'width height'
155
156class MakeCheckField(Command):
157    args = 'width height'
158
159class MakeTextField(Command):
160    args = 'width height'
161
162class MakeChoiceField(Command):
163    args = 'width height'
164
165class MakeButtonField(Command):
166    args = 'self'
167
168
169class DefaultHeightofSubmit(Command):
170    args = 'size:dimen'
171
172class DefaultWidthofSubmit(Command):
173    args = 'size:dimen'
174
175class DefaultHeightofReset(Command):
176    args = 'size:dimen'
177
178class DefaultWidthofReset(Command):
179    args = 'size:dimen'
180
181class DefaultHeightofCheckBox(Command):
182    args = 'size:dimen'
183
184class DefaultWidthofCheckBox(Command):
185    args = 'size:dimen'
186
187class DefaultHeightofChoiceMenu(Command):
188    args = 'size:dimen'
189
190class DefaultWidthofChoiceMenu(Command):
191    args = 'size:dimen'
192
193class DefaultHeightofText(Command):
194    args = 'size:dimen'
195
196class DefaultWidthofText(Command):
197    args = 'size:dimen'
198
199class pdfbookmark(Command):
200    args = '[level:number] text name'
201
202class currentpdfbookmark(Command):
203    args = 'text name'
204
205class subpdfbookmark(Command):
206    args = 'text name'
207
208class belowpdfbookmark(Command):
209    args = 'text name'
210