comwes / mkpdfs-mkdocs-plugin

Converts your mkdocs documentation in PDF, to be shared with users
https://comwes.github.io/mkpdfs-mkdocs-plugin
GNU General Public License v3.0
103 stars 36 forks source link

transform.py launch list index out of range exception #11

Closed thegrima closed 4 years ago

thegrima commented 4 years ago

Using mkpdfs-mkdocs 1.0.1 Using mkdocs 1.0.4 Using python 3.8.1

When trying to use mkpdfs-mkdocs on our project, i've encoutered an exception :

ERROR   -  Error building page '[...]Release 250.md': list index out of range
Traceback (most recent call last):
  File "c:\python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Python38\Scripts\mkdocs.exe\__main__.py", line 9, in <module>
  File "c:\python38\lib\site-packages\click\core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "c:\python38\lib\site-packages\click\core.py", line 717, in main
    rv = self.invoke(ctx)
  File "c:\python38\lib\site-packages\click\core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\python38\lib\site-packages\click\core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\python38\lib\site-packages\click\core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "c:\python38\lib\site-packages\mkdocs\__main__.py", line 128, in serve_command
    serve.serve(
  File "c:\python38\lib\site-packages\mkdocs\commands\serve.py", line 142, in serve
    config = builder()
  File "c:\python38\lib\site-packages\mkdocs\commands\serve.py", line 137, in builder
    build(config, live_server=live_server, dirty=dirty)
  File "c:\python38\lib\site-packages\mkdocs\commands\build.py", line 295, in build
    _build_page(file.page, config, files, nav, env, dirty)
  File "c:\python38\lib\site-packages\mkdocs\commands\build.py", line 219, in _build_page
    output = config['plugins'].run_event(
  File "c:\python38\lib\site-packages\mkdocs\plugins.py", line 94, in run_event
    result = method(item, **kwargs)
  File "c:\python38\lib\site-packages\mkpdfs_mkdocs\mkpdfs.py", line 58, in on_post_page
    pdf_url = self.generator.add_article(output_content, page, base_url)
  File "c:\python38\lib\site-packages\mkpdfs_mkdocs\generator.py", line 113, in add_article
    article = prep_combined(article, base_url, page.file.url)
  File "c:\python38\lib\site-packages\mkpdfs_mkdocs\preprocessor\prep.py", line 17, in get_combined
    a['href'] = transform_href(a['href'], rel_url)
  File "c:\python38\lib\site-packages\mkpdfs_mkdocs\preprocessor\links\transform.py", line 17, in transform_href
    id = str.split(ext, '#')[1]
IndexError: list index out of range

After a little code hacking i've managed to identify the issue seems to happens on this type of links in our markdown :

# [More…](./#!/62GettingValues#meta-and-internalmeta)

I've managed to bypass the problem by patching preprocessor\links\transform.py like this :

    elif num_hashtags is 1:
        section, ext = tuple(os.path.splitext(tail))
        id = str.split(ext, '#')[1]

to :

    elif num_hashtags == 1:
        section, ext = tuple(os.path.splitext(tail))
        id = str.split(ext if len(ext) > 0 else section, '#')[1]

Hope this helps

comwes commented 4 years ago

Hi @thegrima;

Thanks for your report and the provided fix. I'll manage to insert this fix in the upcoming release as soon as I have few time to work on the project.

KR, Gerry N.

xbencat commented 4 years ago

I had the same issue, @thegrima thank you for solving. And ofc @comwes thank you for building this, it saved me so much work. I had school project documented via mkdocs, but they wanted the documentation in pdf in the final submission. 40 pages, you are a savior