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

Compile from string #2

Open
piever opened this issue Oct 11, 2018 · 2 comments · May be fixed by #26
Open

Compile from string #2

piever opened this issue Oct 11, 2018 · 2 comments · May be fixed by #26

Comments

@piever
Copy link
Owner

piever commented Oct 11, 2018

libsass allows to compile from string, though that currently doesn't work here. If I try:

context = sass_make_data_context("div { a { color: blue; } }")
compiler = sass_make_data_compiler(context)
sass_compiler_parse(compiler)
sass_compiler_execute(compiler)
sass_delete_compiler(compiler)

It segfaults when deleting the compiler as it also tries to deallocate the Julia string. One can either:

  • not delete the compiler and accept some memory leak
  • figure out how to "pass ownership of a buffer from Julia to C"

The second option seems better but I have no idea how to do it.

@hhaensel
Copy link

hhaensel commented May 17, 2023

A workaround for doing this would be

function compile_sass(io::IOBuffer, args...; kwargs...)
  local css
  mktemp(pwd()) do filepath, fileio
    sass = String(take!(io))
    is_indented = get(kwargs, :is_indented_syntax_src, false)
    write(fileio, sass)
    close(fileio)
    is_indented && mv(filepath, filepath * ".sass")
    css = try
      Sass.compile_file(filepath * (is_indented ? ".sass" : ""), args...; kwargs...)
    catch e
      @warn e
    finally
      is_indented && mv(filepath * ".sass", filepath)
    end
  end
  css
end

compile_sass(s::AbstractString, args...; kwargs...) = compile_sass(IOBuffer(s), args...; kwargs...)

At least for me (OS: Windows, Julia: 1.9), calling with is_indented_syntax_src = true had no effect. File ending seems to have precedence over the option. So I renamed the temp file to end with ".sass"

@hhaensel
Copy link

Inspired by https://github.com/sass/libsass/blob/master/docs/api-context-example.md I found the true solution:

function sass_copy_string(s)
    ccall((:sass_copy_c_string, Sass.libsass_so),
        Ptr{Cstring}, (Cstring,), s)
end
  
text = sass_copy_string("div { a { color: blue; } }")
context = sass_make_data_context(text)
compiler = sass_make_data_compiler(context)
sass_compiler_parse(compiler)
sass_compiler_execute(compiler)
output = sass_context_get_output_string(context)
sass_delete_data_context(context)
sass_delete_compiler(compiler)

I'll submit a PR to make that available in the julia api.

@hhaensel hhaensel linked a pull request May 17, 2023 that will close this issue
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 a pull request may close this issue.

2 participants