Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exporting to latex template #80

Closed
wants to merge 26 commits into from
Closed

Add exporting to latex template #80

wants to merge 26 commits into from

Conversation

universvm
Copy link
Contributor

Closes #66 .

@mokeyish
Copy link
Owner

mokeyish commented Jun 5, 2023

Thanks for the contribution👍

src/ui/ExportDialog.tsx Outdated Show resolved Hide resolved
src/ui/ExportDialog.tsx Outdated Show resolved Hide resolved
@universvm
Copy link
Contributor Author

Hi @mokeyish this is still a draft. Need to do a bit more work

@universvm
Copy link
Contributor Author

universvm commented Jun 6, 2023

Hi @mokeyish ,

I am currently waiting on pandoc to see how to use relative paths as it seems that resource-path is not being used for .sty files: jgm/pandoc#8894

Have tried a lot but don't seem to be able to export. Essentially if you use iCloud, your files are placed in a folder with a space in its name. Latex does not like spaces. Even when trying to sanitise the path, both in latex and javascript, I still had issues.

The current workaround is to pass the path to the template in the metadata and make sure there are no spaces:

---
title: Your Title
subtitle: ""
authors:
  - name: Leonardo V. Castoirna
    affiliation: School of Informatics
    institution: University of Edinburgh
    email: justanemail@domain.ext
    address: Edinburgh
  - name: Coauthor
    affiliation: Affiliation
    institution: Institution
    email: coauthor@example.com
    address: Address
  - name: Another Author
    affiliation: Another Affiliation
    institution: Another Institution
    email: another.author@example.com
****stylefile: /Users/leo/Desktop/style/neurips***
---

So far I've built two templates. Here are some sample documents

paper.pdf
dissertation.pdf

Generated from this
paper.md

Regarding your points. I updated the UI to this:
Screenshot 2023-06-06 at 16 42 14

Initially I thought it would be best to have the export templates in export_command_templates as you mentioned. But I realised after showing it to people that it was not intuitive. People seem to see "Type" as the file format and it is not intuitive to scroll through all the extensions to then find the correct pdf.

So I ended up creating a conditional element in the UI. If the format can take the template, then you see the above image. Else:

Screenshot 2023-06-06 at 16 45 00

Let me know your thoughts. Also I don't think I will be able to work on this much more. Once I find a fix for the template problem I will push it though not sure when.

@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

  • Try to add the path of .sty to --resource-path, it maybe work.
  • You may have misunderstood what I meant. I mean just put the latex template definition into that file.
  • latex template selector should be condintional emelent, which is why I asked you whether all types support latex templates at the beginning.

@universvm
Copy link
Contributor Author

universvm commented Jun 7, 2023

  1. The problem is the space in the file path /Users/leo/Library/Mobile Documents/ I believe. I did try with resource-path. It works if the path has no space but iCloud keeps everything in the "Mobile Documents" folder...
  2. and 3. Sorry, I am not sure what you mean. Do you want to define the template path for every export setting?

@universvm
Copy link
Contributor Author

universvm commented Jun 7, 2023

