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

Move custom parsing of code cell options to new extension #160

Open
coatless opened this issue Feb 25, 2024 · 0 comments
Open

Move custom parsing of code cell options to new extension #160

coatless opened this issue Feb 25, 2024 · 0 comments
Labels
p: low Address issue when time permits t: chore
Milestone

Comments

@coatless
Copy link
Owner

Relevant lines to move to:

https://github.com/coatless-quarto/codecelloptions

Are:

-- Remove lines with only whitespace until the first non-whitespace character is detected.
local function removeEmptyLinesUntilContent(codeText)
-- Iterate through each line in the codeText table
for _, value in ipairs(codeText) do
-- Detect leading whitespace (newline, return character, or empty space)
local detectedWhitespace = string.match(value, "^%s*$")
-- Check if the detectedWhitespace is either an empty string or nil
-- This indicates whitespace was detected
if isVariableEmpty(detectedWhitespace) then
-- Delete empty space
table.remove(codeText, 1)
else
-- Stop the loop as we've now have content
break
end
end
-- Return the modified table
return codeText
end
-- Extract Quarto code cell options from the block's text
local function extractCodeBlockOptions(block)
-- Access the text aspect of the code block
local code = block.text
-- Define two local tables:
-- the block's attributes
-- the block's code lines
local cellOptions = {}
local newCodeLines = {}
-- Iterate over each line in the code block
for line in code:gmatch("([^\r\n]*)[\r\n]?") do
-- Check if the line starts with "#|" and extract the key-value pairing
-- e.g. #| key: value goes to cellOptions[key] -> value
local key, value = line:match("^#|%s*(.-):%s*(.-)%s*$")
-- If a special comment is found, then add the key-value pairing to the cellOptions table
if key and value then
cellOptions[key] = value
else
-- Otherwise, it's not a special comment, keep the code line
table.insert(newCodeLines, line)
end
end
-- Merge cell options with default options
cellOptions = mergeCellOptions(cellOptions)
-- Set the codeblock text to exclude the special comments.
cellCode = table.concat(newCodeLines, '\n')
-- Return the code alongside options
return cellCode, cellOptions
end

@coatless coatless added p: low Address issue when time permits t: chore labels Feb 25, 2024
@coatless coatless added this to the 4.2 (next release) milestone May 5, 2024
@coatless coatless modified the milestones: 0.4.2 (next release), 0.4.x Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: low Address issue when time permits t: chore
Projects
None yet
Development

No branches or pull requests

1 participant