teedoc plugin development =========== The plug-in development of `teedoc` is very simple. At present, you only need to modify the template according to the template to realize the new function. If you know about `python` `html` `css` `js`, it will be easier. The operating principle of the plugin is that `teedoc` provides [plugin API](https://github.com/teedoc/teedoc/blob/main/teedoc/plugin.py), write a `python` package, inherit this class, and rewrite The required method (`API`) is fine, the specific meaning of each method is detailed in the [plugin API](https://github.com/teedoc/teedoc/blob/main/teedoc/plugin.py) file Annotation > If you find that the API is unreasonable, you can [submit issue](https://github.com/teedoc/teedoc/issues) to initiate a discussion and improve together~ ## Reference template You can refer to [default plugin](https://github.com/teedoc/teedoc/tree/main/plugins) ## Plug-in directory structure Here is [teedoc-plugin-baidu-tongji](https://github.com/teedoc/teedoc/tree/main/plugins/teedoc-plugin-baidu-tongji) as an example * Create a directory, the directory name is the same as the plug-in name, it is recommended to start with `teedoc-plugin-` for easy search * Then create a `setup.py` file, this is the general configuration file of the `python` package, the most important thing in the file is to execute the `setuptools.setup()` function, the parameters mainly include the following several, more can be `python` official document search ```python setup( name='teedoc-plugin-baidu-tongji', version="1.0.2", author='Neucrack', author_email='xxx@xxx.com', description='baidu tongji plugin for teedoc', long_description=long_description, # can read from README.md long_description_content_type="text/markdown", url='https://github.com/Neutree/teedoc', license='MIT', # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ # Indicate who your project is intended for 'Intended Audience :: Developers', # Pick your license as you wish (should match "license" above) 'License :: OSI Approved :: MIT License', # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. 'Programming Language :: Python :: 3' ], keywords='teedoc baidu tongji', # List run-time dependencies here. These will be installed by pip when # your project is installed. For an analysis of "install_requires" vs pip's # requirements files see: # https://packaging.python.org/en/latest/requirements.html install_requires=install_requires, # List additional groups of dependencies here (e.g. development # dependencies). You can install these using the following syntax, # for example: # $ pip install -e .[dev,test] extras_require={ #'dev': ['check-manifest'], #'test': ['coverage'], }, # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). packages=packages, # If there are data files included in your packages that need to be # installed, specify them here. If using Python 2.6 or less, then these # have to be included in MANIFEST.in as well. package_data={ "teedoc_plugin_baidu_tongji": ['assets/*'], }, # Although'package_data' is the preferred approach, in some case you may # need to place data files outside of your packages. See: # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa # In this case,'data_file' will be installed into'/my_data' data_files=[ ], # To provide executable scripts, use entry points in preference to the # "scripts" keyword. Entry points provide cross-platform support and allow # pip to create the appropriate form of executable for the target platform. entry_points={ #'console_scripts': [ # #'gui_scripts': [ #'teedoc=teedoc-plugin-markdown-parser.main:main', # ], }, ) ``` * Create package You need to create another subfolder to store the source code. The file name is just to replace the minus sign `-` in the project name with an underscore `_`, because the package name in the `python` code requires that the minus sign cannot be used, for example, here is `teedoc_plugin_baidu_tongji` Then create a new file `__init__.py` * Edit `__init__.py` To realize the function of Baidu statistics, it is actually necessary to add a script specified by Baidu statistics to the `` tags of all pages, that is, the `` tag. At the same time, from the document Get the statistical number (`code`) in the configuration, the code is as follows: ```python import os, sys from teedoc import Plugin_Base from teedoc import Fake_Logger class Plugin(Plugin_Base): name = "teedoc-plugin-baidu-tongji" desc = "baidu tongji support for teedoc" defautl_config = { } def on_init(self, config, doc_src_path, site_config, logger = None): ''' @config a dict object @logger teedoc.logger.Logger object ''' self.logger = Fake_Logger() if not logger else logger self.doc_src_path = doc_src_path self.site_config = site_config self.config = Plugin.defautl_config self.config.update(config) self.logger.i("-- plugin <{}> init".format(self.name)) self.logger.i("-- plugin <{}> config: {}".format(self.name, self.config)) # set site_root_url env value if not "code" in config: self.logger.e('can not find config["code"] in plugin {}'.format(self.name)) return baidu_tongji_code =''''''.format(config["code"]) self.html_header_items = [baidu_tongji_code] def on_add_html_header_items(self): return self.html_header_items ``` You can see that `class Plugin(Plugin_Base):` inherits the `Plugin_Base` class, and then rewrites the `on_init` and `on_add_html_header_items` methods. When the document is built by `teedoc`, these two functions will be called at the right time. We obtained the Baidu statistics of the `code` from the `config` in the `on_init` initialization function, then generated the content of the `