Skip to content

Conversation

@schrimpf
Copy link
Contributor

This pull request adds a chunk option to be re-evaluated as code.

It partially addresses #89 by allowing an external file to be included as

    ```julia; output_as_code=true
    print(read("filename.jl",String))
    ```

When used in combination with Revise.jl and CodeTracking.jl, it also allows including the text of function definitions from modules in the Weaved document. For example, weaving the following document

    ```julia; output_as_code=true; output_eval=false                                                                                                                             
    using Revise, CodeTracking, InteractiveUtils                                                                                                                                 
    using Random                                                                                                                                                                 
    print(definition(@which rand(2)))                                                                                                                                            
    ```

produces

begin
    #= /build/julia/src/julia-1.2.0/usr/share/julia/stdlib/v1.2/Random/src/Random.jl:254 =#
    rand(dims::Integer...) = begin
            #= /build/julia/src/julia-1.2.0/usr/share/julia/stdlib/v1.2/Random/src/Random.jl:254 =#
            rand(Float64, Dims(dims))
        end
end

You can get nicer output by stripping comments with e.g.

'''julia; output_as_code=true; output_eval=false                                                                                                                             
using Revise, CodeTracking, InteractiveUtils                                                                                                                                 
using Random  
                                                                                                                                                               
function definition_text(method::Method)                                                                                                                                     
  function reformat(txt)                                                                                                                                                     
    # remove begin...end block and its indentation                                                                                                                           
    txt = replace(txt,r"^begin\n"=>"\n")                                                                                                                                     
    txt = replace(txt,r"end$"=>"")                                                                                                                                           
    txt = replace(txt,r"\n    " => "\n")                                                                                                                                     
    # strip line number comments                                                                                                                                             
    txt = replace(txt,r"(#= .+ =#)( |\n)" => s"\2")                                                                                                                          
    txt = replace(txt,r"\n\s+\n"=>"\n")                                                                                                                                      
    # change indentation from 4 to 2                                                                                                                                         
    maxdepth = 10                                                                                                                                                            
    for d in 1:maxdepth                                                                                                                                                      
      r = Regex("(\\n) {$(4*d)}(\\S)")                                                                                                                                       
      s = SubstitutionString("\\1"*prod(fill(" ",2*d))*"\\2")                                                                                                                
      txt = replace(txt,Pair(r,s))                                                                                                                                           
    end                                                                                                                                                                      
    txt                                                                                                                                                                      
  end                                                                                                                                                                        
  txt = string(definition(method))                                                                                                                                           
  txt = reformat(txt)                                                                                                                                                        
end                                                                                                                                                                          
                                                                                                                                                                             
using Random                                                                                                                                                                 
print(definition_text(@which rand(2)))                                                                                                                                       
''' 

which creates output with

rand(dims::Integer...) = begin
    rand(Float64, Dims(dims))
  end

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.

1 participant