-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add: function to add line numbers to block #220
Open
dlesnoff
wants to merge
10
commits into
pietroppeter:main
Choose a base branch
from
dlesnoff:codeLines
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ca00999
Add: function to add line numbers to block. Not called yet.
dlesnoff 4e58789
Move away proc->func addLineNumbersToHighlightedCode and attempts at …
dlesnoff 5947550
Add small test
dlesnoff 2791e79
Add a beginning documentation (and visual test)
dlesnoff ccfacea
Remove addLineNumbers from src/nimib.nim
dlesnoff c06117f
Update render{Plans, Procs}; remove test
dlesnoff f772975
Small 's' omit broke rendering
dlesnoff 82d5182
Fix addLineNumbers
dlesnoff 60c4fa8
Merge with main
dlesnoff 72ebbd1
Change the HTML approach to display numbers, warning: test broken
dlesnoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,13 @@ task docsdeps, "install dependendencies required for doc building": | |
exec "nimble -y install [email protected] [email protected] nimoji nimpy [email protected]" | ||
|
||
task test, "General tests": | ||
for file in ["tsources.nim", "tblocks.nim", "tnimib.nim", "trenders.nim"]: | ||
for file in ["tsources.nim", "tblocks.nim", "tnimib.nim", "trenders.nim", "tcodeLines.nim"]: | ||
exec "nim r --hints:off tests/" & file | ||
for file in ["tblocks.nim", "tnimib.nim", "trenders.nim"]: | ||
exec "nim r --hints:off -d:nimibCodeFromAst tests/" & file | ||
|
||
task readme, "update readme": | ||
exec "nim -d:mdOutput r docsrc/index.nim" | ||
exec "nim -d:mdOutput r docsrc/index.nim" | ||
|
||
task docs, "Build documentation": | ||
for file in ["allblocks", "hello", "mostaccio", "numerical", "nolan", | ||
|
@@ -34,4 +34,4 @@ task docs, "Build documentation": | |
exec "nim r --hints:off docsrc/" & file & ".nim" | ||
when not defined(nimibDocsSkipPenguins): | ||
exec "nim r --hints:off docsrc/penguins.nim" | ||
exec "nimble readme" | ||
exec "nimble readme" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import std / [strutils, tables, sugar, os, strformat, sequtils] | ||
import std / [strutils, tables, sugar, sequtils] | ||
import ./types, ./jsutils, markdown, mustache | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are unused imports. |
||
|
||
import highlight | ||
|
@@ -10,6 +10,23 @@ proc mdOutputToHtml(doc: var NbDoc, blk: var NbBlock) = | |
proc highlightCode(doc: var NbDoc, blk: var NbBlock) = | ||
blk.context["codeHighlighted"] = highlightNim(blk.code) | ||
|
||
proc addLineNumbersToHighlightedCode(code: string): string = | ||
let nlines = code.splitLines().len | ||
# var lineNumbers: seq[string] = @[] # newSeqOfCap(nlines) | ||
for i in 1..nlines: | ||
## We have line numbers in the same line than code | ||
result.add("""<span class="hljs-comment"> """ & $i & "</span> " & code.splitlines()[i-1] & "\n") | ||
## but we can not copy-paste code without the numbers | ||
# The following separates line numbers from code, but display is wrong | ||
# result = """ <div> | ||
# <code>""" & lineNumbers.join("") & """</code> | ||
# <code>""" & code & """</code> | ||
# </div> | ||
# """ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should wrap the code in a table or a div container so that we can copy-paste code without the numbers. |
||
proc addLineNumbers(doc: var NbDoc, blk: var NbBlock) = | ||
if blk.context["enableLineNumbers"].castBool or doc.context["enableLineNumbers"].castBool: | ||
blk.context["codeHighlighted"] = addLineNumbersToHighlightedCode(blk.context["codeHighlighted"].castStr) | ||
|
||
proc useHtmlBackend*(doc: var NbDoc) = | ||
doc.partials["nbText"] = "{{&outputToHtml}}" | ||
|
@@ -45,14 +62,15 @@ proc useHtmlBackend*(doc: var NbDoc) = | |
# I prefer to initialize here instead of in nimib (each backend should re-initialize) | ||
doc.renderPlans = initTable[string, seq[string]]() | ||
doc.renderPlans["nbText"] = @["mdOutputToHtml"] | ||
doc.renderPlans["nbCode"] = @["highlightCode"] # default partial automatically escapes output (code is escaped when highlighting) | ||
doc.renderPlans["nbCodeSkip"] = @["highlightCode"] | ||
doc.renderPlans["nbCode"] = @["highlightCode", "addLineNumbers"] # default partial automatically escapes output (code is escaped when highlighting) | ||
doc.renderPlans["nbCodeSkip"] = @["highlightCode", "addLineNumbers"] | ||
doc.renderPlans["nbJsFromCodeOwnFile"] = @["compileNimToJs", "highlightCode"] | ||
doc.renderPlans["nbJsFromCode"] = @["highlightCode"] | ||
doc.renderPlans["nimibCode"] = doc.renderPlans["nbCode"] | ||
|
||
doc.renderProcs = initTable[string, NbRenderProc]() | ||
doc.renderProcs["mdOutputToHtml"] = mdOutputToHtml | ||
doc.renderProcs["addLineNumbers"] = addLineNumbers | ||
doc.renderProcs["highlightCode"] = highlightCode | ||
doc.renderProcs["compileNimToJs"] = compileNimToJs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import nimib | ||
import unittest | ||
|
||
suite "test sources": | ||
nbInit() | ||
enableLineNumbersDoc() | ||
nbCode: | ||
# a comment | ||
let | ||
x = 1 | ||
nbSave() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, the diff contains a lot of line changes because of some automatic tool in my Vim editor to remove whitespaces at the end of lines.