Skip to content

Conversation

eihli
Copy link
Contributor

@eihli eihli commented Sep 14, 2022

To link to a chapter/section:
[anchor text](@chapter or section title)

This change adds a slot to the weaver to store a map of chapter/section titles to their IDs.

Then when we're weaving, we search for these links and replace them with their anchor text.

This commit is a work-in-progress that I want to at least get out to start bouncing ideas around.

To link to a chapter/section:
[anchor text](@chapter or section title)

This change adds a slot to the weaver to store a map
of chapter/section titles to their IDs.

Then when we're weaving, we search for these links and
replace them with their [anchor text](#c:id).

This commit is a work-in-progress that I want to at least
get out to start bouncing ideas around.

--- foobaz
(format nil (* 2 2))
---
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was using this as a scratch file to play around with in a REPL.

(let* ((file-defs (parse-lit-files '("dev.lit")))
        (weaver (make-weaver-default file-defs)))
   (add-section weaver '("Some Chapter") "c2")
   ; ...
   )


(defun weave-prose-line (weaver line def)
(loop for expr in line do
(cond ((stringp expr) (write-string expr))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't intend to make this blank space change. I have a habit of running M-q for paredit-reindent-defun and saved/committed this and didn't want to bother with undoing it yet.

weave.lisp Outdated
(replace-sections-with-id-anchors
(weaver-section-map weaver)
expr))
(write-string expr)))
Copy link
Contributor Author

@eihli eihli Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ This is the new bit that is obscured by all of the blank space changes. This along with the comment below are the two core pieces. However it's implemented, these seem like the two most reasonable places to hook in to (1) map section/chapter titles to IDs and (2) replace the link references with their IDs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any regex scanning like this should happen in the parser. By the time it gets here it should be a structured command.
For example, all the here is a line with a @{reference} becomes "here is a line with a " (:INCLUDE "reference"). So similarly, this should be a command type like (:SECTION-REFERENCE "name").

utils.lisp Outdated
;;
;; I haven't yet gotten close to finishing that implementation. So it
;; doesn't make much sense to have this data structure right now.
;; But oh well. Whatever. It works for an MVP of the chapter/section linking stuff.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Of course that could also be done with simple string matching. I was also thinking this could be useful for something like letting you use [some section](@some section) if it's unique but [some section](@some chapter:@some-section) if the section title wasn't unique. (Etc. for @some file name). Basically... I had a very loosely woven idea that it might be useful and it seemed like a fun way to practice LISP. I don't think it's actually worthwhile for anything.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had not thought of this idea. I am perfectly open to giving it a try and think its a cool idea. I am also just fine with forcing them to write out the title.

weave.lisp Outdated
(section-id
(weaver-section-counter weaver)
(weaver-chapter-counter weaver)))
(add-section weaver (list (second expr)) (section-id (weaver-section-counter weaver)
Copy link
Contributor Author

@eihli eihli Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this out from all the noise. One core piece of this here on line 289 and 298.

This seemed like a good place to hook in and save off the mapping of chapter/section titles to IDs.

Copy link
Owner

@justinmeiners justinmeiners Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table of contents toc structure on weaver should have all the information you need. It it will specify what all the chapters and sections are and which files they are in. (Just try printing it out and seeing). I would recommend making a pure function which takes the toc as input and produces whatever identifier trie you need.

I would also probably put that function in toc.lisp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table of contents toc structure on weaver should have all the information you need.

Ah! That's obvious now.

@justinmeiners
Copy link
Owner

justinmeiners commented Sep 14, 2022

I have only looked at this briefly. What do you think about the idea of making @chapter-name (whatever the syntax is) expand to the anchor/relative link, regardless of whether it's in a markdown link or not?

From my experience there are actually a lot of subtle rules about how markdown is expanded (likely there are edge cases), and a lot of the complexity here seems to be parsing those links.

@eihli
Copy link
Contributor Author

eihli commented Sep 14, 2022

I have only looked at this briefly. What do you think about the idea of making @chapter-name (whatever the syntax is) expand to the anchor/relative link, regardless of whether it's in a markdown link or not?

I think I like that. Keeps a clear separation between what's weave and what's markdown.

Rather than [foo](@foo) --weave--> [foo](#c1) --markdown--> <a href...> it's just @{## foo} --weave--> <a href...>.

As for syntax, I like the idea of keeping parity with the code blocks by re-using the same @{...} syntax. But to differentiate between the name of a code block and the name of a section/chapter, maybe include the # and ## in the reference to a section/chapter.

So @{foo} for the code block named foo and @{## foo} for the section titled foo.

@justinmeiners
Copy link
Owner

@eihli great! Let me take an in-depth look tonight and give you more appropriate feedback.

weave.lisp Outdated
;;
;; Right now, it doesn't work across files in a book.
;; Heck... it barely works at all.
(section-map '() :type list)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be conscious of multiple files. When a chapter/section is referenced in the same file it should be a relative link (just the anchor portion). When it is referenced in a different file, that file name will be prepended to the anchor. See how block reference anchors are produced.

@justinmeiners
Copy link
Owner

justinmeiners commented Sep 15, 2022

@eihli

My comments are probably hard to read out of order, so here is a summary:

  1. If you don't see any problems, let's go ahead with a reference which expands to an anchor (using syntax just like the one you proposed), rather than attempting to parse markdown link syntax.
  2. Make sure you do all the regex finding in the parse step. You should have a structured command with symbols by the time it gets to the weave.
  3. I think you can utilize the toc structure to get what you want, if not let's improve it.

@justinmeiners
Copy link
Owner

justinmeiners commented Sep 15, 2022

Thinking out loud. There is a slight inconsistency in our proposed syntax.
For a block, using @{block name} always produces a link (an tag), but what we are proposing for sections produces just the anchor/href. Perhaps we should have one syntax for producing anchors/hrefs (for blocks, chapters, and sections) and another one which wraps that functionality to produce full links.

Perhaps @{block name} @{# heading} @{## section} is appropriate to expand to a link, and something like @anchor{block name} @anchor{# heading} @anchor{## section} would expand to the href. Just an idea.

@justinmeiners
Copy link
Owner

Excited to see where this goes. Just following up to see if you need anything from me.

@eihli
Copy link
Contributor Author

eihli commented Oct 1, 2022

Excited to see where this goes. Just following up to see if you need anything from me.

No questions or anything yet. Just haven't had much free time lately.

Detect # vs ## and use the list (:ANCHOR (:C "Ch Title"))
or (:ANCHOR (:S "Section Title")) depending on which.
@eihli eihli force-pushed the link-to-sections branch from 10fb425 to 768a5b6 Compare October 1, 2022 18:02
@eihli
Copy link
Contributor Author

eihli commented Dec 11, 2022

Just a status update as of commit cb4e960.

Most of the diff is either space changes that can be ignored or comment blocks with scratch code that helped me explore. There's only a few lines of new code.

I've been using paredit-reindent-defun to manage indentation. My config must be different from yours because it results in a lot of space edits. I'm not going to worry about them for now. You can view a diff that ignores space changes by adding ?w=1 to a Github URL: https://github.com/justinmeiners/srcweave/pull/28/files?w=1

This create-global-toc-linkmap function creates a mapping of chapter/section name to the file.html#s0:1 url path of the woven output.

https://github.com/justinmeiners/srcweave/pull/28/files?w=1#diff-932b4bb5bad7e82585c83d4d8fcaf295e66649c16f2c949a2e02e915438d1623R52

And then the :ANCHOR gets woven into an a href at https://github.com/justinmeiners/srcweave/pull/28/files?w=1#diff-4f8d805adf8da1e80d7ab36038b531fb74ac5aba28588c867ef469785e837facR271.

Not great yet. Can't provide link text for the anchor that's different from the chapter/section name that you're linking to.

But getting there.

I've been using some scratch files to test/iterate. Will update the tests once things settle down.

srcweave --weave dist --formatter srcweave-format dev.lit scratch.lit

links

@justinmeiners
Copy link
Owner

Really excited! I will review this in detail soon.

@justinmeiners
Copy link
Owner

justinmeiners commented Jan 2, 2023

@eihli Just letting you know I had another personal delay.

As an aside, I tried to find your contact information on your site. If you are interested in providing that feel free to send me an email (on my github page).

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.

2 participants