# -*- coding: utf-8 -*-"""This module is a sphinx-doc add-on to implement the following markup:: .. icontable:: <dirname> :n_columns: <n_columns>For example:: .. icontable:: demo-images :n_columns: 3It looks for image file in <currend_dir>/<dirname>, and put them into a rstlist table, <n_columns> item each row."""from__future__importunicode_literalsimportsphinx.utilfromdocutilsimportnodesfromdocutils.parsers.rstimportDirectivefromdocutils.statemachineimportStringListfrompathlib_mateimportPathfromrstobj.directivesimportListTable,Imagetry:# in python2fromitertoolsimportizip_longestaszip_longestexcept:# in python3fromitertoolsimportzip_longest
[docs]defderive_rst(current_dir,image_dir,n_columns):""" scan ``image_dir`` find all image path, find the relative path to ``current_dir``, and put them in a table, ``n_columns`` width. return the list table rst directive text. """current_dir,image_dir=Path(current_dir),Path(image_dir)image_list=[Image(uri=str(p.relative_to(current_dir)),height=64,width=64)forpinimage_dir.select_image()]data=list(grouper(image_list,n_columns))ltable=ListTable(data=data,header=False,index=False)returnltable.render()