Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 130 lines (99 sloc) 4.297 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
%D \module
%D [ file=t-comments,
%D version=2011.11.14,
%D title=\CONTEXT\ User Module,
%D subtitle=Multi line comments,
%D author=Hans Hagen and Aditya Mahajan,
%D date=\currentdate,
%D copyright=Hans Hagen and Aditya Mahajan,
%D email=adityam <at> ieee <dot> org,
%D license=Simplified BSD License]

\writestatus{loading}{Comments (ver: 2011.11.14)}

\startluacode
     thirddata = thirddata or { }
     local comments = { filters = { } }
     thirddata.comments = comments

     local textlineactions = resolvers.openers.helpers.textlineactions
     local sequencers = utilities.sequencers
     local constants = interfaces.constants

     local gsub, find, format = string.gsub, string.find, string.format

     local empty_line = "%%" -- FIXME Make catcode independent!

     function comments.install(name,settings)
         local settings = utilities.parsers.settings_to_hash(settings)
         local start = string.escapedpattern(settings[constants.start] or "")
         local stop = string.escapedpattern(settings[constants.stop] or "")
         local any = ".-"
         if start ~= "" and stop ~= "" then
            local strip_inline = function(s) return gsub(s, start .. any .. stop, "", 1) end
            local strip_from_start = function(s) return gsub(s, start .. any .. "$", "", 1) end
local strip_to_stop = function(s) return gsub(s, "^" .. any .. stop, "", 1) end
-- A simple state based parser for multi-line comments
local insideComment = false
local cleaned_line = ""
comments.filters[name] = function(s)
local currentfilter = comments.filters[name]
if s == "" then
return s
elseif insideComment then
if find(s,stop) then
insideComment = false
return cleaned_line .. currentfilter(strip_to_stop(s))
else
return empty_line
end
elseif find(s,start) then
if find(s, stop) then
return currentfilter(strip_inline(s))
else
insideComment = true
cleaned_line = strip_from_start(s)
return empty_line
end
else
return s
end
end
sequencers.appendaction(textlineactions,"after",format("thirddata.comments.filters.%s",name))
comments.disable(name)
end
end

function comments.enable(name)
sequencers.enableaction(textlineactions,format("thirddata.comments.filters.%s",name))
end

function comments.disable(name)
sequencers.disableaction(textlineactions,format("thirddata.comments.filters.%s",name))
end

\stopluacode

\unprotect

\unexpanded\def\installcomments{\dodoubleargument\doinstallcomments}

\def\doinstallcomments[#name][#settings]%
{\ctxlua{thirddata.comments.install(\!!bs#name\!!es,\!!bs\detokenize{#settings}\!!es)}}

\unexpanded\def\enablecomments[#name]%
{\ctxlua{thirddata.comments.enable (\!!bs#name\!!es)}}

\unexpanded\def\disablecomments[#name]%
{\ctxlua{thirddata.comments.disable(\!!bs#name\!!es)}}

\protect

\installcomments[C] [start=/*,stop=*/]

\continueifinputfile{t-comments.mkvi}

\starttext

\section{Multi line comments}

Unlike most programming languages, \TEX\ has no support for multi line comments,
which can extend over multiple lines, or limit a comment to part of a line. This
module provides such a support.

As an example, lets consider the following text.

\startbuffer[C]
\section{First /*actually was second*/ section}
Random text to demostrate /*really well*/ how the concept /* of multi line
comments works. This particular example uses C style multi-line comments,
but other styles also */ work.

A/* single line comment*/B

A/* multi line
comment
with line breaks*/B

\stopbuffer

\typebuffer[C]

Now, if we enable C-type multi-line comments, this text is typeset as

\startframedtext[width=broad]
\enablecomments[C] \getbuffer[C]
\stopframedtext


\stoptext
Something went wrong with that request. Please try again.