LQWiki:Emacs tips and tricks/External links formatting

From LQWiki
Jump to navigation Jump to search

This is a repository of external link formatting functions

Those functions were used when doing heavy reformatting ( think hundreds of pages ) on the wiki.

One function explanation

To get an idea of what this is all about we will take a look at one function, my-lqwiki-reformat-external-link-parenthesize-domain-name

External links like :
[http://my.domain.com/some/path/page_name.html The page's title]
will be formatted like this :
[http://my.domain.com/some/path/page_name.html The page's title] (''my.domain.com'')
Which should look like :
The page's title (my.domain.com)
  • An external link to be formatted :
    • Stands on one line only.
    • Is not followed by "optional spaces + (", this avoids reformatting already formatted links.
    • Is a link of the form : [address title] -- (the [address] kind of link won't be formatted).
  • Extra spaces after the enclosing "]" are removed, leaving only one or none if at the end of a line.

The key combination to launch this function is "control+alt+l" and then "u".

This is a query-replace, this will cover links from the current line to the end of page. When asked type space to format the underlined link ( a couple of other characters are too ) or "n" to skip and look for the next.

Doing all external links formatting in one go

You can run all the formatting functions in the proper order by simply running my-lqwiki-reformat-external-link-all-formatting (control+alt+l A)

The query-replace function will present you with the name of the proposed formatting in the minibuffer area.

I personnaly apply those functions on particular subsections of a buffer, but i didn't put all the code relating to that on the wiki so for now the function will apply on the whole buffer, or only on the "External links" section if any.

Please report problems in the discussion page if any.

This is for convenience only, use at your own risk ( always like to put this one. Seriously use the undo function of course :) ).

Elisp code

Elisp code to put in the relevant place (e.g .emacs)

(progn
  (defun my-lqwiki-reformat-external-link-parenthesize-domain-name()
    "\nExample.\n
\[http://www.somesite.org/Keyboard_shortcuts_at_a_glance Some useful keyboard shortcuts\]\n
will be transformed to:\n
\[http://www.somesite.org/Keyboard_shortcuts_at_a_glance Some useful keyboard shortcuts\] \(''www.somesite.org''\)
"
    (interactive)
    (beginning-of-line)
    (query-replace-regexp " __parenthesize-domain-name__ \\|\
\\(^\\|[^[:cntrl:][]\\)\
\\(\\[[^[][^]]*\\|\\[\\)\
\\(https?\\|ftp\\)://\
\\([^]/[:space:]]+\\)\
\\([^][:space:]]*[[:blank:]]+[^][:cntrl:]]+\\]\\)\
\\([[:blank:]]*$\\|\\([[:blank:]]\\)?[[:blank:]]*\\([^]([:cntrl:][:blank:]]\\)\\)\
" "\\1\\2\\3://\\4\\5 (''\\4'')\\7\\8\\9" nil nil nil))
  (global-set-key [(control meta ?l) (?u)]
		  'my-lqwiki-reformat-external-link-parenthesize-domain-name))


(progn
  (defun my-lqwiki-reformat-external-link-bare-link-wikipedia()
    "\nFormats a bare url link to a wikipedia article. For instance\n
http://en.wikipedia.org/wiki/Uniform_Resource_Identifier\n
will be transformed to :\n
\[http://en.wikipedia.org/wiki/Uniform_Resource_Identifier Uniform_Resource_Identifier\] \(''en.wikipedia.org''\)\n
You can then use my-lqwiki-reformat-external-link-replace-underscores-with-spaces to remove underscores."
    (interactive)
    (beginning-of-line)
    (query-replace-regexp " __reformat-bare-link-wikipedia__ \\|\
\\(^\\|[^[:cntrl:][]\\)\
\\(http://\\(en.wikipedia.org\\)/wiki/\\)\
\\([^][:space:]]+\\)\
" "\\1[\\2\\4 \\4] (''\\3'')" nil nil nil))
  (global-set-key [(control meta ?l) (?W)]
		  'my-lqwiki-reformat-external-link-simple-title-encapsulation))


(progn
  (defun my-lqwiki-reformat-external-link-bare-link-title-encapsulation()
    "\nExample.\n
http://wiki.linuxquestions.org/wiki/Keyboard_shortcuts_at_a_glance -- Some useful keyboard shortcuts\n
will be transformed to:\n
\[http://wiki.linuxquestions.org/wiki/Keyboard_shortcuts_at_a_glance  Some useful keyboard shortcuts\] \(''wiki.linuxquestions.org''\)
\nThe transformation occurs in the absence of \"--\" too.
"
    (interactive)
    (beginning-of-line)
    (query-replace-regexp " __bare-link-title-encapsulation__ \\|\
\\(^\\|[^[:cntrl:][]\\)\
\\(https?\\|ftp\\)://\
\\([^]/[:space:]]+\\)\
\\([^][:space:]]*[[:blank:]]+\\)\
\-?-?-?[[:blank:]]*\\([^][:cntrl:]]+\\)[[:blank:]]*\
" "\\1[\\2://\\3\\4 \\5] (''\\3'')" nil nil nil))
  (global-set-key [(control meta ?l) (?s)]
		  'my-lqwiki-reformat-external-link-bare-link-title-encapsulation))


(progn
  (defun my-lqwiki-reformat-external-link-commented-footnote-style-link()
    "\nExample.\n
\[http://wiki.linuxquestions.org/wiki/Keyboard_shortcuts_at_a_glance\] -- Some useful keyboard shortcuts
\nwill be transformed to:\n
\[http://wiki.linuxquestions.org/wiki/Keyboard_shortcuts_at_a_glance Some useful keyboard shortcuts\] \(''wiki.linuxquestions.org''\)

\nThe transformation occurs in the absence of \"--\" too.
"
    (interactive)
    (beginning-of-line)
    (query-replace-regexp " __reformat-commented-footnote-style-link__ \\|\
\\(^\\|[^[:cntrl:][]\\)\\[\
\\(https?\\|ftp\\)://\
\\([^]/[:space:]]+\\)\
\\([^][:space:]]*\\)[[:blank:]]*\\]\
\[[:blank:]]*-?-?-?[[:blank:]]*\\([^([:space:]][^[:cntrl:]]*$\\)?\
" "\\1[\\2://\\3\\4 \\5] (''\\3'')" nil nil nil))
  (global-set-key [(control meta ?l) (?r)]
		  'my-lqwiki-reformat-external-link-commented-footnote-style-link))


(progn
  (defun my-lqwiki-reformat-external-link-bare-link-with-domain-name-only()
    "\nExample.\n
http://www.gnu.org
\nwill be transformed to:\n
\[http://www.gnu.org www.gnu.org\] \(''www.gnu.org''\)
"
    (interactive)
    (beginning-of-line)
    (query-replace-regexp " __reformat-bare-link-with-domain-name-only__ \\|\
\\(^\\|[^[:cntrl:][]\\)\
\\(https?\\|ftp\\)://\
\\([^]/[:space:]]+\\)\\(/?\\)[[:blank:]]*$\
" "\\1[\\2://\\3\\4 \\3] (''\\3'')" nil nil nil))
  (global-set-key [(control meta ?l) (?w)]
		  'my-lqwiki-reformat-external-link-bare-link-with-domain-name-only))


(progn
  (defun my-lqwiki-reformat-external-link-description-to-new-line()
    "\nExample.\n
\[http://en.wikipedia.org/wiki/Uniform_Resource_Identifier URI\] -- Wikipedia article about URI
\nwill be transformed to:\n
\[http://en.wikipedia.org/wiki/Uniform_Resource_Identifier URI\]
: Wikipedia article about URI
\nThe transformation occurs in the absence of \"--\" too.
"
    (interactive)
    (beginning-of-line)
    ;;emacs seems to need to have a starting "\" on each line...
    (query-replace-regexp " __description-to-new-line__ \\|\
\\([*#:]*\\)[[:blank:]]*\\[\
\\(https?\\|ftp\\)://\
\\([^]/[:space:]]+\\)\
\\([^][:space:]]*[[:blank:]]+[^][:cntrl:]]+\\)\\]\
\[[:blank:]]*-?-?-?[[:blank:]]*\\([^([:cntrl:][:blank:]][^[:cntrl:]]+\\)\
" "\\1[\\2://\\3\\4]
\\1: \\5" nil nil nil))
  (global-set-key [(control meta ?l) (?c)]
		  'my-lqwiki-reformat-external-link-description-to-new-line))


(progn
  (defun my-lqwiki-reformat-external-link-replace-underscores-with-spaces()
    "\nExample.\n
\[http://en.wikipedia.org/wiki/Uniform_Resource_Identifier Uniform_Resource_Identifier\] \(''en.wikipedia.org''\)
\nwill be transformed to:\n
\[http://en.wikipedia.org/wiki/Uniform_Resource_Identifier Uniform Resource Identifier\] \(''en.wikipedia.org''\)
"
    (interactive)
    (beginning-of-line)
    (while (search-forward-regexp "\
\\(^\\|[^[:cntrl:][]\\)\
\\(\\[[[:blank:]]*\\)\
\\(https?\\|ftp\\)://\
\\([^]/[:space:]]+\\)\
\\([^][:space:]]*[[:blank:]]+\\)\
\\(\\([^][:cntrl:]_]+_[^][:cntrl:]]+\\)\\]\\)\
" nil t nil)
      ;;query-replace-regexp is buggy and needs its matched string to be shorter than the region to operate on.
      ;;this is emacs 21.2-1woody3 -- bug is probably resolved by now :)
      (let ((m-b (match-beginning 6)) (m-e (match-end 6)) (m-s (match-string 7)))
	(query-replace-regexp " __replace-underscores-with-spaces__ \\|[^][]+"
			      (replace-regexp-in-string "_" " " m-s ) nil m-b m-e)
	))
    )
  (global-set-key [(control meta ?l) (?U)]
		  'my-lqwiki-reformat-external-link-replace-underscores-with-spaces))


(progn
  (defun my-lqwiki-reformat-external-link-all-formatting()
    (interactive)
    ;;(my-region-uuu-get-bounderies uuu_topl_mark)
    (save-window-excursion
      (save-excursion
	(save-restriction
	  ;;(narrow-to-region (marker-position uuu_regbeg) (marker-position uuu_regend))
	  ;;(my-dragndrop-clean)
	  (let ((uuu_regbeg (point-min-marker))(uuu_regend (point-max-marker)) )
	    (end-of-buffer)
	    (if (search-backward-regexp "= *external.*links *=" nil 't)
		(setq uuu_regbeg (point-marker)))
	    (narrow-to-region (marker-position uuu_regbeg) (marker-position uuu_regend))
	    (beginning-of-buffer)
	    ;;If i don't the recenter command, sometimes in case of big narrowed regions,
	    ;;query-replace-regexp might not make visible the underlined text to replace. Bug ??
	    (recenter)
	    (my-lqwiki-reformat-external-link-bare-link-wikipedia)
	    (beginning-of-buffer)
	    (my-lqwiki-reformat-external-link-bare-link-title-encapsulation)
	    (beginning-of-buffer)
	    (my-lqwiki-reformat-external-link-commented-footnote-style-link)
	    (beginning-of-buffer)
	    (my-lqwiki-reformat-external-link-description-to-new-line)
	    (beginning-of-buffer)
	    (my-lqwiki-reformat-external-link-bare-link-with-domain-name-only)
	    (beginning-of-buffer)
	    (my-lqwiki-reformat-external-link-parenthesize-domain-name)
	    (beginning-of-buffer)
	    (my-lqwiki-reformat-external-link-replace-underscores-with-spaces)
	    )))))
  (global-set-key [(control meta ?l) (?A)]
		  'my-lqwiki-reformat-external-link-all-formatting)
  )