cmhughes / latexindent.pl

Perl script to add indentation (leading horizontal space) to LaTeX files. It can modify line breaks before, during and after code blocks; it can perform text wrapping and paragraph line break removal. It can also perform string-based and regex-based substitutions/replacements. The script is customisable through its YAML interface.
GNU General Public License v3.0
881 stars 83 forks source link

Support using `latexindent` as a `pre-commit` hook #316

Closed hamdanal closed 2 years ago

hamdanal commented 2 years ago

This is an amazing tool, thank you for your work. I could not follow the issue template as it is designed for issues that concern written code or bug reports while this is a feature request related to the tool usage. Here goes:

pre-commit is a famous framework for managing and maintaining pre-commit hooks such as linters and code formatters. It supports most of the famous programming languages including perl. It would be very helpful to be able to run latexindent as a pre-commit hook. The support can be added by creating a .pre-commit-hooks.yaml file in the root of this repository which will tell pre-commit how to install and run latexindent.

Here an example hook file https://github.com/psf/black/blob/main/.pre-commit-hooks.yaml for the famous python formatter black for reference.

cmhughes commented 2 years ago

Thanks for this, and I'm glad the tool works for you.

I'm sorry, I'm unclear what you would like or need me to do. Does the yaml file you describe need to live in this repository? Or is it something that users create in their own repositories?

If something needs to be created, and you know what needs to be done, please do feel free to submit a pull request to the develop branch :)

hamdanal commented 2 years ago

Does the yaml file you describe need to live in this repository? Or is it something that users create in their own repositories?

Yes. The .pre-commit-hooks.yaml needs to be created in the root of this repository. This gives necessary information to the pre-commit framework on how to install and run latexindent for the users.

If something needs to be created, and you know what needs to be done, please do feel free to submit a pull request to the develop branch :)

Unfortunately I don't know enough about perl (I don't know anything to be frank) to submit a PR. If you are interested in adding this functionality, I can gladly help creating the .pre-commit-hooks.yaml file but the repository first needs a Makefile.PL or Build.PL installation file according to the pre-commit docs.

cmhughes commented 2 years ago

OK, thanks for the follow up.

I'm happy to explore this and to try to get it moving. I'm new to the pre commit stuff but from my early reading I can see it being helpful. I found the following post interesting

https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/

I'd certainly welcome your assistance with any part that you can, including the documentation.

I guess the first step is for me to create Makefile.pl.

hamdanal commented 2 years ago

No problem and thank you for being interactive.

I guess the first step is for me to create Makefile.pl.

Indeed. Please ping me here when this is done so that I can proceed with the creation of the hook file and adding an entry in the docs. We need also to discuss the default command line arguments to use when running the tool with pre-commit. For instance using the --silent and --overwrite flags seems to make sense in this context.

cmhughes commented 2 years ago

This looks helpful as a demonstration that has a Makefile.pl

https://github.com/mlawren/githook-perltidy

cmhughes commented 2 years ago

OK, will do. Development is slow at the moment, but it's on the list :) all help welcome!

cmhughes commented 2 years ago

Possibly helpful (search for perl on page) :

https://pre-commit.com/hooks.html

hamdanal commented 2 years ago

Thanks. I found this project https://github.com/perltidy/perltidy interesting. It has a Makefile.PL file and defines a pre-commit hook. Here is a preliminary version of the hook that we should define:

- id: latexindent.pl
  name: latexindent.pl
  description: Perl script to add indentation (leading horizontal space) to LaTeX files.
  minimum_pre_commit_version: 2.1.0
  entry: latexindent.pl --silent --overwrite
  language: perl
  types_or: [tex, sty, cls, bib]

Notes:

  1. language: perl tells pre-commit that this is a perl tool and to use the Makefile.PL file to install the tool;
  2. types_or: [tex, sty, cls, bib] tells pre-commit to run the tool for any of these file types (from https://latexindentpl.readthedocs.io/en/latest/sec-introduction.html#lst-fileextensionpreference-demo);
  3. entry: latexindent.pl --silent --overwrite runs the tool with these defaults. Users can add extra arguments in their projects
cmhughes commented 2 years ago

That looks good, nicely done. Perhaps the Makefile.pl can be adapted for this too.

First step would be to implement Makefile.pl on a dummy repository to demonstrate concept.

tdegeus commented 2 years ago

I would love to use this !!

It seems that pre-commit can use conda : https://pre-commit.com/#new-hooks In that case things might be simple : one can just use the feedstock on conda forge (does require https://github.com/cmhughes/latexindent.pl/issues/303 to be merged for macOS ARM users)

cmhughes commented 2 years ago

This is implemented as of https://github.com/cmhughes/latexindent.pl/pull/322

A huge thank you to @tdegeus for their work on this, and for all of their time in answering my questions! I've got a few minor documentation tweaks to do, and then I'll get the next release published, which will include this feature.

I'll leave this open until that happens. Thank you again!

cmhughes commented 2 years ago

This is resolved as of https://github.com/cmhughes/latexindent.pl/releases/tag/V3.15

Thank you very much to @tdegeus for this contribution, fantastic stuff! :) I've uploaded to ctan as well.