habamax / vim-asciidoctor

Asciidoctor plugin for Vim
MIT License
181 stars 16 forks source link

Convert to HTML on save or Live Preview #22

Closed jfernandz closed 4 years ago

jfernandz commented 4 years ago

This is only a suggestion, but ... Have you considered to add a kind of option to produce the output file when you are saving with :w or even a live preview similar to this one for markdown?

habamax commented 4 years ago

Have you considered to add a kind of option to produce the output file when you are saving with :w

Not really. It should be easy to set up in your settings instead using autocommands.

or even a live preview similar to this one for markdown?

This one is interesting, but not as an option, it should be a separate plugin to manage all the complexity and external deps.

What you can have now is just a browser extension (asciidoctorjs?) that autoreloads after you have saved your file. The only big missing part from markdown link you provided is following the cursor/change.

jfernandz commented 4 years ago

Not really. It should be easy to set up in your settings instead using autocommands.

Could you provide me a clue on how to do that? Is it like a sort of macro? I guess I'd need a conditional structure to chain :w and :Asciidoctor2HTML if the saved file has an asciidoc extension like .adoc or .asc ... but what about if the file is a .txt?

habamax commented 4 years ago

Could you provide me a clue on how to do that?

augroup ON_ASCIIDOCTOR_SAVE | au!
    au BufWritePost *.adoc :Asciidoctor2HTML
augroup end

If you want it to be silent and in background, consider installing vim-dispatch and

augroup ON_ASCIIDOCTOR_SAVE | au!
    au BufWritePost *.adoc :compiler asciidoctor2html | Make!
augroup end

but what about if the file is a .txt?

Well, if you are sure it could be translated with asciidoctor then add *.txt

augroup ON_ASCIIDOCTOR_SAVE | au!
    au BufWritePost *.adoc,*.txt :compiler asciidoctor2html | Make!
augroup end
habamax commented 4 years ago

For a txt file you can try to analyze first line and if it is = Title then do conversion:

func! ConvertAsciidoctorToHTML()
    " Text file with asciidoctor contents?
    if &filetype == 'text' && getline(1) =~ '^= .*$'
        " text files have no asciidoctor commands
        set filetype=asciidoctor
        Asciidoctor2HTML
        set filetype=text
    elseif &filetype == 'asciidoctor'
        Asciidoctor2HTML
    endif
endfunc
augroup ON_ASCIIDOCTOR_SAVE | au!
    au BufWritePost *.adoc,*.txt call ConvertAsciidoctorToHTML()
augroup end
habamax commented 4 years ago

I have added this info to readme, closing