zim-linklist/linklist.py

75 lines
1.8 KiB
Python
Raw Normal View History

2015-01-28 11:21:48 +00:00
from zim.plugins import PluginClass
from zim.command import Command
from zim.notebook import resolve_notebook, build_notebook
2016-05-24 21:17:15 +00:00
usagehelp = '''\
usage: zim --plugin linklist [OPTIONS] <notebook>
2015-01-28 11:21:48 +00:00
2016-05-24 21:10:03 +00:00
--help, -h Print this help
--existing-only List only pages that exist
--missing-only List only pages that don't exist
2015-01-28 11:21:48 +00:00
'''
2016-05-24 21:10:03 +00:00
2016-05-24 21:17:15 +00:00
2015-01-28 11:21:48 +00:00
class LinkListPlugin(PluginClass):
plugin_info = {
'name': 'Link List',
2016-05-24 21:10:03 +00:00
'description': 'List all links throughout the pages',
2015-01-28 11:21:48 +00:00
'author': 'Gergely Polonkai',
}
2016-05-24 21:17:15 +00:00
2015-01-28 11:21:48 +00:00
class LinkListCommand(Command):
options = (
('help', 'h', 'Print this help text and exit'),
('existing-only', 'e', 'List only existing pages'),
('missing-only', 'm', 'List only missing pages'),
)
def parse_options(self, *args):
Command.parse_options(self, *args)
def _all_links(self):
for page in self.nb.index.walk():
yield page
def run(self):
if self.opts.get('help'):
print usagehelp
return
nbi = None
2016-05-24 21:17:15 +00:00
if len(self.args) > 0:
nbi = resolve_notebook(self.args[0])
2015-01-28 11:21:48 +00:00
2016-05-24 21:17:15 +00:00
if nbi is None:
2015-01-28 11:21:48 +00:00
print("Notebook must be specified!")
return
self.nb, ns = build_notebook(nbi)
listing = None
if self.opts.get('missing-only'):
listing = 'M'
if self.opts.get('existing-only'):
if listing == 'M':
print """\
--missing-only and --existing-only are mutually exclusive!"""
return
listing = 'E'
for page in self._all_links():
exists = page.exists()
2016-05-24 21:17:15 +00:00
if listing is None or \
(listing == 'M' and exists == 0) or \
(listing == 'E' and exists == 1):
2015-01-28 11:21:48 +00:00
print "%c %s" % ('E' if exists == 1 else 'M', page.name)