@mokeyish I found a workaround using TEXINPUTS. TEXINPUTS is a variable for setting additional style paths (which is what we need to do here.

On terminal I modified my .zshrc with this:

export TEXINPUTS=/Users/leo/Library/Mobile\ Documents/iCloud~md~obsidian/Documents/exports/.obsidian/plugins/obsidian-enhancing-export/textemplate//:./:$TEXINPUTS

Ideally we just need to use this variable rather than the path ${textemplateDir}

Is this something we can help with? I really need some help here because I don't know JavaScript / Apps set this environment variables...

@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

@mokeyish I found a workaround using TEXINPUTS. TEXINPUTS is a variable for setting additional style paths (which is what we need to do here.

On terminal I modified my .zshrc with this:

export TEXINPUTS=/Users/leo/Library/Mobile\ Documents/iCloud~md~obsidian/Documents/exports/.obsidian/plugins/obsidian-enhancing-export/textemplate//:./:$TEXINPUTS

Ideally we just need to use this variable rather than the path ${textemplateDir}

Is this something we can help with? I really need some help here because I don't know JavaScript / Apps set this environment variables...

You can set environment variables with following code. (Maybe should make it configurable in the Settings Tab)

await exec('commands', { env: 'TEXINPUTS': 'path/to/textemplate'})

export function exec(cmd: string, options?: ExecOptions): Promise<string> {

@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

  1. The problem is the space in the file path /Users/leo/Library/Mobile Documents/ I believe. I did try with resource-path. It works if the path has no space but iCloud keeps everything in the "Mobile Documents" folder...

    1. and 3. Sorry, I am not sure what you mean. Do you want to define the template path for every export setting?

It seems that my English is really poor, I can't explain my meaning correctly😂.

@universvm
Copy link
Contributor Author

universvm commented Jun 7, 2023

@mokeyish I found a workaround using TEXINPUTS. TEXINPUTS is a variable for setting additional style paths (which is what we need to do here.
On terminal I modified my .zshrc with this:

export TEXINPUTS=/Users/leo/Library/Mobile\ Documents/iCloud~md~obsidian/Documents/exports/.obsidian/plugins/obsidian-enhancing-export/textemplate//:./:$TEXINPUTS

Ideally we just need to use this variable rather than the path ${textemplateDir}
Is this something we can help with? I really need some help here because I don't know JavaScript / Apps set this environment variables...

You can set environment variables with following code. (Maybe should make it configurable in the Settings Tab)

await exec('commands', { env: 'TEXINPUTS': 'path/to/textemplate'})

export function exec(cmd: string, options?: ExecOptions): Promise<string> {

Is there a way to access ${textemplateDir} in from that file? Also how do we add += rather than set =to that variable?

TEXINPUTS can contain many paths.

@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

You should pass your ${textemplateDir} here just as the working directory(cwd).

await exec(cmd, { cwd: variables.currentDir });

The concatination of multiple textemplateDirs are different on different platform(unix: join paths with :, windows: join paths with;).

@universvm
Copy link
Contributor Author

universvm commented Jun 7, 2023

Sorry I'm not sure I follow. This is the command we want:

export TEXINPUTS="${textemplateDir}":./:$TEXINPUTS

If you don't mind, could commit the change and I can test it? I'm going crazy working with typescript and doing the template took a really long time...

@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

Sorry I'm not sure I follow. This is the command we want:

export TEXINPUTS="${textemplateDir}":./:$TEXINPUTS

If you don't mind, could commit the change and I can test it? I'm going crazy working with typescript and doing the template took a really long time...

Just like this:

TEXINPUTS = joinEnvPath(textemplateDir, orignal_TEXINPUTS)

export function joinEnvPath(...paths: string[]) {
switch (platform) {
case 'win32':
return paths.join(';');
default:
return paths.join(':');
}
}

@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

Saw your email, don't worry. Please rewrite following code:

await exec(cmd, { cwd: variables.currentDir });

as:

// orginal `$TEXINPUTS` should be empty, so ignore it.
await exec(cmd, { cwd: variables.currentDir, env: { TEXINPUTS: joinEnvPath('path/to/texttemplates/', './') } }); 

@universvm
Copy link
Contributor Author

universvm commented Jun 7, 2023

Hey @mokeyish I think we are close! I changed the variable to this:

 await exec(cmd, { cwd: variables.currentDir, env: { TEXINPUTS: joinEnvPath(`${textemplateDir}`, `${process.env.TEXINPUTS}`) } }); 

Without the previous TEXINPUTS, latex throws errors like no article.cls available.

The problem at the moment is that it says "Success!" and no file is produced. Do you know what could be happening?

src/exporto0o.ts Outdated Show resolved Hide resolved
src/exporto0o.ts Outdated Show resolved Hide resolved
src/exporto0o.ts Outdated Show resolved Hide resolved
@mokeyish
Copy link
Owner

mokeyish commented Jun 7, 2023

Hey @mokeyish I think we are close! I changed the variable to this:

 await exec(cmd, { cwd: variables.currentDir, env: { TEXINPUTS: joinEnvPath(`${textemplateDir}`, `${process.env.TEXINPUTS}`) } }); 

Without the previous TEXINPUTS, latex throws errors like no article.cls available.

The problem at the moment is that it says "Success!" and no file is produced. Do you know what could be happening?

Are you sure it's an environment variable issue? Obsidian's environment variables are only the most basic, and we have to see what is missing and add the rest.

just like

case 'darwin': {
let envPath = process.env['PATH'];
const extraBins = ['/usr/local/bin', '/Library/TeX/texbin'];
for (const bin of extraBins) {
if (envPath.includes(bin)) continue;
envPath = joinEnvPath(bin, envPath);
}
env.PATH = envPath;

@universvm universvm marked this pull request as ready for review June 9, 2023 18:04
Repository owner deleted a comment from universvm Jun 9, 2023
@mokeyish
Copy link
Owner

mokeyish commented Jun 9, 2023

I'm on windows and can't test latex. Let's do this for now, and optimize it slowly later.

@universvm
Copy link
Contributor Author

Nice, is this good to be merged then?

@mokeyish
Copy link
Owner

Nice, is this good to be merged then?

It cannot be merged yet, latex templates are currently hard-coded. We should make it configurable.

@universvm
Copy link
Contributor Author

universvm commented Jun 10, 2023

Nice, is this good to be merged then?

It cannot be merged yet, latex templates are currently hard-coded. We should make it configurable.

There is a dropdown menu. You made this - I just modified the TEXINPUTS from your commit

Screenshot 2023-06-10 at 08 09 29

src/exporto0o.ts Outdated Show resolved Hide resolved
@universvm
Copy link
Contributor Author

Sorry I thought I fixed the conflicts on merge... give me a minute.

src/ui/ExportDialog.tsx Outdated Show resolved Hide resolved
@universvm
Copy link
Contributor Author

Ok I can add your changes but as I mentioned before the app does not work. It loads forever when exporting.

@mokeyish
Copy link
Owner

@universvm Hi Leo, Please have a look at the latest code, now you can add Dropdown through configuration.

Here is the full command template.

{
    name: 'PDF',
    type: 'pandoc',
    arguments:
      '-f markdown --resource-path="${currentDir}" --resource-path="${attachmentFolderPath}" --lua-filter="${luaDir}/pdf.lua" ${ options.textemplate ? `--template="${pluginDir}/textemplate/${options.textemplate}"` : `` } --embed-resources --standalone --metadata title="${currentFileName}" -s -o "${outputPath}" -t pdf',
    customArguments: '--pdf-engine=typst',
    optionsMeta: {
      'textemplate': {
        title: 'Latex Template',
        type: 'dropdown',
        options: [
          { name: 'None', value: null },//
          { name: 'Dissertation', value: 'dissertation.tex' },
          { name: 'Academic Paper', value: 'academic.tex' }
        ]
      }
    },
    extension: '.pdf',
  }

There are two points above.

  1. Add following snippet to arguments
    ${ options.textemplate ? `--template="${pluginDir}/textemplate/${options.textemplate}"` : `` }
    
  2. Add following snippet to optionsMeta
    {
      optionsMeta: {
       'textemplate': {
         title: 'Latex Template',
         type: 'dropdown',
         options: [
           { name: 'None', value: null },//
           { name: 'Dissertation', value: 'dissertation.tex' },
           { name: 'Academic Paper', value: 'academic.tex' }
         ]
       }
     }
    }

test('test Template rendering options.textemplate', async () => {
expect(renderTemplate('pandoc ${ options.textemplate ? `--template="${options.textemplate}"` : `` }',
{ options: { textemplate: 'dissertation.tex' } }))
.toBe('pandoc --template="dissertation.tex"');
expect(renderTemplate('pandoc ${ options.textemplate ? `--template="${options.textemplate}"` : `` }',
{ options: { textemplate: null } }))
.toBe('pandoc ');
});

@universvm Hi Leo,
With the latest code, Adding latex dropdown just change the export template definition above

@universvm
Copy link
Contributor Author

@mokeyish see the code now. I did what you said but the plugin does not work. Unfortunately I cannot work on this anymore.

If you can fix the bug feel free to merge. If not close this issue and I will publish my templates somewhere else.

@mokeyish
Copy link
Owner

I also don't really want to spend time on this project 😂. I wrote this project because I couldn't find a suitable tool for sharing notes and wanted to export it to the hugo blog, so I spent a few days writing such a plugin.

If you don't want to continue to toss. You can put the final TEXINPUTS value and the final export command. Check it out if I have time.

@universvm
Copy link
Contributor Author

@mokeyish Addressed all the changes you had made + fixed a small bug.

The templates will be published in here: https://github.com/universvm/academic-pandoc-templates

I will keep this plugin updated with the templates. The repository above contains tutorials on how to use the templates etc..

@universvm universvm requested a review from mokeyish June 14, 2023 14:51
@mokeyish
Copy link
Owner

Let's close this PR, and could you resubmit a new one base on the branch https://github.com/mokeyish/obsidian-enhancing-export/tree/dev ?

please use git rebase to keep git logs clean. Here is the git rebase documentation: https://git-scm.com/docs/git-rebase . Or use git gui such as https://www.syntevo.com/smartgit/, I pretty like smartgit.

@mokeyish mokeyish closed this Jun 16, 2023
@mokeyish
Copy link
Owner

Adding a screenshot, we can see that the commit log without rebase looks very confusing.

图片

// show progress
progress.setMessage(lang.preparing(outputFileFullName));
beforeExport && beforeExport();
progress.show();

const pandocPath = getPlatformValue(globalSetting.pandocPath) ?? 'pandoc';

const cmdTpl =
let cmdTpl =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are unnecessary. The template render function will do this automatically. Do you understand?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole changes of this file are also unnecessary.

@universvm
Copy link
Contributor Author

Hi @mokeyish

Could you have a look now? I reverted the changes you mentioned and tested.

If all is good I will clean up the commit history. If not let me know I can change

@universvm
Copy link
Contributor Author

@mokeyish I am having problems with conflicts squashing the commits. Can we squash everything into one when merging to dev?

@mokeyish
Copy link
Owner

@mokeyish I am having problems with conflicts squashing the commits. Can we squash everything into one when merging to dev?

No need to resolve conflicts. create a new branch to applly your changes.

@universvm
Copy link
Contributor Author

@mokeyish I am having problems with conflicts squashing the commits. Can we squash everything into one when merging to dev?

No need to resolve conflicts. create a new branch to applly your changes.

Ah ok! Will probably do these tomorrow :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding latex template support
2 participants