Skip to content

Commit

Permalink
Checking for links to figures
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Wilson committed Jul 7, 2016
1 parent 9a5ab18 commit e577ea8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bin/extract_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
from util import Reporter, read_markdown


# Things an image file's name can end with.
PATH_SUFFICES = {
'.gif',
'.jpg',
'.png',
'.svg'
}


def main():
"""Main driver."""

Expand Down Expand Up @@ -54,6 +63,7 @@ def get_images(parser, filename):
content = read_markdown(parser, filename)
result = []
find_image_nodes(content['doc'], result)
find_image_links(content['doc'], result)
return result


Expand All @@ -68,6 +78,18 @@ def find_image_nodes(doc, result):
find_image_nodes(child, result)


def find_image_links(doc, result):
"""Find all links to files in the 'fig' directory."""

if (doc['type'] == 'a') and ('attr' in doc) and ('href' in doc['attr']):
path = doc['attr']['href']
if os.path.splitext(path)[1].lower() in PATH_SUFFICES:
result.append({'alt':'', 'src': doc['attr']['href']})
else:
for child in doc.get('children', []):
find_image_links(child, result)


def save(stream, images):
"""Save results as Markdown."""

Expand Down

0 comments on commit e577ea8

Please sign in to comment.