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

Parse math/latex in table input. #994

Open
iabraham opened this issue Jan 29, 2023 · 4 comments
Open

Parse math/latex in table input. #994

iabraham opened this issue Jan 29, 2023 · 4 comments

Comments

@iabraham
Copy link

How do I make Franklin parse Latex or math equations when using the \tableinput{}{} command?

Currently if I have a CSV file

Equation, Formula
Euler, $e^{ix} = \cos x + i \sin x$
Einstein, $e=mc^2$

it doesn't get parsed to math when input into a markdown file using \tableinput{}{} command.

@tlienart
Copy link
Owner

tlienart commented Jan 30, 2023

Hello @iabraham right, the \tableinput does not do a "secondary" pass (i.e. after having parsed the CSV file it writes HTML assuming there's no more parsing to be done). We could change that, just no-one had brought a use cases before 😄

In your case we can just write a short function that does this for you, it'll be a bit different than \tableinput but I'm hoping it'll also be more transparent so you can adjust it to your needs; if you put this in utils.jl

using DelimitedFiles

function hfun_mytableinput(params)
  fpath = joinpath("_assets", params[1])
  content = readdlm(fpath, ',')

  io = IOBuffer()
  write(io, """
    <table>
    <thead>
      <tr>
    """)
  
  header = content[1, :]
  for col in header
    write(io, """
      <th>$col</th>
      """)
  end
  write(io, "</thead><tbody>")

  for rowidx in 2:size(content, 1)
    row = content[rowidx, :]
    write(io, "<tr>")
    for col in row
      write(io, "<td>$(fd2html(col; internal=true, nop=true))</td>")
    end
    write(io, "</tr>")
  end

  write(io, """
      </tbody>
    </table>
    """)
  return String(take!(io))
end

What it will do is it will read a CSV file assumed to be in _assets/ and write a table having processed each entry as if it was markdown (that's the internal fd2html call). It also assumes the first row is the header, I let you adjust that if you see the need.

So as an example if you put the exact content you showed in a file _assets/example.csv and have an index.md like:

+++
hasmath = true
+++

# Hello

{{mytableinput example.csv}}

The result will be:

Screenshot 2023-01-30 at 09 28 30

Note: you have to explicitly specify that hasmath = true here as there's no "actual" equations on the page so that Franklin wouldn't know it has to include KaTeX on the page.

@iabraham
Copy link
Author

iabraham commented Feb 4, 2023

@tlienart Perfect, that does the job (note: the above would with thead and th ended up designating first two rows as header so I had to modify the HTML) but it works.

Thanks for this.

Could you point me to how to make Franklin look for the CSV file in the appropriate folder based on where the index.md file is written? For example if the index.md file is in: _assets/<subfolder>/<page1>/index.md then the \input command knows to look in _assets/<subfolder>/<page1>/ and then appropriately code and output folders.

Is there someway for me to make hfuns work in a similar fashion?

@tlienart
Copy link
Owner

tlienart commented Feb 5, 2023

In the hfun you can use locvar(:fd_rpath) (sorry it's obscure but actually in the docs under page variables) and it will give you the relative path to the current file (e.g foo/bar/index.md, so the relative path to the file that calls the hfun)

Note that if you place .md files in the _assets folder they will not be processed (that folder is just taken as a "copy this content in the appropriate location doing nothing else with it")

@iabraham
Copy link
Author

iabraham commented Feb 5, 2023

Oh right ... my bad. I had mean to say that if index.md is in /<category>/<page1>/index.md then Franklin knows to look for files in /_assets/<category>/<page1>/ when using the \input command. I will take a look at using locvar(:fd_path). Thanks!

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

No branches or pull requests

2 participants