class JupyterNB::Cell

Public Class Methods

new(type, metadata, outputs, source) click to toggle source

Constructor @param [String] type defines the type of the cell (markdown, code, …) @param metadata can be given as a string with linebreaks or as an array of strings @param outputs can be given as a string with linebreaks or as an array of strings def add_cell(type,metadata,outputs,source) @param source can be given as a string with linebreaks or as an array of strings

# File lib/cell.rb, line 31
def initialize(type, metadata, outputs, source)
        @type = type
        @metadata = read_parameter(metadata)
        @source = read_parameter(source)
        @count = 0
end

Public Instance Methods

generate(indent=1,last=false) click to toggle source

Returns a string that contains an IPython Notebook cell

The outputs field is not implemented yet. Should actually not be necessary for a pure generator. Generation of metadata within the cell is also not yet implemented.

@param [Integer] indent defines the indentation of the cell content @param [Boolean] last if set to true, the trailing ',' is omitted

# File lib/cell.rb, line 46
def generate(indent=1,last=false)
        @indent = indent

        result = ""
        result << open_group
        result << add_field("cell_type", @type)
        result << add_field("execution_count", @count) if (@type == "code")
        result << open_group("metadata")
        # not implemented yet
        result << close_group

        result << open_array("source")

        @source.each do |l|
                result << add_string(l)

                (l.equal? @source.last) ? result << "\n" : result << ",\n"
        end
        result << close_array(true)
        result << close_group(last)

        return result
end

Private Instance Methods

read_parameter(param) click to toggle source

Returns an array of strings for multi-line parameters like metadata or source @param param can be given as a string with linebreaks or as an array of strings

# File lib/cell.rb, line 74
def read_parameter (param)
        result = []
        
        unless (param.nil? == true)
                if param.is_a?(String)
                        param.each_line do |l|
                                result << l.chomp
                        end
                elsif param.is_a?(Array)
                        param.each do |l|
                                next unless l.is_a?(String)

                                result << l
                        end
                end
        end

        return result
end