diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..9733b15 --- /dev/null +++ b/.gdbinit @@ -0,0 +1 @@ +set args -d sample.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2b0a80 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# temporary compile results +*.o + +# Binary excutions +mdp +mdp.exe + +# Other temporary files +.DS_Store +*~ +*.swp +*.sublime-workspace +*.out +tags diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..f21717a --- /dev/null +++ b/AUTHORS @@ -0,0 +1,14 @@ +Author: visit1985 + +Contributors: +Pyrohh +FreeBirdLjj +ungureanuvladvictor +lukaslueg +mattn +fredjean +hspak +dopsi +alx741 +mnalt + diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/CREDITS b/CREDITS new file mode 100644 index 0000000..b613273 --- /dev/null +++ b/CREDITS @@ -0,0 +1,8 @@ +Credits go to + +Joshua M. Clulow for his terminal-based presentation tool in node.js +https://github.com/jclulow/vtmc + +David Parsons for his inspiring Markdown implementation in C +https://github.com/Orc/discount + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9c63e0a --- /dev/null +++ b/Makefile @@ -0,0 +1,70 @@ +# +# Makefile +# Copyright (C) 2015 Michael Goehler +# +# This file is part of mdp. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +UNAME_S := $(shell uname -s 2>/dev/null || echo not) + +SOURCES = $(wildcard src/*.c) +OBJECTS = $(SOURCES:.c=.o) +TARGET = mdp +DESTDIR = +PREFIX = /usr/local + +CURSES = ncursesw +LDFLAGS ?= -s + +ifeq (Windows_NT,$(OS)) + ifeq (,$(findstring CYGWIN,$(UNAME_S))) + CURSES := pdcurses + endif +endif + +ifeq ($(UNAME_S),Darwin) + CURSES := ncurses + LDFLAGS := +endif + +ifeq ($(DEBUG),1) + LDFLAGS := +endif + +LDLIBS = -l$(CURSES) + +all: $(TARGET) + +$(TARGET): src + $(CC) $(OBJECTS) $(LDLIBS) $(LDFLAGS) -o $(TARGET) + +src: + $(MAKE) $(MFLAGS) -C src + +clean: + $(MAKE) -C src clean + $(RM) $(TARGET) + +install: + install -d $(DESTDIR)$(PREFIX)/bin + install -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(TARGET) + install -d $(DESTDIR)$(PREFIX)/share/man/man1 + install -m 644 mdp.1 $(DESTDIR)$(PREFIX)/share/man/man1/$(TARGET).1 + +uninstall: + $(RM) $(DESTDIR)$(PREFIX)/$(TARGET) + +.PHONY: all clean install src uninstall diff --git a/README.md b/README.md new file mode 100644 index 0000000..136ec44 --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ + +## mdp - A command-line based markdown presentation tool. + +![image](https://cloud.githubusercontent.com/assets/2237222/5810237/797c494c-a043-11e4-9dbd-959cab4055fa.gif) + +--- + +***How to get started:*** + +mdp needs the ncursesw headers to compile. +So make sure you have them installed: +- on Ubuntu/Debian you need `libncursesw5` and `libncursesw5-dev` +- on Cygwin you need `libncursesw10` and `libncurses-devel` + +Now download and install mdp: + + $ git clone https://github.com/visit1985/mdp.git + $ cd mdp + $ make + $ make install + $ mdp sample.md + +- On Arch, you can use the existing [AUR package](https://aur.archlinux.org/packages/mdp-git/). +- On Slackware, grab the SlackBuild here: (http://slackbuilds.org/apps/mdp/), or run `sbopkg -i mdp`. +- On FreeBSD, you can use the port [misc/mdp](http://www.freshports.org/misc/mdp). +- On OS-X, use the existing [Homebrew Formula](http://brewformulas.org/Mdp) by running `brew install mdp`. + +Most terminals support 256 colors only if the TERM variable is +set correctly. To enjoy mdp's color fading feature: + + $ export TERM=xterm-256color + +--- + +***How to use it:*** + +Horizontal rulers are used as slide separator. + +Supports basic markdown formating: + +- line wide markup + - headlines + - code + - quotes + - unordered list + +- in-line markup + - bold text + - underlined text + - code + +Supports headers prefixed by @ symbol. + +- first two header lines are displayed as title and author + in top and bottom bar + +Review sample.md for more details. + +--- + +***Controls:*** + +- h, j, k, l, Arrow keys, + Space, Enter, Backspace, + Page Up, Page Down - next/previous slide +- Home, g - go to first slide +- End, G - go to last slide +- 1-9 - go to slide n +- r - reload input file +- q - exit + + +--- + +***How to debug it:*** + +To make a debug version of `mdp`, just type: + + $ make DEBUG=1 + diff --git a/include/bitops.h b/include/bitops.h new file mode 100644 index 0000000..0d18aec --- /dev/null +++ b/include/bitops.h @@ -0,0 +1,43 @@ +#if !defined( BITOPS_H ) +#define BITOPS_H + +/* + * Macros to do bit operations on integer variables. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * macro: SET_BIT to set bit at pos of var to 1 + * macro: CLEAR_BIT to set bit at pos of var to 0 + * macro: TOGGLE_BIT to toggle bit at pos of var from 1 to 0 and vice versa + * macro: CHECK_BIT returns > 0 if bit at pos of var is set to 1 + * + * Example: + * int i = 0; + * i = SET_BIT(i, 2); + * if(CHECK_BIT(i, 2)) + * printf("bit 2 is set\n"); + * printf("value of i is %d\n", i); + * + */ + +#define SET_BIT(var, pos) ((var) |= (1<<(pos))) +#define CLEAR_BIT(var, pos) ((var) &= (~(1<<(pos)))) +#define TOGGLE_BIT(var, pos) ((var) ^= (1<<(pos))) +#define CHECK_BIT(var, pos) ((var) & (1<<(pos))) + +#endif // !defined( BITOPS_H ) diff --git a/include/common.h b/include/common.h new file mode 100644 index 0000000..5f22246 --- /dev/null +++ b/include/common.h @@ -0,0 +1,51 @@ +#if !defined( COMMON_H ) +#define COMMON_H + +/* + * Common macros. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * type: bool variable stores only true or false. + * + * macro: MAX returns the higher one of two input variables + * macro: MIN returns the lower one of two input variables + * + */ + +#if defined( __STDC__ ) // for standard C compiler +#if __STDC_VERSION__ >= 199901L // for C99 and later +#include +#else // __STDC_VERSION__ >= 199901L +#if !defined( bool ) +typedef enum { + false = 0, + true = 1 +} bool; +#endif // !defined( bool ) +#endif // __STDC_VERSION__ >= 199901L +#else // defined( __STDC__ ) +#define bool int +#define true 1 +#define false 0 +#endif // defined( __STDC__ ) + +#define MAX(a, b) ({ typeof(a) _a = a; typeof(b) _b = b; _a > _b? _a : _b; }) +#define MIN(a, b) ({ typeof(a) _a = a; typeof(b) _b = b; _a < _b? _a : _b; }) + +#endif // !defined( COMMON_H ) diff --git a/include/cstack.h b/include/cstack.h new file mode 100644 index 0000000..a876ddb --- /dev/null +++ b/include/cstack.h @@ -0,0 +1,62 @@ +#if !defined( CSTACK_H ) +#define CSTACK_H + +/* + * An implementation of a char stack in heap memory. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * struct: cstack_t which defines char stack type in heap memory + * + * function: cstack_init to intialize struct of type cstack_t + * function: cstack_t->push to add one char on top if the stack + * function: cstack_t->pop to remove the top char from the stack + * function: cstack_t->top to test if the top char is a given char + * function: cstack_t->empty to test if the stack is empty + * function: cstack_t->delete to free the allocated memory + * + * Example: + * cstack_t *p = cstack_init(); + * (p->push)(p, 'X'); + * printf("%c\n", (p->pop)(p)); + * (p->delete)(p); + * + */ + +#include "common.h" + +typedef struct _cstack_t { + wchar_t *content; + size_t alloc; + size_t size; + int head; + void (*push)(struct _cstack_t *self, wchar_t c); + wchar_t (*pop)(struct _cstack_t *self); + bool (*top)(struct _cstack_t *self, wchar_t c); + bool (*empty)(struct _cstack_t *self); + void (*delete)(struct _cstack_t *self); +} cstack_t; + +cstack_t *cstack_init(); +void cstack_push(cstack_t *self, wchar_t c); +wchar_t cstack_pop(cstack_t *self); +bool cstack_top(cstack_t *self, wchar_t c); +bool cstack_empty(cstack_t *self); +void cstack_delete(cstack_t *self); + +#endif // !defined( CSTACK_H ) diff --git a/include/cstring.h b/include/cstring.h new file mode 100644 index 0000000..506b52c --- /dev/null +++ b/include/cstring.h @@ -0,0 +1,62 @@ +#if !defined( CSTRING_H ) +#define CSTRING_H + +/* + * An implementation of expandable c strings in heap memory. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * struct: cstring_t which defines a expandable c string type in heap memory + * + * function: cstring_init to intialize struct of type cstring_t + * function: cstring_t->expand to add one character to the struct + * function: cstring_t->expand_arr to add a string to the struct + * function: cstring_t->strip to remove a substring + * function: cstring_t->reset to clear and reuse the struct + * function: cstring_t->delete to free the allocated memory + * + * Example: + * cstring_t *p = cstring_init(); + * (p->expand)(p, 'X'); + * (p->delete)(p); + * + */ + +// The amount of memory allocated from heap when string expansion hits the +// allocated memory limit +#define REALLOC_ADD 10 + +typedef struct _cstring_t { + wchar_t *value; + size_t size; + size_t alloc; + void (*expand)(struct _cstring_t *self, wchar_t x); + void (*expand_arr)(struct _cstring_t *self, wchar_t *x); + void (*strip)(struct _cstring_t *self, int pos, int len); + void (*reset)(struct _cstring_t *self); + void (*delete)(struct _cstring_t *self); +} cstring_t; + +cstring_t *cstring_init(); +void cstring_expand(cstring_t *self, wchar_t x); +void cstring_expand_arr(cstring_t *self, wchar_t *x); +void cstring_strip(cstring_t *self, int pos, int len); +void cstring_reset(cstring_t *self); +void cstring_delete(cstring_t *self); + +#endif // !defined( CSTRING_H ) diff --git a/include/main.h b/include/main.h new file mode 100644 index 0000000..8f3f706 --- /dev/null +++ b/include/main.h @@ -0,0 +1,30 @@ +#if !defined( MAIN_H ) +#define MAIN_H + +/* + * mdp -- A command-line based markdown presentation tool. + * Copyright (C) 2015 Michael Goehler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "parser.h" +#include "viewer.h" + +#define MDP_VER_MAJOR 1 +#define MDP_VER_MINOR 0 +#define MDP_VER_REVISION 3 + +#endif // !defined( MAIN_H ) diff --git a/include/markdown.h b/include/markdown.h new file mode 100644 index 0000000..3a85947 --- /dev/null +++ b/include/markdown.h @@ -0,0 +1,93 @@ +#if !defined( MARKDOWN_H ) +#define MARKDOWN_H + +/* + * An implementation of markdown objects. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * enum: line_bitmask which enumerates markdown formating bits + * + * struct: deck_t the root object representing a deck of slides + * struct: slide_t a linked list element of type slide contained in a deck + * struct: line_t a linked list element of type line contained in a slide + * + * function: new_deck to initialize a new deck + * function: new_slide to initialize a new linked list of type slide + * function: next_slide to extend a linked list of type slide by one element + * function: new_line to initialize a new linked list of type line + * function: next_line to extend a linked list of type line by one element + * function: free_line to free a line elements memory + * function: free_deck to free a deck's memory + * + */ + +#include "cstring.h" +#include "bitops.h" + +enum line_bitmask { + IS_H1, + IS_H1_ATX, + IS_H2, + IS_H2_ATX, + IS_QUOTE, + IS_CODE, + IS_TILDE_CODE, + IS_HR, + IS_UNORDERED_LIST_1, + IS_UNORDERED_LIST_2, + IS_UNORDERED_LIST_3, + IS_UNORDERED_LIST_EXT, + IS_CENTER, + IS_STOP, + IS_EMPTY +}; + +typedef struct _line_t { + cstring_t *text; + struct _line_t *prev; + struct _line_t *next; + int bits; + int length; + int offset; +} line_t; + +typedef struct _slide_t { + line_t *line; + struct _slide_t *prev; + struct _slide_t *next; + int lines; + int stop; +} slide_t; + +typedef struct _deck_t { + line_t *header; + slide_t *slide; + int slides; + int headers; +} deck_t; + +line_t *new_line(); +line_t *next_line(line_t *prev); +slide_t *new_slide(); +slide_t *next_slide(slide_t *prev); +deck_t *new_deck(); +void free_line(line_t *l); +void free_deck(deck_t *); + +#endif // !defined( MARKDOWN_H ) diff --git a/include/parser.h b/include/parser.h new file mode 100644 index 0000000..7a18377 --- /dev/null +++ b/include/parser.h @@ -0,0 +1,62 @@ +#if !defined( PARSER_H ) +#define PARSER_H + +/* + * Functions necessary to parse a file and transform its content into + * a deck of slides containing lines. All based on markdown formating + * rules. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * function: markdown_load is the main function which reads a file handle, + * and initializes deck, slides and lines + * function: markdown_analyse which is used to identify line wide formating + * rules in given line + * function: markdown_debug to print a report of the generated data structure + * function: adjust_line_length to calculate line length excluding markup + * function: is_utf8 detects multi-byte char + * function: length_utf8 calculates the amount of bytes used for a multi-byte + * char + * function: next_nonblank, next_blank, next_word to calculate string offset's + * + */ + +#include "common.h" +#include "markdown.h" +#include "cstack.h" + +#if defined( CYGWIN ) +#undef WEOF +#define WEOF (0xffff) +#endif // defined( CYGWIN ) + +#define EXPAND_TABS 4 +#define CODE_INDENT 4 +#define UNORDERED_LIST_MAX_LEVEL 3 + +deck_t *markdown_load(FILE *input); +int markdown_analyse(cstring_t *text, int prev); +void markdown_debug(deck_t *deck, int debug); +void adjust_line_length(line_t *line); +int next_nonblank(cstring_t *text, int i); +int prev_blank(cstring_t *text, int i); +int next_blank(cstring_t *text, int i); +int next_word(cstring_t *text, int i); +int next_nontilde(cstring_t *text, int i); + +#endif // !defined( PARSER_H ) diff --git a/include/url.h b/include/url.h new file mode 100644 index 0000000..254ca6d --- /dev/null +++ b/include/url.h @@ -0,0 +1,44 @@ +#if !defined( URL_H ) +#define URL_H + +/* + * An object to store all urls of a slide. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * function: url_init to intialize a new url object + */ + +typedef struct _url_t { + wchar_t *link_name; + wchar_t *target; + int x; + int y; + struct _url_t *next; +} url_t; + +void url_init(void); +int url_add(const wchar_t *link_name, int link_name_length, const wchar_t *target, int target_length, int x, int y); +wchar_t* url_get_target(int index); +wchar_t* url_get_name(int index); +int url_get_amount(void); +void url_purge(void); +void url_dump(void); +int url_count_inline(const wchar_t *line); +int url_len_inline(const wchar_t *value); + +#endif // !defined( URL_H ) diff --git a/include/viewer.h b/include/viewer.h new file mode 100644 index 0000000..eef6869 --- /dev/null +++ b/include/viewer.h @@ -0,0 +1,66 @@ +#if !defined( VIEWER_H ) +#define VIEWER_H + +/* + * Functions necessary to display a deck of slides in different color modes + * using ncurses. Only white, red, and blue are supported, as they can be + * faded in 256 color mode. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * function: ncurses_display initializes ncurses, defines colors, calculates + * window geometry and handles key strokes + * function: add_line detects inline markdown formating and prints line char + * by char + * function: fade_in, fade_out implementing color fading in 256 color mode + * function: int_length to calculate decimal length of slide count + * + */ + +#define _GNU_SOURCE // enable ncurses wchar support +#define _XOPEN_SOURCE_EXTENDED 1 // enable ncurses wchar support + +#if defined( WIN32 ) +#include +#else +#include +#endif + +#include "common.h" +#include "parser.h" +#include "cstack.h" +#include "url.h" + +#define CP_WHITE 1 // 255 +#define CP_BLUE 2 // 123 +#define CP_RED 3 // 213 +#define CP_YELLOW 4 // 208 +#define CP_BLACK 5 // CP_WHITE with foreground and background swapped + +#define FADE_DELAY 15000 // micro seconds +#define GOTO_SLIDE_DELAY 5 // tenths of seconds + +int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum); +void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors); +void inline_display(WINDOW *window, const wchar_t *c, const int colors); +void fade_out(WINDOW *window, int trans, int colors, int invert); +void fade_in(WINDOW *window, int trans, int colors, int invert); +int int_length (int val); +int get_slide_number(char init); + +#endif // !defined( VIEWER_H ) diff --git a/long-lines.md b/long-lines.md new file mode 100644 index 0000000..ea4c6e9 --- /dev/null +++ b/long-lines.md @@ -0,0 +1,9 @@ +%title: The Road Not Taken +%author: Robert Frost + +Two roads diverged in a yellow wood, and sorry I could not travel both. And be one traveler, long I stood, and looked down one as far as I could. To where it bent in the undergrowth. + +Then took the other, as just as fair, and having perhaps the better claim. Because it was grassy and wanted wear, though as for that the passing there Had worn them really about the same. And both that morning equally lay, in leaves no step had trodden black. + +Oh, I kept the first for another day! Yet knowing how way leads on to way. I doubted if I should ever come back, I shall be telling this with a sigh. Somewhere ages and ages hence: Two roads diverged in a wood, and I. I took the one less traveled by, and that has made all the difference. + diff --git a/mdp.1 b/mdp.1 new file mode 100644 index 0000000..9361e4f --- /dev/null +++ b/mdp.1 @@ -0,0 +1,143 @@ +.\" This is the groff documentation source for MDP +.\" +.\" Preview with: groff -man -Tascii mdp.1 +.\" or: man -l mdp.1 +.\" +. +. +.TH MDP 1 "2015-04-10" "User Commands" +.SH NAME +mdp \- A command-line based +markdown presentation tool +.SH SYNOPSIS +.B mdp +.RI [ OPTION ].\|.\|.\| +.RI [ FILE ] +. +.SH DESCRIPTION +.B mdp +is a command-line program that allows you to make elegant presentations from +.B markdown formatted +.IR FILE s. +.PP +It is as easy as write your presentation content in the text editor of your +preference and launch the presentation from the command-line. +. +.SH OPTIONS +.SS "Input Control" +.TP +.IR FILE +The input file from which the presentation is read. If no file is specified, +or if the file name is +.BR \- "," +the presentation is read from standard input. +.SS "Output Control" +.TP +.BR \-f ", " \-\^\-nofade +Disable color fading in 256 color mode. +.TP +.BR \-i ", " \-\^\-invert +Swap black and white color. +.TP +.BR \-t ", " \-\^\-notrans +Disable transparency in transparent terminal. +. +.SS "Miscellaneous Options" +.TP +.BR \-h ", " \-\^\-help +Display usage message and exit. +.TP +.BR \-d ", " \-\^\-debug +Enable debug messages on STDERR. Add multiple times to increases debug level. +. +.SH MARKDOWN FORMATTING +For a complete list of supported markups, refer the sample presentation +(sample.md) provided alongside +.BR mdp ,\| +or online available at +.IR https://github.com/visit1985/mdp . +.SS "Slides" +The input +.IR FILE +is split into multiple slides by horizontal rules. Each consisting of at least +3 +.B \-\-\- +or +.B *** +characters on a single line. +.B This line must be prefixed by an completely empty line. +It can also contain spaces but no other characters. +.PP +If any slide is too large to fit into your current screen, an error message +will be displayed at the moment the presentation is launched. +. +.SS "Line-by-Line Mode" +.SS "Block-by-Block Mode" +A single +.BR "
" ", " "
" " or " "^" +on an otherwise empty line signals +.B mdp +to stop output of the current slide (stop point) and wait for a key-press by +the user. +.PP +This enables the user to display bullet points or list items one by one +(line by line) or block by block. +. +.SS "Headers" +.B mdp +supports header lines in the format of +.BR @ "[DESCRIPTION] " [VALUE] +The first two header lines are displayed as title and author in top and +bottom bar. +.PP +Headers are only recognized at the top of the input +.IR FILE . +. +.SS "Line spanning markup" +Supported are headlines, code blocks, quotes and unordered lists. +. +.SS "In-line markup" +As well as bold text, underlined text and in-line code. +. +.SH COLOR SUPPORT +Most terminals are able to display 256 colors these days. But some of them +enable only 16 colors by default. To enjoy +.BR mdp "'s" +full capabilities, these terminals need to be signaled to enable 256 color +mode. This is usually done by setting the TERM environment variable. +.PP +.BR "export TERM=xterm-256color" +. +.SH KEYBOARD CONTROLS +.TP +.BR "h, j, k, l, Arrow keys, Space, Enter, Backspace, Page Up, Page Down" +Display next/previous slide or continue after a stop point. +.TP +.BR "g, Home" +Jump to first slide. +.TP +.BR "G, End" +Jump to last slide. +.TP +.BR "1..N" +Jump to +.BR N "th" +slide. +.TP +.BR "r" +Reload the input +.IR FILE .\| +This key is disabled if input was read from standard input. +.TP +.BR "q" +Exit +.BR mdp "." +. +.SH AUTHOR +Written by Michael Goehler and others, see +.IR https://github.com/visit1985/mdp/blob/master/AUTHORS "." +.SH COPYRIGHT +Copyright (C) 2015 Michael Goehler +.PP +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/mdp.sublime-project b/mdp.sublime-project new file mode 100644 index 0000000..a2a86d0 --- /dev/null +++ b/mdp.sublime-project @@ -0,0 +1,35 @@ +{ + "settings": + { + "tab_size": 4, + "translate_tabs_to_spaces": true, + "use_tab_stops": false + } + ,"folders": + [ + { + "follow_symlinks": true, + "path": ".", + "file_exclude_patterns": [".*", "mdp", "mdp.sublime-project", "mdp.sublime-workspace"] + } + ] + ,"build_systems": + [ + { + "name": "mdp", + "env": { + "DEBUG": "1" + }, + "cmd": ["make"], + "working_dir": "${project_path:${folder}}", + + "variants": + [ + { + "name": "Clean", + "cmd": ["make", "clean"] + } + ] + } + ] +} diff --git a/sample.md b/sample.md new file mode 100644 index 0000000..6e70ab8 --- /dev/null +++ b/sample.md @@ -0,0 +1,300 @@ +%title: mdp - Sample Presentation +%author: visit1985 +%date: 2014-09-22 + +-> mdp <- +========= + +-> A command-line based markdown presentation tool. <- + +_Basic controls:_ + +next slide *Enter*, *Space*, *Page Down*, *j*, *l*, + *Down Arrow*, *Right Arrow* + +previous slide *Backspace*, *Page Up*, *h*, *k*, + *Up Arrow*, *Left Arrow* + +quit *q* +reload *r* +slide N *1..9* +first slide *Home*, *g* +last slide *End*, *G* + +------------------------------------------------- + +-> # Supported markdown formatting <- + +The input file is split into multiple slides by +horizontal rules (hr). A hr consisting of at +least 3 *\** or *-*. It can also contain spaces but +no other characters. + +Each of these represents the start of a new slide. + +\* \* \* +\--- +\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* +\- - - + +------------------------------------------------- + +-> # Supported markdown formatting <- + +First-level headers can be prefixed by single *#* +or underlined by *===*. + +\# first-level + +becomes + +# first-level + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Second-level headers can be prefixed by *##* or +underlined by *---*. + +second-level +\------------ + +becomes + +second-level +------------ + + +------------------------------------------------- + +-> # Supported markdown formatting's <- + +Inline codes are surrounded with backticks. + +C program starts with \`main()\`. + +becomes + +C program starts with `main()`. + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Code blocks are automatically detected by 4 +spaces at the beginning of a line. + +Tabs are automatically expanded to 4 spaces +while parsing the input. + +\ int main(int argc, char \*argv[]) { +\ printf("%s\\n", "Hello world!"); +\ } + +becomes + + int main(int argc, char *argv[]) { + printf("%s\n", "Hello world!"); + } + +------------------------------------------------- + +-> # Supported markdown formatting <- + +You can also use [pandoc](http://pandoc.org/demo/example9/pandocs-markdown.html)'s fenced code block extension. +Use at least three ~ chars to open and at least as many or +more ~ for closing. + +\~~~ {.numberLines} +\int main(int argc, char \*argv[]) { +\ printf("%s\\n", "Hello world!"); +\} +\~~~~~~~~~~~~~~~~~~ + +becomes + +~~~ {.numberLines} +int main(int argc, char \*argv[]) { + printf("%s\\n", "Hello world!"); +} +~~~~~~~~~~~~~~~~~~ + +Pandoc attributes (like ".numberlines" etc.) will be ignored + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Quotes are auto-detected by preceding *>*. + +Multiple *>* are interpreted as nested quotes. + +\> quote +\>> nested quote 1 +\> > nested quote 2 + +becomes + +> quote +>> nested quote 1 +> > nested quote 2 + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Inline highlighting is supported as followed: + +\- *\** colors text as red +\- *\_* underlines text + +\_some\_ \*highlighted\* \_\*text\*\_ + +becomes + +_some_ *highlighted* _*text*_ + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Backslashes force special markdown characters +like *\**, *\_*, *#* and *>* to be printed as normal +characters. + +\\\*special\\\* + +becomes + +\*special\* + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Leading *\** or *-* indicate lists. + +list +\* major +\ - minor +\ - \*important\* +\ detail +\ - minor + +becomes + +list +* major + - minor + - *important* + detail + - minor + +------------------------------------------------- + +-> # Supported markdown formatting <- + +A single *\* or *^* in a line indicates mdp +to stop the output on that position. + +This can be used to show bullet points +line by line. + +*\* is also not displayed in HTML converted +output. + +Agenda +
+* major +
+ * minor +
+* major + ^ + * minor + ^ + * detail + +------------------------------------------------- + +-> # Supported markdown formatting <- + +Leading *->* indicates centering. + +\-> # test <- +\-> ## test <- +\-> test +\-> \_\*test\*\_ <- + +becomes + +-> # test <- +-> ## test <- +-> test +-> _*test*_ <- + +------------------------------------------------- + +-> # Supported markdown formatting <- + +URL in pandoc style are supported: + +\[Google](http://www.google.com/) + +becomes + +[Google](http://www.google.com/) + +------------------------------------------------- + +-> ## More information about markdown <- + +can be found in the [markdown documentation](http://daringfireball.net/projects/markdown/). + +------------------------------------------------- + +-> # Support for UTF-8 special characters <- + +Here are some examples. + +ae = ä, oe = ö, ue = ü, ss = ß +upsilon = Ʊ, phi = ɸ + +▛▀▀▀▀▀▀▀▀▀▜ +▌rectangle▐ +▙▄▄▄▄▄▄▄▄▄▟ + + +------------------------------------------------- + +-> # Suspend your presentation for hands-on examples <- + +Use *Ctrl + z* to suspend the presentation. + +Use *fg* to resume it. + +------------------------------------------------- + +-> # Convert your presentation to PDF <- + +To publish your presentation later on, you may +want to convert it to PDF. + +This can be achieved by two additional tools: + +\- *markdown* to convert to HTML +\- *wkhtmltopdf* to convert from HTML to PDF + +After installing them, you can simply type: + + $ markdown sample.md | wkhtmltopdf - sample.pdf + +------------------------------------------------- + +-> ## Last words <- + +I hope you like *mdp*. + +If you observe strange behavior, feel free to +open an issue on [GitHub](https://github.com/visit1985/mdp). diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..70b5af5 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,52 @@ +# +# Makefile +# Copyright (C) 2015 Michael Goehler +# +# This file is part of mdp. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +UNAME_S := $(shell uname -s 2>/dev/null || echo not) + +SOURCES = $(wildcard *.c) +OBJECTS = $(SOURCES:.c=.o) +CFLAGS ?= -O3 +CFLAGS += -Wall +CPPFLAGS += -I../include + +ifeq ($(DEBUG),1) + CFLAGS := -O0 -Wall -g +endif + +ifeq ($(OS),Windows_NT) + ifeq (,$(findstring CYGWIN,$(UNAME_S))) + CPPFLAGS += -DWIN32 + else + CPPFLAGS += -DCYGWIN + endif +endif + +ifeq ($(UNAME_S),Linux) + LSB_RELEASE := $(shell lsb_release -si 2>/dev/null || echo not) + ifneq ($(filter $(LSB_RELEASE),Debian Ubuntu LinuxMint CrunchBang),) + CPPFLAGS += -I/usr/include/ncursesw + endif +endif + +all: $(OBJECTS) + +clean: + $(RM) $(OBJECTS) + diff --git a/src/cstack.c b/src/cstack.c new file mode 100644 index 0000000..610483e --- /dev/null +++ b/src/cstack.c @@ -0,0 +1,74 @@ +/* + * An implementation of a char stack in heap memory. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include // fprintf +#include // malloc, realloc + +#include "cstack.h" + +cstack_t *cstack_init() { + cstack_t *stack = NULL; + if((stack = malloc(sizeof(cstack_t))) != NULL) { + stack->content = NULL; + stack->alloc = stack->size = 0; + stack->head = -1; + stack->push = cstack_push; + stack->pop = cstack_pop; + stack->top = cstack_top; + stack->empty = cstack_empty; + stack->delete = cstack_delete; + } else { + fprintf(stderr, "%s\n", "cstack_init() failed to allocate memory."); + exit(EXIT_FAILURE); + } + return stack; +} + +void cstack_push(cstack_t *self, wchar_t c) { + if(self->size + sizeof(c) > self->alloc) { + self->alloc += (sizeof(wchar_t)); + if((self->content = realloc(self->content, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstack_push() failed to reallocate memory."); + exit(EXIT_FAILURE); + } + } + self->content[++self->head] = c; + self->size += (sizeof(wchar_t)); +} + +wchar_t cstack_pop(cstack_t *self) { + self->size -= (sizeof(wchar_t)); + return self->content[self->head--]; +} + +bool cstack_top(cstack_t *self, wchar_t c) { + return self->head >= 0 && self->content[self->head] == c; +} + +bool cstack_empty(cstack_t *self) { + return self->head == -1; +} + +void cstack_delete(cstack_t *self) { + free(self->content); + free(self); +} diff --git a/src/cstring.c b/src/cstring.c new file mode 100644 index 0000000..7184d2f --- /dev/null +++ b/src/cstring.c @@ -0,0 +1,92 @@ +/* + * An implementation of expandable c strings in heap memory. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include // wcslen, wcscat, wmemmove +#include // fprintf +#include // malloc, realloc + +#include "cstring.h" + +cstring_t *cstring_init() { + cstring_t *x = NULL; + if((x = malloc(sizeof(cstring_t))) != NULL) { + x->value = NULL; + x->size = x->alloc = 0; + x->expand = cstring_expand; + x->expand_arr = cstring_expand_arr; + x->strip = cstring_strip; + x->reset = cstring_reset; + x->delete = cstring_delete; + } else { + fprintf(stderr, "%s\n", "cstring_init() failed to allocate memory."); + exit(EXIT_FAILURE); + } + return x; +} + +void cstring_expand(cstring_t *self, wchar_t x) { + if((self->size + 2) * sizeof(wchar_t) > self->alloc) { + self->alloc += (REALLOC_ADD * sizeof(wchar_t)); + if((self->value = realloc(self->value, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstring_expand() failed to reallocate memory."); + exit(EXIT_FAILURE); + } + } + self->value[self->size] = x; + self->value[self->size+1] = L'\0'; + self->size = wcslen(self->value); +} + +void cstring_expand_arr(cstring_t *self, wchar_t *x) { + if((self->size + wcslen(x) + 1) * sizeof(wchar_t) > self->alloc) { + self->alloc = ((self->size + wcslen(x) + 1) * sizeof(wchar_t)); + if((self->value = realloc(self->value, self->alloc)) == NULL) { + fprintf(stderr, "%s\n", "cstring_expand_arr() failed to reallocate memory."); + exit(EXIT_FAILURE); + } + } + self->value = wcscat(self->value, x); + self->size = wcslen(self->value); + self->value[self->size+1] = L'\0'; +} + +void cstring_strip(cstring_t *self, int pos, int len) { + if(pos + len >= self->size) { + if(pos <= self->size) { + self->value[pos] = L'\0'; + self->size = pos; + } + return; + } + wmemmove(&self->value[pos], &self->value[pos+len], self->size - pos - len+1); + self->size -= len; +} + +void cstring_reset(cstring_t *self) { + free(self->value); + self->value = NULL; + self->size = self->alloc = 0; +} + +void cstring_delete(cstring_t *self) { + free(self->value); + free(self); +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..00ef6d8 --- /dev/null +++ b/src/main.c @@ -0,0 +1,167 @@ +/* + * mdp -- A command-line based markdown presentation tool. + * Copyright (C) 2015 Michael Goehler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include // setlocale +#include +#include +#include + +#include "main.h" + +void usage() { + fprintf(stderr, "%s", "Usage: mdp [OPTION]... [FILE]\n"); + fprintf(stderr, "%s", "A command-line based markdown presentation tool.\n\n"); + fprintf(stderr, "%s", " -d, --debug enable debug messages on STDERR\n"); + fprintf(stderr, "%s", " add it multiple times to increases debug level\n"); + fprintf(stderr, "%s", " -f, --nofade disable color fading in 256 color mode\n"); + fprintf(stderr, "%s", " -h, --help display this help and exit\n"); + fprintf(stderr, "%s", " -i, --invert swap black and white color\n"); + fprintf(stderr, "%s", " -t, --notrans disable transparency in transparent terminal\n"); + fprintf(stderr, "%s", " -s, --noslidenum do not show slide number at the bottom\n"); + fprintf(stderr, "%s", " -v, --version display the version number and license\n"); + fprintf(stderr, "%s", " -x, --noslidemax show slide number, but not total number of slides\n"); + fprintf(stderr, "%s", "\nWith no FILE, or when FILE is -, read standard input.\n\n"); + exit(EXIT_FAILURE); +} + +void version() { + printf("mdp %d.%d.%d\n", MDP_VER_MAJOR, MDP_VER_MINOR, MDP_VER_REVISION); + printf("Copyright (C) 2015 Michael Goehler\n"); + printf("License GPLv3+: GNU GPL version 3 or later .\n"); + printf("This is free software: you are free to change and redistribute it.\n"); + printf("There is NO WARRANTY, to the extent permitted by law.\n"); + printf("\nWritten by Michael Goehler and others, see .\n"); + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) { + int notrans = 0; // disable transparency + int nofade = 0; // disable fading + int invert = 0; // invert color (black on white) + int reload = 0; // reload page N (0 means no reload) + int noreload = 1; // reload disabled until we know input is a file + int slidenum = 2; // 0:don't show; 1:show #; 2:show #/# + + // define command-line options + struct option longopts[] = { + { "debug", no_argument, 0, 'd' }, + { "nofade", no_argument, 0, 'f' }, + { "help", no_argument, 0, 'h' }, + { "invert", no_argument, 0, 'i' }, + { "notrans", no_argument, 0, 't' }, + { "version", no_argument, 0, 'v' }, + { "noslidenum", no_argument, 0, 's' }, + { "noslidemax", no_argument, 0, 'x' }, + { 0, 0, 0, 0 } + }; + + // parse command-line options + int opt, debug = 0; + while ((opt = getopt_long(argc, argv, ":dfhitvsx", longopts, NULL)) != -1) { + switch(opt) { + case 'd': debug += 1; break; + case 'f': nofade = 1; break; + case 'h': usage(); break; + case 'i': invert = 1; break; + case 't': notrans = 1; break; + case 'v': version(); break; + case 's': slidenum = 0; break; + case 'x': slidenum = 1; break; + case ':': fprintf(stderr, "%s: '%c' requires an argument\n", argv[0], optopt); usage(); break; + case '?': + default : fprintf(stderr, "%s: option '%c' is invalid\n", argv[0], optopt); usage(); break; + } + } + + // set locale to that of the environment, so that ncurses properly renders + // UTF-8 characters if the system supports it + setlocale(LC_CTYPE, ""); + + // open file or set input to STDIN + char *file = NULL; + FILE *input; + if (optind < argc) { + do { + file = argv[optind]; + } while(++optind < argc); + + if(!strcmp(file, "-")) { + input = stdin; + } else { + input = fopen(file,"r"); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); + exit(EXIT_FAILURE); + } + // enable reload because input is a file + noreload = 0; + } + } else { + input = stdin; + } + + // reload loop + do { + + // reopen input file on reload + if(noreload == 0 && reload > 0) { + if(file) { + input = fopen(file,"r"); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], file, strerror(errno)); + exit(EXIT_FAILURE); + } + } else { + fprintf(stderr, "%s: %s\n", argv[0], "no input file"); + exit(EXIT_FAILURE); + } + } + + // load deck object from input + deck_t *deck; + deck = markdown_load(input); + + // close file + fclose(input); + + // replace stdin with current tty if input was a pipe + // if input was a pipe reload is disabled, so we simply check that + if(noreload == 1) { + input = freopen("/dev/tty", "rw", stdin); + if(!input) { + fprintf(stderr, "%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno)); + exit(EXIT_FAILURE); + } + } + + if(debug > 0) { + markdown_debug(deck, debug); + } + + reload = ncurses_display(deck, notrans, nofade, invert, reload, noreload, slidenum); + + free_deck(deck); + + // reload if supported and requested + } while(noreload == 0 && reload > 0); + + return EXIT_SUCCESS; +} diff --git a/src/markdown.c b/src/markdown.c new file mode 100644 index 0000000..32e8c31 --- /dev/null +++ b/src/markdown.c @@ -0,0 +1,89 @@ +/* + * An implementation of markdown objects. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include + +#include "markdown.h" + +line_t *new_line() { + line_t *x = malloc(sizeof(line_t)); + x->text = NULL; + x->prev = x->next = NULL; + x->bits = x->length = x->offset = 0; + return x; +} + +line_t *next_line(line_t *prev) { + line_t *x = new_line(); + x->prev = prev; + prev->next = x; + return x; +} + +slide_t *new_slide() { + slide_t *x = malloc(sizeof(slide_t)); + x->line = NULL; + x->prev = x->next = NULL; + x->lines = x->stop = 0; + return x; +} + +slide_t *next_slide(slide_t *prev) { + slide_t *x = new_slide(); + x->prev = prev; + prev->next = x; + return x; +} + +deck_t *new_deck() { + deck_t *x = malloc(sizeof(deck_t)); + x->header = NULL; + x->slide = new_slide(); + x->slides = x->headers = 0; + return x; +} + +void free_line(line_t *line) { + line_t *next; + while (line) { + next = line->next; + if(line->text) + (line->text->delete)(line->text); + free(line); + line = next; + } +} + +void free_deck(deck_t *deck) { + slide_t *slide, *next; + if (deck == NULL) + return; + slide = deck->slide; + while (slide) { + free_line(slide->line); + next = slide->next; + free(slide); + slide = next; + } + free_line(deck->header); + free(deck); +} diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..f0d4ed3 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,675 @@ +/* + * Functions necessary to parse a file and transform its content into + * a deck of slides containing lines. All based on markdown formating + * rules. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "parser.h" + +deck_t *markdown_load(FILE *input) { + + wchar_t c = L'\0'; // char + int i = 0; // increment + int hc = 0; // header count + int lc = 0; // line count + int sc = 1; // slide count + int bits = 0; // markdown bits + int prev = 0; // markdown bits of previous line + + deck_t *deck = new_deck(); + slide_t *slide = deck->slide; + line_t *line = NULL; + line_t *tmp = NULL; + cstring_t *text = cstring_init(); + + // initialize bits as empty line + SET_BIT(bits, IS_EMPTY); + + while ((c = fgetwc(input)) != WEOF) { + if (ferror(input)) { + fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + if(c == L'\n') { + + // markdown analyse + prev = bits; + bits = markdown_analyse(text, prev); + + // if first line in file is markdown hr + if(!line && CHECK_BIT(bits, IS_HR)) { + + // clear text + (text->reset)(text); + + } else if(line && CHECK_BIT(bits, IS_STOP)) { + + // set stop bit on last line + SET_BIT(line->bits, IS_STOP); + + // clear text + (text->reset)(text); + + // if text is markdown hr + } else if(CHECK_BIT(bits, IS_HR) && + CHECK_BIT(line->bits, IS_EMPTY)) { + + slide->lines = lc; + + // clear text + (text->reset)(text); + + // create next slide + slide = next_slide(slide); + sc++; + + } else if(CHECK_BIT(bits, IS_TILDE_CODE) && CHECK_BIT(bits, IS_EMPTY)) { + // remove tilde code markers + (text->reset)(text); + + } else { + + // if slide ! has line + if(!slide->line || !line) { + + // create new line + line = new_line(); + slide->line = line; + lc = 1; + + } else { + + // create next line + line = next_line(line); + lc++; + + } + + // add text to line + line->text = text; + + // add bits to line + line->bits = bits; + + // calc offset + line->offset = next_nonblank(text, 0); + + // adjust line length dynamicaly - excluding markup + if(line->text->value) + adjust_line_length(line); + + // new text + text = cstring_init(); + } + + } else if(c == L'\t') { + + // expand tab to spaces + for (i = 0; i < EXPAND_TABS; i++) { + (text->expand)(text, L' '); + } + + } else if(c == L'\\') { + + // add char to line + (text->expand)(text, c); + + // if !IS_CODE add next char to line + // and do not increase line count + if(next_nonblank(text, 0) < CODE_INDENT) { + + c = fgetwc(input); + (text->expand)(text, c); + } + + } else if(iswprint(c) || iswspace(c)) { + + // add char to line + (text->expand)(text, c); + } + } + (text->delete)(text); + + slide->lines = lc; + deck->slides = sc; + + // detect header + line = deck->slide->line; + if(line && line->text->size > 0 && line->text->value[0] == L'%') { + + // assign header to deck + deck->header = line; + + // find first non-header line + while(line && line->text->size > 0 && line->text->value[0] == L'%') { + hc++; + line = line->next; + } + + // only split header if any non-header line is found + if(line) { + + // split linked list + line->prev->next = NULL; + line->prev = NULL; + + // remove header lines from slide + deck->slide->line = line; + + // adjust counts + deck->headers += hc; + deck->slide->lines -= hc; + } else { + + // remove header from deck + deck->header = NULL; + } + } + + slide = deck->slide; + while(slide) { + line = slide->line; + + // ignore mdpress format attributes + if(line && + slide->lines > 1 && + !CHECK_BIT(line->bits, IS_EMPTY) && + line->text->value[line->offset] == L'=' && + line->text->value[line->offset + 1] == L' ') { + + // remove line from linked list + slide->line = line->next; + line->next->prev = NULL; + + // maintain loop condition + tmp = line; + line = line->next; + + // adjust line count + slide->lines -= 1; + + // delete line + (tmp->text->delete)(tmp->text); + free(tmp); + } + + while(line) { + // combine underlined H1/H2 in single line + if((CHECK_BIT(line->bits, IS_H1) || + CHECK_BIT(line->bits, IS_H2)) && + CHECK_BIT(line->bits, IS_EMPTY) && + line->prev && + !CHECK_BIT(line->prev->bits, IS_EMPTY)) { + + + // remove line from linked list + line->prev->next = line->next; + if(line->next) + line->next->prev = line->prev; + + // set bits on previous line + if(CHECK_BIT(line->bits, IS_H1)) { + SET_BIT(line->prev->bits, IS_H1); + } else { + SET_BIT(line->prev->bits, IS_H2); + } + + // adjust line count + slide->lines -= 1; + + // maintain loop condition + tmp = line; + line = line->prev; + + // delete line + (tmp->text->delete)(tmp->text); + free(tmp); + + // pass enclosing flag IS_UNORDERED_LIST_3 + // to nested levels for unordered lists + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) { + tmp = line->next; + line_t *list_last_level_3 = line; + + while(tmp && + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) { + if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) { + list_last_level_3 = tmp; + } + tmp = tmp->next; + } + + for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) { + SET_BIT(tmp->bits, IS_UNORDERED_LIST_3); + } + + // pass enclosing flag IS_UNORDERED_LIST_2 + // to nested levels for unordered lists + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) { + tmp = line->next; + line_t *list_last_level_2 = line; + + while(tmp && + (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) || + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) { + if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) { + list_last_level_2 = tmp; + } + tmp = tmp->next; + } + + for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) { + SET_BIT(tmp->bits, IS_UNORDERED_LIST_2); + } + + // pass enclosing flag IS_UNORDERED_LIST_1 + // to nested levels for unordered lists + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) { + tmp = line->next; + line_t *list_last_level_1 = line; + + while(tmp && + (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) || + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) || + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) { + if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) { + list_last_level_1 = tmp; + } + tmp = tmp->next; + } + + for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) { + SET_BIT(tmp->bits, IS_UNORDERED_LIST_1); + } + } + + line = line->next; + } + slide = slide->next; + } + + return deck; +} + +int markdown_analyse(cstring_t *text, int prev) { + + // static variables can not be redeclaired, but changed outside of a declaration + // the program remembers their value on every function calls + static int unordered_list_level = 0; + static int unordered_list_level_offset[] = {-1, -1, -1, -1}; + static int num_tilde_characters = 0; + + int i = 0; // increment + int bits = 0; // markdown bits + int offset = 0; // text offset + int eol = 0; // end of line + + int equals = 0, hashes = 0, + stars = 0, minus = 0, + spaces = 0, other = 0; // special character counts + + const int unordered_list_offset = unordered_list_level_offset[unordered_list_level]; + + // return IS_EMPTY on null pointers + if(!text || !text->value) { + SET_BIT(bits, IS_EMPTY); + return bits; + } + + // count leading spaces + offset = next_nonblank(text, 0); + + // IS_TILDE_CODE + if (wcsncmp(text->value, L"~~~", 3) == 0) { + int tildes_in_line = next_nontilde(text, 0); + if (tildes_in_line >= num_tilde_characters) { + if (num_tilde_characters > 0) { + num_tilde_characters = 0; + } else { + num_tilde_characters = tildes_in_line; + } + SET_BIT(bits, IS_EMPTY); + SET_BIT(bits, IS_TILDE_CODE); + return bits; + } + } + + if (num_tilde_characters > 0) { + SET_BIT(bits, IS_CODE); + SET_BIT(bits, IS_TILDE_CODE); + return bits; + } + + // IS_STOP + if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) && + (!wcsncmp(&text->value[offset], L"
", 4) || + !wcsncmp(&text->value[offset], L"
", 4) || + !wcsncmp(&text->value[offset], L"^", 1))) { + SET_BIT(bits, IS_STOP); + return bits; + } + + // strip trailing spaces + for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--); + + // IS_UNORDERED_LIST_# + if(text->size >= offset + 2 && + (text->value[offset] == L'*' || text->value[offset] == L'-') && + iswspace(text->value[offset + 1])) { + + // if different from last lines offset + if(offset != unordered_list_offset) { + + // test if offset matches a lower indent level + for(i = unordered_list_level; i >= 0; i--) { + if(unordered_list_level_offset[i] == offset) { + unordered_list_level = i; + break; + } + } + // if offset doesn't match any previously stored indent level + if(i != unordered_list_level) { + unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL); + // memorize the offset as next bigger indent level + unordered_list_level_offset[unordered_list_level] = offset; + } + } + + // if no previous indent level matches, this must be the first line of the list + if(unordered_list_level == 0) { + unordered_list_level = 1; + unordered_list_level_offset[1] = offset; + } + + switch(unordered_list_level) { + case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break; + case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break; + case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break; + default: break; + } + } + + if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_2) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) { + + // continue list if indent level is still the same as in previous line + if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) || + CHECK_BIT(prev, IS_UNORDERED_LIST_2) || + CHECK_BIT(prev, IS_UNORDERED_LIST_3)) && + offset >= unordered_list_offset) { + + switch(unordered_list_level) { + case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break; + case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break; + case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break; + default: break; + } + + // this line extends the previous list item + SET_BIT(bits, IS_UNORDERED_LIST_EXT); + + // or reset indent level + } else { + unordered_list_level = 0; + } + } + + if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_2) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) { + + // IS_CODE + if(offset >= CODE_INDENT && + (CHECK_BIT(prev, IS_EMPTY) || + CHECK_BIT(prev, IS_CODE) || + CHECK_BIT(prev, IS_STOP))) { + SET_BIT(bits, IS_CODE); + + } else { + + // IS_QUOTE + if(text->value[offset] == L'>') { + SET_BIT(bits, IS_QUOTE); + } + + // IS_CENTER + if(text->size >= offset + 3 && + text->value[offset] == L'-' && + text->value[offset + 1] == L'>' && + iswspace(text->value[offset + 2])) { + SET_BIT(bits, IS_CENTER); + + // remove start tag + (text->strip)(text, offset, 3); + eol -= 3; + + if(text->size >= offset + 3 && + text->value[eol - 1] == L'-' && + text->value[eol - 2] == L'<' && + iswspace(text->value[eol - 3])) { + + // remove end tags + (text->strip)(text, eol - 3, 3); + + // adjust end of line + for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--); + + } + } + + for(i = offset; i < eol; i++) { + + if(iswspace(text->value[i])) { + spaces++; + + } else { + switch(text->value[i]) { + case L'=': equals++; break; + case L'#': hashes++; break; + case L'*': stars++; break; + case L'-': minus++; break; + case L'\\': other++; i++; break; + default: other++; break; + } + } + } + + // IS_H1 + if(equals > 0 && + hashes + stars + minus + spaces + other == 0) { + SET_BIT(bits, IS_H1); + } + if(text->value[offset] == L'#' && + iswspace(text->value[offset+1])) { + SET_BIT(bits, IS_H1); + SET_BIT(bits, IS_H1_ATX); + } + + // IS_H2 + if(minus > 0 && + equals + hashes + stars + spaces + other == 0) { + SET_BIT(bits, IS_H2); + } + if(text->value[offset] == L'#' && + text->value[offset+1] == L'#' && + iswspace(text->value[offset+2])) { + SET_BIT(bits, IS_H2); + SET_BIT(bits, IS_H2_ATX); + } + + // IS_HR + if((minus >= 3 && equals + hashes + stars + other == 0) || + (stars >= 3 && equals + hashes + minus + other == 0)) { + + SET_BIT(bits, IS_HR); + } + + // IS_EMPTY + if(other == 0) { + SET_BIT(bits, IS_EMPTY); + } + } + } + + return bits; +} + +void markdown_debug(deck_t *deck, int debug) { + + int sc = 0; // slide count + int lc = 0; // line count + + int offset; + line_t *header; + + if(debug == 1) { + fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides); + + } else if(debug > 1) { + + // print header to STDERR + if(deck->header) { + header = deck->header; + while(header && + header->length > 0 && + header->text->value[0] == L'%') { + + // skip descriptor word (e.g. %title:) + offset = next_blank(header->text, 0) + 1; + + fwprintf(stderr, L"header: %S\n", &header->text->value[offset]); + header = header->next; + } + } + } + + slide_t *slide = deck->slide; + line_t *line; + + // print slide/line count to STDERR + while(slide) { + sc++; + + if(debug == 1) { + fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines); + + } else if(debug > 1) { + + // also print bits and line length + fwprintf(stderr, L" slide %i:\n", sc); + line = slide->line; + lc = 0; + while(line) { + lc++; + fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length); + line = line->next; + } + } + + slide = slide->next; + } +} + +void adjust_line_length(line_t *line) { + int l = 0; + const static wchar_t *special = L"\\*_`"; // list of interpreted chars + const wchar_t *c = &line->text->value[line->offset]; + cstack_t *stack = cstack_init(); + + // for each char in line + for(; *c; c++) { + // if char is in special char list + if(wcschr(special, *c)) { + + // closing special char (or second backslash) + if((stack->top)(stack, *c)) { + if(*c == L'\\') l++; + (stack->pop)(stack); + + // treat special as regular char + } else if((stack->top)(stack, L'\\')) { + l++; + (stack->pop)(stack); + + // opening special char + } else { + (stack->push)(stack, *c); + } + + } else { + // remove backslash from stack + if((stack->top)(stack, L'\\')) + (stack->pop)(stack); + l++; + } + } + + if(CHECK_BIT(line->bits, IS_H1_ATX)) + l -= 2; + if(CHECK_BIT(line->bits, IS_H2_ATX)) + l -= 3; + + line->length = l; + + (stack->delete)(stack); +} + +int next_nonblank(cstring_t *text, int i) { + while ((i < text->size) && iswspace((text->value)[i])) + i++; + + return i; +} + +int prev_blank(cstring_t *text, int i) { + while ((i > 0) && !iswspace((text->value)[i])) + i--; + + return i; +} + +int next_blank(cstring_t *text, int i) { + while ((i < text->size) && !iswspace((text->value)[i])) + i++; + + return i; +} + +int next_word(cstring_t *text, int i) { + return next_nonblank(text, next_blank(text, i)); +} + +int next_nontilde(cstring_t *text, int i) { + while ((i < text->size) && text->value[i] == L'~') + i++; + + return i; +} + diff --git a/src/url.c b/src/url.c new file mode 100644 index 0000000..18b7397 --- /dev/null +++ b/src/url.c @@ -0,0 +1,201 @@ +/* + * Functions necessary to handle pandoc URLs. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include +#include + +#include "url.h" + +static void url_del_elem(url_t *elem); +static void url_print(url_t *u); + +static url_t *list; +static int index_max; +static int init_ok; + +void url_init(void) { + list = NULL; + index_max = 0; + init_ok = 1; +} + +int url_add(const wchar_t *link_name, int link_name_length, const wchar_t *target, int target_length, int x, int y) { + if (!init_ok) return -1; + + url_t *tmp = NULL; + int i = 0; + + if (list) { + tmp = list; + while (tmp->next) { + tmp = tmp->next; + i++; + } + tmp->next = malloc(sizeof(url_t)); + assert(tmp->next); + tmp = tmp->next; + } else { + list = malloc(sizeof(url_t)); + tmp = list; + assert(tmp); + } + + tmp -> link_name = calloc(link_name_length+1, sizeof(wchar_t)); + assert(tmp->link_name); + wcsncpy(tmp->link_name, link_name, link_name_length); + tmp->link_name[link_name_length] = '\0'; + + tmp->target = calloc(target_length+1, sizeof(wchar_t)); + assert(tmp->target); + wcsncpy(tmp->target, target, target_length); + tmp->target[target_length] = '\0'; + + tmp->x = x; + tmp->y = y; + tmp->next = NULL; + + index_max++; + + return index_max-1; +} + +wchar_t * url_get_target(int index) { + if (!init_ok) return NULL; + + url_t *tmp = list; + + if (!tmp) return NULL; + + while (index > 0 && tmp && tmp->next) { + tmp = tmp->next; + index--; + } + + if (!index) { + return tmp->target; + } else return NULL; +} + +wchar_t * url_get_name(int index) { + url_t *tmp = list; + + while (index > 0 && tmp && tmp->next) { + tmp = tmp->next; + index --; + } + + if (!index) { + return tmp->link_name; + } else return NULL; +} + +void url_purge() { + url_del_elem(list); + list = NULL; + index_max = 0; + init_ok = 0; +} + +static void url_del_elem(url_t *elem) { + if (!elem) return; + + if (elem->next) { + url_del_elem(elem->next); + elem->next = NULL; + } + + if (elem->target) { + free(elem->target); + elem->target = NULL; + } + + if (elem->link_name) { + free(elem->link_name); + elem->link_name = NULL; + } + + free(elem); +} + +void url_dump(void) { + if (!list) return; + + url_t *tmp = list; + + while (tmp) { + url_print(tmp); + if (tmp->next) + tmp = tmp->next; + else break; + } +} + +static void url_print(url_t *u) { + printf("url_t @ %p\n", u); +} + +int url_get_amount(void) { + return index_max; +} + +int url_count_inline(const wchar_t *line) { + int count = 0; + const wchar_t *i = line; + + for (; *i; i++) { + if (*i == '\\') { + i++; + } else if ( *i == '[' && *(i+1) != ']') { + while (*i && *i != ']') i++; + i++; + if (*i == '(' && wcschr(i, ')')) { + count ++; + i = wcschr(i, ')'); + } + } + } + + return count; +} + +int url_len_inline(const wchar_t *value) { + int count = 0; + const wchar_t *i = value; + + for (; *i; i++) { + if (*i == '\\') { + i++; + } else if ( *i == '[' && *(i+1) != ']') { + while (*i && *i != ']') i++; + i++; + if (*i == '(' && wcschr(i, ')')) { + while (*i && *i != ')') { + count++; + i++; + } + } + } + } + + return count; +} diff --git a/src/viewer.c b/src/viewer.c new file mode 100644 index 0000000..0e8204f --- /dev/null +++ b/src/viewer.c @@ -0,0 +1,917 @@ +/* + * Functions necessary to display a deck of slides in different color modes + * using ncurses. Only white, red, and blue are supported, as they can be + * faded in 256 color mode. + * Copyright (C) 2015 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include // isalnum +#include // wcschr +#include // iswalnum +#include // strcpy +#include // usleep +#include "viewer.h" + +// color ramp for fading from black to color +static short white_ramp[24] = { 16, 232, 233, 234, 235, 236, + 237, 238, 239, 240, 241, 242, + 244, 245, 246, 247, 248, 249, + 250, 251, 252, 253, 254, 255 }; + +static short blue_ramp[24] = { 16, 17, 17, 18, 18, 19, + 19, 20, 20, 21, 27, 33, + 32, 39, 38, 45, 44, 44, + 81, 81, 51, 51, 123, 123 }; + +static short red_ramp[24] = { 16, 52, 52, 53, 53, 89, + 89, 90, 90, 126, 127, 127, + 163, 163, 164, 164, 200, 200, + 201, 201, 207, 207, 213, 213 }; + +// color ramp for fading from white to color +static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251, + 250, 249, 248, 247, 246, 245, + 243, 242, 241, 240, 239, 238, + 237, 236, 235, 234, 233, 232}; + +static short blue_ramp_invert[24] = { 15, 231, 231, 195, 195, 159, + 159, 123, 123, 87, 51, 44, + 45, 38, 39, 32, 33, 33, + 26, 26, 27, 27, 21, 21}; + +static short red_ramp_invert[24] = { 15, 231, 231, 224, 224, 225, + 225, 218, 218, 219, 212, 213, + 206, 207, 201, 200, 199, 199, + 198, 198, 197, 197, 196, 196}; + +int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum) { + + int c = 0; // char + int i = 0; // iterate + int l = 0; // line number + int lc = 0; // line count + int sc = 1; // slide count + int colors = 0; // amount of colors supported + int fade = 0; // disable color fading by default + int trans = -1; // enable transparency if term supports it + int max_lines = 0; // max lines per slide + int max_cols = 0; // max columns per line + int offset; // text offset + int stop = 0; // passed stop bits per slide + + // header line 1 is displayed at the top + int bar_top = (deck->headers > 0) ? 1 : 0; + // header line 2 is displayed at the bottom + // anyway we display the slide number at the bottom + int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0; + + slide_t *slide = deck->slide; + line_t *line; + + // init ncurses + initscr(); + + while(slide) { + lc = 0; + line = slide->line; + + while(line && line->text) { + + if (line->text->value) + lc += url_count_inline(line->text->value); + + if (line->text->value) + line->length -= url_len_inline(line->text->value); + + if(line->length > COLS) { + i = line->length; + offset = 0; + while(i > COLS) { + + i = prev_blank(line->text, offset + COLS) - offset; + + // single word is > COLS + if(!i) { + // calculate min_width + i = next_blank(line->text, offset + COLS) - offset; + + // disable ncurses + endwin(); + + // print error + fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i); + fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n"); + + // no reload + return 0; + } + + // set max_cols + max_cols = MAX(i, max_cols); + + // iterate to next line + offset = prev_blank(line->text, offset + COLS); + i = line->length - offset; + lc++; + } + // set max_cols one last time + max_cols = MAX(i, max_cols); + } else { + // set max_cols + max_cols = MAX(line->length, max_cols); + } + lc++; + line = line->next; + } + + max_lines = MAX(lc, max_lines); + + slide = slide->next; + } + + // not enough lines + if(max_lines + bar_top + bar_bottom > LINES) { + + // disable ncurses + endwin(); + + // print error + fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom); + fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n"); + + // no reload + return 0; + } + + // disable cursor + curs_set(0); + + // disable output of keyboard typing + noecho(); + + // make getch() process one char at a time + cbreak(); + + // enable arrow keys + keypad(stdscr,TRUE); + + // set colors + if(has_colors() == TRUE) { + start_color(); + use_default_colors(); + + // 256 color mode + if(COLORS == 256) { + + if(notrans) { + if(invert) { + trans = 15; // white in 256 color mode + } else { + trans = 16; // black in 256 color mode + } + } + + if(invert) { + init_pair(CP_WHITE, 232, trans); + init_pair(CP_BLUE, 21, trans); + init_pair(CP_RED, 196, trans); + init_pair(CP_BLACK, 15, 232); + } else { + init_pair(CP_WHITE, 255, trans); + init_pair(CP_BLUE, 123, trans); + init_pair(CP_RED, 213, trans); + init_pair(CP_BLACK, 16, 255); + } + init_pair(CP_YELLOW, 208, trans); + + // enable color fading + if(!nofade) + fade = true; + + // 8 color mode + } else { + + if(notrans) { + if(invert) { + trans = 7; // white in 8 color mode + } else { + trans = 0; // black in 8 color mode + } + } + + if(invert) { + init_pair(CP_WHITE, 0, trans); + init_pair(CP_BLACK, 7, 0); + } else { + init_pair(CP_WHITE, 7, trans); + init_pair(CP_BLACK, 0, 7); + } + init_pair(CP_BLUE, 4, trans); + init_pair(CP_RED, 1, trans); + init_pair(CP_YELLOW, 3, trans); + } + + colors = 1; + } + + // set background color of main window + if(colors) + wbkgd(stdscr, COLOR_PAIR(CP_YELLOW)); + + // setup main window + WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0); + if(colors) + wbkgd(content, COLOR_PAIR(CP_WHITE)); + + slide = deck->slide; + + // find slide to reload + while(reload > 1 && reload <= deck->slides) { + slide = slide->next; + sc++; + reload--; + } + + // reset reload indicator + reload = 0; + + while(slide) { + + url_init(); + + // clear windows + werase(content); + werase(stdscr); + + // always resize window in case terminal geometry has changed + wresize(content, LINES - bar_top - bar_bottom, COLS); + + // setup header + if(bar_top) { + line = deck->header; + offset = next_blank(line->text, 0) + 1; + // add text to header + mvwaddwstr(stdscr, + 0, (COLS - line->length + offset) / 2, + &line->text->value[offset]); + } + + // setup footer + if(deck->headers > 1) { + line = deck->header->next; + offset = next_blank(line->text, 0) + 1; + switch(slidenum) { + case 0: // add text to center footer + mvwaddwstr(stdscr, + LINES - 1, (COLS - line->length + offset) / 2, + &line->text->value[offset]); + break; + case 1: + case 2: // add text to left footer + mvwaddwstr(stdscr, + LINES - 1, 3, + &line->text->value[offset]); + break; + } + } + + // add slide number to right footer + switch(slidenum) { + case 1: // show slide number only + mvwprintw(stdscr, + LINES - 1, COLS - int_length(sc) - 3, + "%d", sc); + break; + case 2: // show current slide & number of slides + mvwprintw(stdscr, + LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6, + "%d / %d", sc, deck->slides); + break; + } + + // make header + fooder visible + wrefresh(content); + wrefresh(stdscr); + + line = slide->line; + l = stop = 0; + + // print lines + while(line) { + add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors); + + // raise stop counter if we pass a line having a stop bit + if(CHECK_BIT(line->bits, IS_STOP)) + stop++; + + l += (line->length / COLS) + 1; + line = line->next; + + // only stop here if we didn't stop here recently + if(stop > slide->stop) + break; + } + + // print pandoc URL references + // only if we already printed all lines of the current slide + if(!line) { + int i, ymax; + getmaxyx( content, ymax, i ); + for (i = 0; i < url_get_amount(); i++) { + mvwprintw(content, ymax - url_get_amount() - 1 + i, 3, + "[%d] ", i); + waddwstr(content, url_get_target(i)); + } + } + + // make content visible + wrefresh(content); + + // fade in + if(fade) + fade_in(content, trans, colors, invert); + + // re-enable fading after any undefined key press + if(COLORS == 256 && !nofade) + fade = true; + + // wait for user input + c = getch(); + + // evaluate user input + i = 0; + switch(c) { + + // show previous slide or stop bit + case KEY_UP: + case KEY_LEFT: + case KEY_PPAGE: + case 8: // BACKSPACE (ascii) + case 127: // BACKSPACE (xterm) + case 263: // BACKSPACE (getty) + case 'h': + case 'k': + if(stop > 1 || (stop == 1 && !line)) { + // show current slide again + // but stop one stop bit earlier + slide->stop--; + fade = false; + } else { + if(slide->prev) { + // show previous slide + slide = slide->prev; + sc--; + //stop on first bullet point always + if(slide->stop > 0) + slide->stop = 0; + } else { + // do nothing + fade = false; + } + } + break; + + // show next slide or stop bit + case KEY_DOWN: + case KEY_RIGHT: + case KEY_NPAGE: + case '\n': // ENTER + case ' ': // SPACE + case 'j': + case 'l': + if(stop && line) { + // show current slide again + // but stop one stop bit later (or at end of slide) + slide->stop++; + fade = false; + } else { + if(slide->next) { + // show next slide + slide = slide->next; + sc++; + } else { + // do nothing + fade = false; + } + } + break; + + // show slide n + case '9': + case '8': + case '7': + case '6': + case '5': + case '4': + case '3': + case '2': + case '1': + i = get_slide_number(c); + if(i > 0 && i <= deck->slides) { + while(sc != i) { + // search forward + if(sc < i) { + if(slide->next) { + slide = slide->next; + sc++; + } + // search backward + } else { + if(slide->prev) { + slide = slide->prev; + sc--; + } + } + } + } else { + // disable fading if slide n doesn't exist + fade = false; + } + break; + + // show first slide + case 'g': + case KEY_HOME: + slide = deck->slide; + sc = 1; + break; + + // show last slide + case 'G': + case KEY_END: + for(i = sc; i <= deck->slides; i++) { + if(slide->next) { + slide = slide->next; + sc++; + } + } + break; + + // reload + case 'r': + if(noreload == 0) { + // reload slide N + reload = sc; + slide = NULL; + } else { + // disable fading if reload is not possible + fade = false; + } + break; + + // quit + case 'q': + // do not fade out on exit + fade = false; + // do not reload + reload = 0; + slide = NULL; + break; + + default: + // disable fading on undefined key press + fade = false; + break; + } + + // fade out + if(fade) + fade_out(content, trans, colors, invert); + + url_purge(); + } + + // disable ncurses + endwin(); + + // free ncurses memory + delwin(content); + if(reload == 0) + delwin(stdscr); + + // return reload indicator (0 means no reload) + return reload; +} + +void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) { + + if(!line->text->value) { + return; + } + + int i; // increment + int offset = 0; // text offset + + // move the cursor in position + wmove(window, y, x); + + // IS_UNORDERED_LIST_3 + if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) { + offset = next_nonblank(line->text, 0); + char prompt[13]; + strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " "); + strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " "); + + if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) { + strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " "); + } else { + strcpy(&prompt[8], " +- "); + offset += 2; + } + + wprintw(window, + "%s", prompt); + + if(!CHECK_BIT(line->bits, IS_CODE)) + inline_display(window, &line->text->value[offset], colors); + + // IS_UNORDERED_LIST_2 + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) { + offset = next_nonblank(line->text, 0); + char prompt[9]; + strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " "); + + if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) { + strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " "); + } else { + strcpy(&prompt[4], " +- "); + offset += 2; + } + + wprintw(window, + "%s", prompt); + + if(!CHECK_BIT(line->bits, IS_CODE)) + inline_display(window, &line->text->value[offset], colors); + + // IS_UNORDERED_LIST_1 + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) { + offset = next_nonblank(line->text, 0); + char prompt[5]; + + if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) { + strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " "); + } else { + strcpy(&prompt[0], " +- "); + offset += 2; + } + + wprintw(window, + "%s", prompt); + + if(!CHECK_BIT(line->bits, IS_CODE)) + inline_display(window, &line->text->value[offset], colors); + } + + // IS_CODE + if(CHECK_BIT(line->bits, IS_CODE)) { + + if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) { + // set static offset for code + offset = CODE_INDENT; + } + + // reverse color for code blocks + if(colors) + wattron(window, COLOR_PAIR(CP_BLACK)); + + // print whole lines + waddwstr(window, &line->text->value[offset]); + } + + if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) && + !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) && + !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) && + !CHECK_BIT(line->bits, IS_CODE)) { + + // IS_QUOTE + if(CHECK_BIT(line->bits, IS_QUOTE)) { + while(line->text->value[offset] == '>') { + // print a reverse color block + if(colors) { + wattron(window, COLOR_PAIR(CP_BLACK)); + wprintw(window, "%s", " "); + wattron(window, COLOR_PAIR(CP_WHITE)); + wprintw(window, "%s", " "); + } else { + wprintw(window, "%s", ">"); + } + + // find next quote or break + offset++; + if(line->text->value[offset] == ' ') + offset = next_word(line->text, offset); + } + + inline_display(window, &line->text->value[offset], colors); + } else { + + // IS_CENTER + if(CHECK_BIT(line->bits, IS_CENTER)) { + if(line->length < max_cols) { + wmove(window, y, x + ((max_cols - line->length) / 2)); + } + } + + // IS_H1 || IS_H2 + if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) { + + // set headline color + if(colors) + wattron(window, COLOR_PAIR(CP_BLUE)); + + // enable underline for H1 + if(CHECK_BIT(line->bits, IS_H1)) + wattron(window, A_UNDERLINE); + + // skip hashes + while(line->text->value[offset] == '#') + offset = next_word(line->text, offset); + + // print whole lines + waddwstr(window, &line->text->value[offset]); + + wattroff(window, A_UNDERLINE); + + // no line-wide markdown + } else { + + inline_display(window, &line->text->value[offset], colors); + } + } + } + + // fill rest off line with spaces + // we only need this if the color is inverted (e.g. code-blocks), + // to ensure the background fades too + if(CHECK_BIT(line->bits, IS_CODE)) + for(i = getcurx(window) - x; i < max_cols; i++) + wprintw(window, "%s", " "); + + // reset to default color + if(colors) + wattron(window, COLOR_PAIR(CP_WHITE)); + wattroff(window, A_UNDERLINE); +} + +void inline_display(WINDOW *window, const wchar_t *c, const int colors) { + const static wchar_t *special = L"\\*_`!["; // list of interpreted chars + const wchar_t *i = c; // iterator + const wchar_t *start_link_name, *start_url; + int length_link_name, url_num; + cstack_t *stack = cstack_init(); + + + // for each char in line + for(; *i; i++) { + + // if char is in special char list + if(wcschr(special, *i)) { + + // closing special char (or second backslash) + // only if not followed by :alnum: + if((stack->top)(stack, *i) && + (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) { + + switch(*i) { + // print escaped backslash + case L'\\': + waddnwstr(window, i, 1); + break; + // disable highlight + case L'*': + if(colors) + wattron(window, COLOR_PAIR(CP_WHITE)); + break; + // disable underline + case L'_': + wattroff(window, A_UNDERLINE); + break; + // disable inline code + case L'`': + if(colors) + wattron(window, COLOR_PAIR(CP_WHITE)); + break; + } + + // remove top special char from stack + (stack->pop)(stack); + + // treat special as regular char + } else if((stack->top)(stack, L'\\')) { + waddnwstr(window, i, 1); + + // remove backslash from stack + (stack->pop)(stack); + + // opening special char + } else { + + // emphasis or code span can start after new-line or space only + // and of cause after another emphasis markup + //TODO this condition looks ugly + if(i == c || + iswspace(*(i - 1)) || + ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') && + ((i - 1) == c || iswspace(*(i - 2)))) || + *i == L'\\') { + + // url in pandoc style + if ((*i == L'[' && wcschr(i, L']')) || + (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) { + + if (*i == L'!') i++; + + if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) { + i++; + + // turn higlighting and underlining on + if (colors) + wattron(window, COLOR_PAIR(CP_BLUE)); + wattron(window, A_UNDERLINE); + + start_link_name = i; + + // print the content of the label + // the label is printed as is + do { + waddnwstr(window, i, 1); + i++; + } while (*i != L']'); + + length_link_name = i - 1 - start_link_name; + + i++; + i++; + + start_url = i; + + while (*i != L')') i++; + + url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0); + + wprintw(window, " [%d]", url_num); + + // turn highlighting and underlining off + wattroff(window, A_UNDERLINE); + wattron(window, COLOR_PAIR(CP_WHITE)); + + } else { + wprintw(window, "["); + } + + } else switch(*i) { + // enable highlight + case L'*': + if(colors) + wattron(window, COLOR_PAIR(CP_RED)); + break; + // enable underline + case L'_': + wattron(window, A_UNDERLINE); + break; + // enable inline code + case L'`': + if(colors) + wattron(window, COLOR_PAIR(CP_BLACK)); + break; + // do nothing for backslashes + } + + // push special char to stack + (stack->push)(stack, *i); + + } else { + waddnwstr(window, i, 1); + } + } + + } else { + // remove backslash from stack + if((stack->top)(stack, L'\\')) + (stack->pop)(stack); + + // print regular char + waddnwstr(window, i, 1); + } + } + + // pop stack until empty to prevent formated trailing spaces + while(!(stack->empty)(stack)) { + switch((stack->pop)(stack)) { + // disable highlight + case L'*': + if(colors) + wattron(window, COLOR_PAIR(CP_WHITE)); + break; + // disable underline + case L'_': + wattroff(window, A_UNDERLINE); + break; + // disable inline code + case L'`': + if(colors) + wattron(window, COLOR_PAIR(CP_WHITE)); + break; + // do nothing for backslashes + } + } + + (stack->delete)(stack); +} + +void fade_out(WINDOW *window, int trans, int colors, int invert) { + int i; // increment + if(colors && COLORS == 256) { + for(i = 22; i >= 0; i--) { + + // dim color pairs + if(invert) { + init_pair(CP_WHITE, white_ramp_invert[i], trans); + init_pair(CP_BLUE, blue_ramp_invert[i], trans); + init_pair(CP_RED, red_ramp_invert[i], trans); + init_pair(CP_BLACK, 15, white_ramp_invert[i]); + } else { + init_pair(CP_WHITE, white_ramp[i], trans); + init_pair(CP_BLUE, blue_ramp[i], trans); + init_pair(CP_RED, red_ramp[i], trans); + init_pair(CP_BLACK, 16, white_ramp[i]); + } + + // refresh window with new color + wrefresh(window); + + // delay for our eyes to recognize the change + usleep(FADE_DELAY); + } + } +} + +void fade_in(WINDOW *window, int trans, int colors, int invert) { + int i; // increment + if(colors && COLORS == 256) { + for(i = 0; i <= 23; i++) { + + // brighten color pairs + if(invert) { + init_pair(CP_WHITE, white_ramp_invert[i], trans); + init_pair(CP_BLUE, blue_ramp_invert[i], trans); + init_pair(CP_RED, red_ramp_invert[i], trans); + init_pair(CP_BLACK, 15, white_ramp_invert[i]); + } else { + init_pair(CP_WHITE, white_ramp[i], trans); + init_pair(CP_BLUE, blue_ramp[i], trans); + init_pair(CP_RED, red_ramp[i], trans); + init_pair(CP_BLACK, 16, white_ramp[i]); + } + + // refresh window with new color + wrefresh(window); + + // delay for our eyes to recognize the change + usleep(FADE_DELAY); + } + } +} + +int int_length (int val) { + int l = 1; + while(val > 9) { + l++; + val /= 10; + } + return l; +} + +int get_slide_number(char init) { + int retval = init - '0'; + char c; + // block for tenths of a second when using getch, ERR if no input + halfdelay(GOTO_SLIDE_DELAY); + while((c = getch()) != ERR) { + if (c < '0' || c > '9') { + retval = -1; + break; + } + retval = (retval * 10) + (c - '0'); + } + nocbreak(); // cancel half delay mode + cbreak(); // go back to cbreak + return retval; +} diff --git a/utf8.md b/utf8.md new file mode 100644 index 0000000..32c357d --- /dev/null +++ b/utf8.md @@ -0,0 +1,57 @@ +%title: UTF-8 Test Presentation + +_Tables:_ + +┍━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━┑ +│ Cell 1 │ Cell 2 │ Cell 3 │ Cell 4 │ +├──────────┼──────────┼──────────┼──────────┤ +│ Cell 5 │ Cell 6 │ Cell 7 │ Cell 8 │ +┕━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━┙ + +--- + +_Greek:_ + +Apò mēkhanês Theós + +ἀπὸ μηχανῆς Θεός + +--- + +_Chinese:_ + +bù zuō sǐ jiù bú huì sǐ + +不作死就不会死 + +--- + +_Japanese:_ + +Saru mo ki kara ochiru + +猿も木から落ちる。 + +--- + +_ASCII art:_ + + ██████████████ + ██▓▓▓▓▓▓▓▓▓ M ▓████ + ██▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██ + ██████░░░░██░░██████ + ██░░░░████░░██░░░░░░░░██ + ██░░░░████░░░░██░░░░░░██ + ████░░░░░░██████████ + ██░░░░░░░░░░░░░██ + ██░░░░░░░░░██ + ██░░░░░░██ + ██▓▓████▓▓▓█ + ██▓▓▓▓▓▓████▓▓█ + ██▓▓▓▓▓▓███░░███░ + ██░░░░░░███████ + ██░░░░███████ + ██████████ + ██▓▓▓▓▓▓▓▓▓██ + █████████████ +