adityam / filter

ConTeXt module to process contents of a start-stop environment through an external program
45 stars 10 forks source link

t-vim `.vimout` file name clashing #59

Closed rijenkii closed 2 years ago

rijenkii commented 2 years ago

Two following use cases do not work correctly:

\typeTEXTfile{folder1/file.txt}
\typeTEXTfile{folder2/file.txt}
\typeTEXTfile[start=1, stop=10]{file.txt}
\typeTEXTfile[start=11, stop=20]{file.txt}

In both cases only a single .vimout file is created.

Similar issue: #57.

adityam commented 2 years ago

I realized that this will clash when writing the patch for #57, but I did not fix it because I don't know what is a good automated solution. Currently, the name of the file is \jobname-\basename-\ext.vimout where \basename is the name of the file and \ext is the extension of the file.

If two files with the same name are in different directories, then the name of the .vimout file will clash. There are two ways to fix this:

I don't like either solution, so I offer a work-around for now: Ask the user to specify the name of the vimout file: for example:

\typeTEXTfile[output=\jobname-folder1-file.vimout]{folder1/file.txt}
\typeTEXTfile[output=\jobname-folder2-file.vimout]{folder2/file.txt}

I know that this is not ideal, but hopefully this will suffice.

rijenkii commented 2 years ago

This will actually suffice, thank you!

I do like the idea of having hash instead of filename though, maybe add it behind some flag? Though, I don't think that ConTeXt has any built-in hash functions..

adityam commented 2 years ago

There is md5 ... (SHA might also be there but it is an overkill for something like this):

\startluacode
  local filename = "folder1/file.txt"
  local hash = md5.HEX(filename)
  print(">>>>", filename, hash)
\stopluacode

So, we can use something like this:

\usemodule[vim]

\definevimtyping[TEX]
                [
                  syntax=context,
                  output={\jobname-\cldcontext{md5.HEX("\externalfilterinputfile")}.vimout},
                ]

\traceexternalfilters

\starttext
\typeTEXfile[start=2,stop=4]{a/test.tex}
\typeTEXfile[start=5,stop=7]{b/test.tex}

Given the two recent bugs (#57 and this one) on handling of external files, I can even make this default for processing external files.

rijenkii commented 2 years ago

If there is fear about the end user not understanding which output files are for which input file and long filenames, you also make filenames look like file-txt-ah3nv8an.vimout, that is <filename>-<ext>-<8 chars of md5>.vimout. But, that's for you to decide.

adityam commented 2 years ago

Can you test t-filter.mkxl from the hash branch. It passes all my tests.

rijenkii commented 2 years ago

This indeed fixes the problem with folders, but in the OP I have also mentioned a start/end clash: same path, different parts of the file.

adityam commented 2 years ago

There is already a cacheoption key. I can do a hash of #3\externalfilterparameters{cacheoption}. Then, to get different start-stop pairs to work, we will need to specify

\defineexternalfilter[...][cacheoption={\externalfilterparameter{start}\externalfilterparameter{stop}}

These could be the default options in t-vim. Let me test this idea

adityam commented 2 years ago

See the latest commit on the hash branch. That resolves the second bug as well

rijenkii commented 2 years ago

Yeah, that does seem to work correctly with my use cases.