commit df1c14700136f91e88b83c3608feab1b7a803cc7 Author: Miraty Date: Sat Oct 9 20:13:55 2021 +0200 Initialize repository diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..be3f7b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 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 Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are 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. + + 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. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + 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 Affero 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. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + 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 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 work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero 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 Affero 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 Affero 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 Affero 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 Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + 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 AGPL, see +. diff --git a/README.md b/README.md new file mode 100755 index 0000000..87c2a0a --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +mkht.php is a PHP script for building Gemini, Markdown and HTML/CSS sites from source documents in Gemini, Markdown, HTML, PHP, CSS and Less. + +# Usage + +Place your pages tree in `/src/*/*.(gmi|md)`. + +Optionnal files: +* `/config.ini` +* `/style.less` +* `/logo.png` +* `/head.inc.html` +* `/footer.inc.html` + +`mkht.php ` + +`destination` is optionnal and can be: +* `dns` if you want local links without exensions +* `onion` if you want links ending with .onion when available + +# Input + +Pages in `/src`can use Gemini (if using `gmi` extension), Markdown, HTML and PHP. + +# Output + +* `/*/*.gmi` (if using `.gmi` extension in /src) +* `/*/*.md` +* `/*/*.html` +* `/*/*.gz` + +Note that format translation is only done in the following order: +Gemini > Markdown > HTML, which means that the last of these formats you will use will be the first that will be readable by hypertext browsers. (PHP is executed before.) + +# External dependencies + +* PHP +* gzip +* find + +# Internal libraries used + +| Name | Description | Source | +| --------- | ------------------------ | ------------------------------------------ | +| less.php | Less compiler in PHP | https://github.com/wikimedia/less.php | +| parsedown | Markdown compiler in PHP | https://parsedown.org | + +# License + +[AGPLv3+](LICENSE) + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License along with this program. If not, see . \ No newline at end of file diff --git a/less.php/CHANGES.md b/less.php/CHANGES.md new file mode 100755 index 0000000..92f82f8 --- /dev/null +++ b/less.php/CHANGES.md @@ -0,0 +1,70 @@ +# 3.1.0 +- [All Changes](https://github.com/wikimedia/less.php/compare/v3.0.0...v3.1.0) +* PHP 8.0 support: Drop use of curly braces for sub-string eval (James D. Forrester) +* Make `Directive::__construct` $rules arg optional (fix PHP 7.4 warning) (Sam Reed) +* ProcessExtends: Improve performance by using a map for selectors and parents (Andrey Legayev) +* build: Run CI tests on PHP 8.0 too (James D. Forrester) +* code: Fix PSR12.Properties.ConstantVisibility.NotFound (Sam Reed) + +# 3.0.0 +- [All Changes](https://github.com/wikimedia/less.php/compare/v2.0.0...v3.0.0) +- Raise PHP requirement from 7.1 to 7.2.9 (James Forrester) +- build: Upgrade phpunit to ^8.5 and make pass (James Forrester) +- build: Install php-parallel-lint (James Forrester) +- build: Install minus-x and make pass (James Forrester) + +# 2.0.0 +- [All Changes](https://github.com/wikimedia/less.php/compare/1.8.2...v2.0.0) +- Relax PHP requirement down to 7.1, from 7.2.9 (Franz Liedke) +- Reflect recent breaking changes properly with the semantic versioning (James Forrester) + +# 1.8.2 +- [All Changes](https://github.com/wikimedia/less.php/compare/1.8.1...1.8.2) +- Require PHP 7.2.9+, up from 5.3+ (James Forrester) +- Release: Update Version.php with the current release ID (COBadger) +- Fix access array offset on value of type null (Michele Locati) +- Fixed test suite on PHP 7.4 (Sergei Morozov) +- docs: Fix 1.8.1 "All changes" link (Timo Tijhof) + +# 1.8.1 +- [All Changes](https://github.com/wikimedia/less.php/compare/v1.8.0...1.8.1) +- Another PHP 7.3 compatibility tweak + +# 1.8.0 +- [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.13...v1.8.0) +- Wikimedia fork +- Supports up to PHP 7.3 +- No longer tested against PHP 5, though it's still remains allowed in `composer.json` for HHVM compatibility +- Switched to [semantic versioning](https://semver.org/), hence version numbers now use 3 digits + +# 1.7.0.13 + - [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.12...v1.7.0.13) + - Fix composer.json (PSR-4 was invalid) + +# 1.7.0.12 + - [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.11...v1.7.0.12) + - set bin/lessc bit executable + - Add 'gettingVariables' method in Less_Parser + +# 1.7.0.11 + - [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.10...v1.7.0.11) + - Fix realpath issue (windows) + - Set Less_Tree_Call property back to public ( Fix 258 266 267 issues from oyejorge/less.php) + +# 1.7.0.10 + + - [All Changes](https://github.com/oyejorge/less.php/compare/v1.7.0.9...v1.7.10) + - Add indentation option + - Add 'optional' modifier for @import + - fix $color in Exception messages + - don't use set_time_limit when running cli + - take relative-url into account when building the cache filename + - urlArgs should be string no array() + - add bug-report fixtures [#6dc898f](https://github.com/oyejorge/less.php/commit/6dc898f5d75b447464906bdf19d79c2e19d95e33) + - fix #269, missing on NameValue type [#a8dac63](https://github.com/oyejorge/less.php/commit/a8dac63d93fb941c54fb78b12588abf635747c1b) + +# 1.7.0.9 + + - [All Changes](https://github.com/oyejorge/less.php/compare/v1.7.0.8...v1.7.0.9) + - Remove space at beginning of Version.php + - Revert require() paths in test interface diff --git a/less.php/LICENSE b/less.php/LICENSE new file mode 100755 index 0000000..82216a5 --- /dev/null +++ b/less.php/LICENSE @@ -0,0 +1,178 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + diff --git a/less.php/README.md b/less.php/README.md new file mode 100755 index 0000000..5d6c0af --- /dev/null +++ b/less.php/README.md @@ -0,0 +1,314 @@ +[![Continuous Integration](https://github.com/wikimedia/less.php/workflows/PHP%20Test/badge.svg)](https://github.com/wikimedia/less.php/actions) + +[Less.php](http://lessphp.typesettercms.com) +======== + +This is the Wikimedia fork of a PHP port of the official LESS processor . + +* [About](#about) +* [Installation](#installation) +* [Basic Use](#basic-use) +* [Caching](#caching) +* [Source Maps](#source-maps) +* [Command Line](#command-line) +* [Integration with other projects](#integration-with-other-projects) +* [Transitioning from Leafo/lessphp](#transitioning-from-leafolessphp) +* [Credits](#credits) + + + +About +--- +The code structure of less.php mirrors that of the official processor which helps us ensure compatibility and allows for easy maintenance. + +Please note, there are a few unsupported LESS features: + +- Evaluation of JavaScript expressions within back-ticks (for obvious reasons). +- Definition of custom functions. + + +Installation +--- + +You can install the library with Composer or manually. + +#### Composer + +1. [Install Composer](https://getcomposer.org/download/) +2. Run `composer require wikimedia/less.php` + +#### Manually From Release + +Step 1. [Download a release](https://github.com/wikimedia/less.php/releases) and upload the PHP files to your server. + +Step 2. Include the library: + +```php +require_once '[path to less.php]/lib/Less/Autoloader.php'; +Less_Autoloader::register(); +``` + +Basic Use +--- + +#### Parsing Strings + +```php +$parser = new Less_Parser(); +$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' ); +$css = $parser->getCss(); +``` + + +#### Parsing LESS Files +The parseFile() function takes two arguments: + +1. The absolute path of the .less file to be parsed +2. The url root to prepend to any relative image or @import urls in the .less file. + +```php +$parser = new Less_Parser(); +$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' ); +$css = $parser->getCss(); +``` + + +#### Handling Invalid LESS +An exception will be thrown if the compiler encounters invalid LESS. + +```php +try{ + $parser = new Less_Parser(); + $parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' ); + $css = $parser->getCss(); +}catch(Exception $e){ + $error_message = $e->getMessage(); +} +``` + + +#### Parsing Multiple Sources +less.php can parse multiple sources to generate a single CSS file. + +```php +$parser = new Less_Parser(); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' ); +$css = $parser->getCss(); +``` + +#### Getting Info About The Parsed Files +less.php can tell you which .less files were imported and parsed. + +```php +$parser = new Less_Parser(); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$css = $parser->getCss(); +$imported_files = $parser->allParsedFiles(); +``` + + +#### Compressing Output +You can tell less.php to remove comments and whitespace to generate minimized CSS files. + +```php +$options = array( 'compress'=>true ); +$parser = new Less_Parser( $options ); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$css = $parser->getCss(); +``` + +#### Getting Variables +You can use the getVariables() method to get an all variables defined and +their value in a php associative array. Note that LESS has to be previously +compiled. +```php +$parser = new Less_Parser; +$parser->parseFile( '/var/www/mysite/bootstrap.less'); +$css = $parser->getCss(); +$variables = $parser->getVariables(); + +``` + + + +#### Setting Variables +You can use the ModifyVars() method to customize your CSS if you have variables stored in PHP associative arrays. + +```php +$parser = new Less_Parser(); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$parser->ModifyVars( array('font-size-base'=>'16px') ); +$css = $parser->getCss(); +``` + + +#### Import Directories +By default, less.php will look for @imports in the directory of the file passed to parseFile(). +If you're using parse() or if @imports reside in different directories, you can tell less.php where to look. + +```php +$directories = array( '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' ); +$parser = new Less_Parser(); +$parser->SetImportDirs( $directories ); +$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' ); +$css = $parser->getCss(); +``` + + +Caching +--- +Compiling LESS code into CSS is a time consuming process, caching your results is highly recommended. + + +#### Caching CSS +Use the Less_Cache class to save and reuse the results of compiled LESS files. +This method will check the modified time and size of each LESS file (including imported files) and regenerate a new CSS file when changes are found. +Note: When changes are found, this method will return a different file name for the new cached content. + +```php +$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' ); +$options = array( 'cache_dir' => '/var/www/writable_folder' ); +$css_file_name = Less_Cache::Get( $less_files, $options ); +$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name ); +``` + +#### Caching CSS With Variables +Passing options to Less_Cache::Get() + +```php +$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' ); +$options = array( 'cache_dir' => '/var/www/writable_folder' ); +$variables = array( 'width' => '100px' ); +$css_file_name = Less_Cache::Get( $less_files, $options, $variables ); +$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name ); +``` + + +#### Parser Caching +less.php will save serialized parser data for each .less file if a writable folder is passed to the SetCacheDir() method. +Note: This feature only caches intermediate parsing results to improve the performance of repeated CSS generation. +Your application should cache any CSS generated by less.php. + +```php +$options = array('cache_dir'=>'/var/www/writable_folder'); +$parser = new Less_Parser( $options ); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$css = $parser->getCss(); +``` + +You can specify the caching technique used by changing the ```cache_method``` option. Supported methods are: +* ```php```: Creates valid PHP files which can be included without any changes (default method). +* ```var_export```: Like "php", but using PHP's ```var_export()``` function without any optimizations. + It's recommended to use "php" instead. +* ```serialize```: Faster, but pretty memory-intense. +* ```callback```: Use custom callback functions to implement your own caching method. Give the "cache_callback_get" and + "cache_callback_set" options with callables (see PHP's ```call_user_func()``` and ```is_callable()``` functions). less.php + will pass the parser object (class ```Less_Parser```), the path to the parsed .less file ("/some/path/to/file.less") and + an identifier that will change every time the .less file is modified. The ```get``` callback must return the ruleset + (an array with ```Less_Tree``` objects) provided as fourth parameter of the ```set``` callback. If something goes wrong, + return ```NULL``` (cache doesn't exist) or ```FALSE```. + + + +Source Maps +--- +Less.php supports v3 sourcemaps + +#### Inline +The sourcemap will be appended to the generated CSS file. + +```php +$options = array( 'sourceMap' => true ); +$parser = new Less_Parser($options); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$css = $parser->getCss(); +``` + +#### Saving to Map File + +```php +$options = array( + 'sourceMap' => true, + 'sourceMapWriteTo' => '/var/www/mysite/writable_folder/filename.map', + 'sourceMapURL' => '/mysite/writable_folder/filename.map', + ); +$parser = new Less_Parser($options); +$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' ); +$css = $parser->getCss(); +``` + + +Command line +--- +An additional script has been included to use the compiler from the command line. +In the simplest invocation, you specify an input file and the compiled CSS is written to standard out: + +``` +$ lessc input.less > output.css +``` + +By using the -w flag you can watch a specified input file and have it compile as needed to the output file: + +``` +$ lessc -w input.less output.css +``` + +Errors from watch mode are written to standard out. + +For more help, run `lessc --help` + + +Integration with other projects +--- + +#### Drupal 7 + +This library can be used as drop-in replacement of lessphp to work with [Drupal 7 less module](https://drupal.org/project/less). + +How to install: + +1. [Download the less.php source code](https://github.com/wikimedia/less.php/archive/master.zip) and unzip it so that 'lessc.inc.php' is located at 'sites/all/libraries/lessphp/lessc.inc.php'. +2. Download and install [Drupal 7 less module](https://drupal.org/project/less) as usual. +3. That's it :) + +#### JBST WordPress theme + +JBST has a built-in LESS compiler based on lessphp. Customize your WordPress theme with LESS. + +How to use / install: + +1. [Download the latest release](https://github.com/bassjobsen/jamedo-bootstrap-start-theme) copy the files to your {wordpress/}wp-content/themes folder and activate it. +2. Find the compiler under Appearance > LESS Compiler in your WordPress dashboard +3. Enter your LESS code in the text area and press (re)compile + +Use the built-in compiler to: +- set any [Bootstrap](http://getbootstrap.com/customize/) variable or use Bootstrap's mixins: + -`@navbar-default-color: blue;` + - create a custom button: `.btn-custom { + .button-variant(white; red; blue); +}` +- set any built-in LESS variable: for example `@footer_bg_color: black;` sets the background color of the footer to black +- use built-in mixins: - add a custom font: `.include-custom-font(@family: arial,@font-path, @path: @custom-font-dir, @weight: normal, @style: normal);` + +The compiler can also be downloaded as [plugin](http://wordpress.org/plugins/wp-less-to-css/) + +#### WordPress + +This simple plugin will simply make the library available to other plugins and themes and can be used as a dependency using the [TGM Library](http://tgmpluginactivation.com/) + +How to install: + +1. Install the plugin from your WordPress Dashboard: http://wordpress.org/plugins/lessphp/ +2. That's it :) + + +Transitioning from Leafo/lessphp +--- +Projects looking for an easy transition from leafo/lessphp can use the lessc.inc.php adapter. To use, [Download the less.php source code](https://github.com/wikimedia/less.php/archive/master.zip) and unzip the files into your project so that the new 'lessc.inc.php' replaces the existing 'lessc.inc.php'. + +Note, the 'setPreserveComments' will no longer have any effect on the compiled LESS. + +Credits +--- +less.php was originally ported to PHP by [Matt Agar](https://github.com/agar) and then updated by [Martin Jantošovič](https://github.com/Mordred). This Wikimedia-maintained fork was split off from [Josh Schmidt's version](https://github.com/oyejorge/less.php). diff --git a/less.php/bin/lessc b/less.php/bin/lessc new file mode 100755 index 0000000..fa1fb95 --- /dev/null +++ b/less.php/bin/lessc @@ -0,0 +1,191 @@ +#!/usr/bin/env php + false, 'relativeUrls' => false); +$silent = false; +$watch = false; +$rootpath = ''; + +// Check for arguments +array_shift($argv); +if (!count($argv)) { + $argv[] = '-h'; +} + +// parse arguments +foreach ($argv as $key => $arg) { + if (preg_match('/^--?([a-z][0-9a-z-]*)(?:=([^\s]+))?$/i', $arg, $matches)) { + $option = $matches[1]; + $value = isset($matches[2]) ? $matches[2] : false; + unset($argv[$key]); + + switch ($option) { + case 'h': + case 'help': + echo << 1) { + $output = array_pop($argv); + $inputs = $argv; +} +else { + $inputs = $argv; + $output = false; +} + +if (!count($inputs)) { + echo("lessc: no input files\n"); + exit; +} + +if ($watch) { + if (!$output) { + echo("lessc: you must specify the output file if --watch is given\n"); + exit; + } + + $lastAction = 0; + + echo("lessc: watching input files\n"); + + while (1) { + clearstatcache(); + + $updated = false; + foreach ($inputs as $input) { + if ($input == '-') { + if (count($inputs) == 1) { + echo("lessc: during watching files is not possible to watch stdin\n"); + exit; + } + else { + continue; + } + } + + if (filemtime($input) > $lastAction) { + $updated = true; + break; + } + } + + if ($updated) { + $lastAction = time(); + $parser = new Less_Parser($env); + foreach ($inputs as $input) { + try { + $parser->parseFile($input, $rootpath); + } + catch (Exception $e) { + echo("lessc: " . $e->getMessage() . " \n"); + continue; // Invalid processing + } + } + + file_put_contents($output, $parser->getCss()); + echo("lessc: output file recompiled\n"); + } + + sleep(1); + } +} +else { + $parser = new Less_Parser($env); + foreach ($inputs as $input) { + if ($input == '-') { + $content = file_get_contents('php://stdin'); + $parser->parse($content); + } + else { + try { + $parser->parseFile($input); + } + catch (Exception $e) { + if (!$silent) { + echo("lessc: " . ((string)$e) . " \n"); + } + } + } + } + + if ($output) { + file_put_contents($output, $parser->getCss()); + } + else { + echo $parser->getCss(); + } +} diff --git a/less.php/lessc.inc.php b/less.php/lessc.inc.php new file mode 100755 index 0000000..284445f --- /dev/null +++ b/less.php/lessc.inc.php @@ -0,0 +1,274 @@ +importDir = (array)$dirs; + } + + public function addImportDir( $dir ) { + $this->importDir = (array)$this->importDir; + $this->importDir[] = $dir; + } + + public function setFormatter( $name ) { + $this->formatterName = $name; + } + + public function setPreserveComments( $preserve ) { + } + + public function registerFunction( $name, $func ) { + $this->libFunctions[$name] = $func; + } + + public function unregisterFunction( $name ) { + unset( $this->libFunctions[$name] ); + } + + public function setVariables( $variables ) { + foreach ( $variables as $name => $value ) { + $this->setVariable( $name, $value ); + } + } + + public function setVariable( $name, $value ) { + $this->registeredVars[$name] = $value; + } + + public function unsetVariable( $name ) { + unset( $this->registeredVars[$name] ); + } + + public function setOptions( $options ) { + foreach ( $options as $name => $value ) { + $this->setOption( $name, $value ); + } + } + + public function setOption( $name, $value ) { + $this->options[$name] = $value; + } + + public function parse( $buffer, $presets = array() ) { + $this->setVariables( $presets ); + + $parser = new Less_Parser( $this->getOptions() ); + $parser->setImportDirs( $this->getImportDirs() ); + foreach ( $this->libFunctions as $name => $func ) { + $parser->registerFunction( $name, $func ); + } + $parser->parse( $buffer ); + if ( count( $this->registeredVars ) ) { + $parser->ModifyVars( $this->registeredVars ); + } + + return $parser->getCss(); + } + + protected function getOptions() { + $options = array( 'relativeUrls' => false ); + switch ( $this->formatterName ) { + case 'compressed': + $options['compress'] = true; + break; + } + if ( is_array( $this->options ) ) { + $options = array_merge( $options, $this->options ); + } + return $options; + } + + protected function getImportDirs() { + $dirs_ = (array)$this->importDir; + $dirs = array(); + foreach ( $dirs_ as $dir ) { + $dirs[$dir] = ''; + } + return $dirs; + } + + public function compile( $string, $name = null ) { + $oldImport = $this->importDir; + $this->importDir = (array)$this->importDir; + + $this->allParsedFiles = array(); + + $parser = new Less_Parser( $this->getOptions() ); + $parser->SetImportDirs( $this->getImportDirs() ); + if ( count( $this->registeredVars ) ) { + $parser->ModifyVars( $this->registeredVars ); + } + foreach ( $this->libFunctions as $name => $func ) { + $parser->registerFunction( $name, $func ); + } + $parser->parse( $string ); + $out = $parser->getCss(); + + $parsed = Less_Parser::AllParsedFiles(); + foreach ( $parsed as $file ) { + $this->addParsedFile( $file ); + } + + $this->importDir = $oldImport; + + return $out; + } + + public function compileFile( $fname, $outFname = null ) { + if ( !is_readable( $fname ) ) { + throw new Exception( 'load error: failed to find '.$fname ); + } + + $pi = pathinfo( $fname ); + + $oldImport = $this->importDir; + + $this->importDir = (array)$this->importDir; + $this->importDir[] = Less_Parser::AbsPath( $pi['dirname'] ).'/'; + + $this->allParsedFiles = array(); + $this->addParsedFile( $fname ); + + $parser = new Less_Parser( $this->getOptions() ); + $parser->SetImportDirs( $this->getImportDirs() ); + if ( count( $this->registeredVars ) ) { + $parser->ModifyVars( $this->registeredVars ); + } + foreach ( $this->libFunctions as $name => $func ) { + $parser->registerFunction( $name, $func ); + } + $parser->parseFile( $fname ); + $out = $parser->getCss(); + + $parsed = Less_Parser::AllParsedFiles(); + foreach ( $parsed as $file ) { + $this->addParsedFile( $file ); + } + + $this->importDir = $oldImport; + + if ( $outFname !== null ) { + return file_put_contents( $outFname, $out ); + } + + return $out; + } + + public function checkedCompile( $in, $out ) { + if ( !is_file( $out ) || filemtime( $in ) > filemtime( $out ) ) { + $this->compileFile( $in, $out ); + return true; + } + return false; + } + + /** + * Execute lessphp on a .less file or a lessphp cache structure + * + * The lessphp cache structure contains information about a specific + * less file having been parsed. It can be used as a hint for future + * calls to determine whether or not a rebuild is required. + * + * The cache structure contains two important keys that may be used + * externally: + * + * compiled: The final compiled CSS + * updated: The time (in seconds) the CSS was last compiled + * + * The cache structure is a plain-ol' PHP associative array and can + * be serialized and unserialized without a hitch. + * + * @param mixed $in Input + * @param bool $force Force rebuild? + * @return array lessphp cache structure + */ + public function cachedCompile( $in, $force = false ) { + // assume no root + $root = null; + + if ( is_string( $in ) ) { + $root = $in; + } elseif ( is_array( $in ) and isset( $in['root'] ) ) { + if ( $force or !isset( $in['files'] ) ) { + // If we are forcing a recompile or if for some reason the + // structure does not contain any file information we should + // specify the root to trigger a rebuild. + $root = $in['root']; + } elseif ( isset( $in['files'] ) and is_array( $in['files'] ) ) { + foreach ( $in['files'] as $fname => $ftime ) { + if ( !file_exists( $fname ) or filemtime( $fname ) > $ftime ) { + // One of the files we knew about previously has changed + // so we should look at our incoming root again. + $root = $in['root']; + break; + } + } + } + } else { + // TODO: Throw an exception? We got neither a string nor something + // that looks like a compatible lessphp cache structure. + return null; + } + + if ( $root !== null ) { + // If we have a root value which means we should rebuild. + $out = array(); + $out['root'] = $root; + $out['compiled'] = $this->compileFile( $root ); + $out['files'] = $this->allParsedFiles(); + $out['updated'] = time(); + return $out; + } else { + // No changes, pass back the structure + // we were given initially. + return $in; + } + } + + public function ccompile( $in, $out, $less = null ) { + if ( $less === null ) { + $less = new self; + } + return $less->checkedCompile( $in, $out ); + } + + public static function cexecute( $in, $force = false, $less = null ) { + if ( $less === null ) { + $less = new self; + } + return $less->cachedCompile( $in, $force ); + } + + public function allParsedFiles() { + return $this->allParsedFiles; + } + + protected function addParsedFile( $file ) { + $this->allParsedFiles[Less_Parser::AbsPath( $file )] = filemtime( $file ); + } +} diff --git a/less.php/lib/Less/.easymin/ignore_prefixes b/less.php/lib/Less/.easymin/ignore_prefixes new file mode 100755 index 0000000..ca953b2 --- /dev/null +++ b/less.php/lib/Less/.easymin/ignore_prefixes @@ -0,0 +1,2 @@ +.easymin +Autoloader.php diff --git a/less.php/lib/Less/Autoloader.php b/less.php/lib/Less/Autoloader.php new file mode 100755 index 0000000..00116f7 --- /dev/null +++ b/less.php/lib/Less/Autoloader.php @@ -0,0 +1,77 @@ + '/' ); + } + + // generate name for compiled css file + $hash = md5( json_encode( $less_files ) ); + $list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list'; + + // check cached content + if ( !isset( $parser_options['use_cache'] ) || $parser_options['use_cache'] === true ) { + if ( file_exists( $list_file ) ) { + + self::ListFiles( $list_file, $list, $cached_name ); + $compiled_name = self::CompiledName( $list, $hash ); + + // if $cached_name is the same as the $compiled name, don't regenerate + if ( !$cached_name || $cached_name === $compiled_name ) { + + $output_file = self::OutputFile( $compiled_name, $parser_options ); + + if ( $output_file && file_exists( $output_file ) ) { + @touch( $list_file ); + return basename( $output_file ); // for backwards compatibility, we just return the name of the file + } + } + } + } + + $compiled = self::Cache( $less_files, $parser_options ); + if ( !$compiled ) { + return false; + } + + $compiled_name = self::CompiledName( $less_files, $hash ); + $output_file = self::OutputFile( $compiled_name, $parser_options ); + + // save the file list + $list = $less_files; + $list[] = $compiled_name; + $cache = implode( "\n", $list ); + file_put_contents( $list_file, $cache ); + + // save the css + file_put_contents( $output_file, $compiled ); + + // clean up + self::CleanCache(); + + return basename( $output_file ); + } + + /** + * Force the compiler to regenerate the cached css file + * + * @param array $less_files Array of .less files to compile + * @param array $parser_options Array of compiler options + * @param array $modify_vars Array of variables + * @return string Name of the css file + */ + public static function Regen( $less_files, $parser_options = array(), $modify_vars = array() ) { + $parser_options['use_cache'] = false; + return self::Get( $less_files, $parser_options, $modify_vars ); + } + + public static function Cache( &$less_files, $parser_options = array() ) { + // get less.php if it exists + $file = dirname( __FILE__ ) . '/Less.php'; + if ( file_exists( $file ) && !class_exists( 'Less_Parser' ) ) { + require_once $file; + } + + $parser_options['cache_dir'] = Less_Cache::$cache_dir; + $parser = new Less_Parser( $parser_options ); + + // combine files + foreach ( $less_files as $file_path => $uri_or_less ) { + + // treat as less markup if there are newline characters + if ( strpos( $uri_or_less, "\n" ) !== false ) { + $parser->Parse( $uri_or_less ); + continue; + } + + $parser->ParseFile( $file_path, $uri_or_less ); + } + + $compiled = $parser->getCss(); + + $less_files = $parser->allParsedFiles(); + + return $compiled; + } + + private static function OutputFile( $compiled_name, $parser_options ) { + // custom output file + if ( !empty( $parser_options['output'] ) ) { + + // relative to cache directory? + if ( preg_match( '#[\\\\/]#', $parser_options['output'] ) ) { + return $parser_options['output']; + } + + return Less_Cache::$cache_dir.$parser_options['output']; + } + + return Less_Cache::$cache_dir.$compiled_name; + } + + private static function CompiledName( $files, $extrahash ) { + // save the file list + $temp = array( Less_Version::cache_version ); + foreach ( $files as $file ) { + $temp[] = filemtime( $file )."\t".filesize( $file )."\t".$file; + } + + return Less_Cache::$prefix.sha1( json_encode( $temp ).$extrahash ).'.css'; + } + + public static function SetCacheDir( $dir ) { + Less_Cache::$cache_dir = $dir; + self::CheckCacheDir(); + } + + public static function CheckCacheDir() { + Less_Cache::$cache_dir = str_replace( '\\', '/', Less_Cache::$cache_dir ); + Less_Cache::$cache_dir = rtrim( Less_Cache::$cache_dir, '/' ).'/'; + + if ( !file_exists( Less_Cache::$cache_dir ) ) { + if ( !mkdir( Less_Cache::$cache_dir ) ) { + throw new Less_Exception_Parser( 'Less.php cache directory couldn\'t be created: '.Less_Cache::$cache_dir ); + } + + } elseif ( !is_dir( Less_Cache::$cache_dir ) ) { + throw new Less_Exception_Parser( 'Less.php cache directory doesn\'t exist: '.Less_Cache::$cache_dir ); + + } elseif ( !is_writable( Less_Cache::$cache_dir ) ) { + throw new Less_Exception_Parser( 'Less.php cache directory isn\'t writable: '.Less_Cache::$cache_dir ); + + } + + } + + /** + * Delete unused less.php files + * + */ + public static function CleanCache() { + static $clean = false; + + if ( $clean || empty( Less_Cache::$cache_dir ) ) { + return; + } + + $clean = true; + + // only remove files with extensions created by less.php + // css files removed based on the list files + $remove_types = array( 'lesscache' => 1,'list' => 1,'less' => 1,'map' => 1 ); + + $files = scandir( Less_Cache::$cache_dir ); + if ( !$files ) { + return; + } + + $check_time = time() - self::$gc_lifetime; + foreach ( $files as $file ) { + + // don't delete if the file wasn't created with less.php + if ( strpos( $file, Less_Cache::$prefix ) !== 0 ) { + continue; + } + + $parts = explode( '.', $file ); + $type = array_pop( $parts ); + + if ( !isset( $remove_types[$type] ) ) { + continue; + } + + $full_path = Less_Cache::$cache_dir . $file; + $mtime = filemtime( $full_path ); + + // don't delete if it's a relatively new file + if ( $mtime > $check_time ) { + continue; + } + + // delete the list file and associated css file + if ( $type === 'list' ) { + self::ListFiles( $full_path, $list, $css_file_name ); + if ( $css_file_name ) { + $css_file = Less_Cache::$cache_dir . $css_file_name; + if ( file_exists( $css_file ) ) { + unlink( $css_file ); + } + } + } + + unlink( $full_path ); + } + + } + + /** + * Get the list of less files and generated css file from a list file + * + */ + static function ListFiles( $list_file, &$list, &$css_file_name ) { + $list = explode( "\n", file_get_contents( $list_file ) ); + + // pop the cached name that should match $compiled_name + $css_file_name = array_pop( $list ); + + if ( !preg_match( '/^' . Less_Cache::$prefix . '[a-f0-9]+\.css$/', $css_file_name ) ) { + $list[] = $css_file_name; + $css_file_name = false; + } + + } + +} diff --git a/less.php/lib/Less/Colors.php b/less.php/lib/Less/Colors.php new file mode 100755 index 0000000..9be76cb --- /dev/null +++ b/less.php/lib/Less/Colors.php @@ -0,0 +1,169 @@ + '#f0f8ff', + 'antiquewhite' => '#faebd7', + 'aqua' => '#00ffff', + 'aquamarine' => '#7fffd4', + 'azure' => '#f0ffff', + 'beige' => '#f5f5dc', + 'bisque' => '#ffe4c4', + 'black' => '#000000', + 'blanchedalmond' => '#ffebcd', + 'blue' => '#0000ff', + 'blueviolet' => '#8a2be2', + 'brown' => '#a52a2a', + 'burlywood' => '#deb887', + 'cadetblue' => '#5f9ea0', + 'chartreuse' => '#7fff00', + 'chocolate' => '#d2691e', + 'coral' => '#ff7f50', + 'cornflowerblue' => '#6495ed', + 'cornsilk' => '#fff8dc', + 'crimson' => '#dc143c', + 'cyan' => '#00ffff', + 'darkblue' => '#00008b', + 'darkcyan' => '#008b8b', + 'darkgoldenrod' => '#b8860b', + 'darkgray' => '#a9a9a9', + 'darkgrey' => '#a9a9a9', + 'darkgreen' => '#006400', + 'darkkhaki' => '#bdb76b', + 'darkmagenta' => '#8b008b', + 'darkolivegreen' => '#556b2f', + 'darkorange' => '#ff8c00', + 'darkorchid' => '#9932cc', + 'darkred' => '#8b0000', + 'darksalmon' => '#e9967a', + 'darkseagreen' => '#8fbc8f', + 'darkslateblue' => '#483d8b', + 'darkslategray' => '#2f4f4f', + 'darkslategrey' => '#2f4f4f', + 'darkturquoise' => '#00ced1', + 'darkviolet' => '#9400d3', + 'deeppink' => '#ff1493', + 'deepskyblue' => '#00bfff', + 'dimgray' => '#696969', + 'dimgrey' => '#696969', + 'dodgerblue' => '#1e90ff', + 'firebrick' => '#b22222', + 'floralwhite' => '#fffaf0', + 'forestgreen' => '#228b22', + 'fuchsia' => '#ff00ff', + 'gainsboro' => '#dcdcdc', + 'ghostwhite' => '#f8f8ff', + 'gold' => '#ffd700', + 'goldenrod' => '#daa520', + 'gray' => '#808080', + 'grey' => '#808080', + 'green' => '#008000', + 'greenyellow' => '#adff2f', + 'honeydew' => '#f0fff0', + 'hotpink' => '#ff69b4', + 'indianred' => '#cd5c5c', + 'indigo' => '#4b0082', + 'ivory' => '#fffff0', + 'khaki' => '#f0e68c', + 'lavender' => '#e6e6fa', + 'lavenderblush' => '#fff0f5', + 'lawngreen' => '#7cfc00', + 'lemonchiffon' => '#fffacd', + 'lightblue' => '#add8e6', + 'lightcoral' => '#f08080', + 'lightcyan' => '#e0ffff', + 'lightgoldenrodyellow' => '#fafad2', + 'lightgray' => '#d3d3d3', + 'lightgrey' => '#d3d3d3', + 'lightgreen' => '#90ee90', + 'lightpink' => '#ffb6c1', + 'lightsalmon' => '#ffa07a', + 'lightseagreen' => '#20b2aa', + 'lightskyblue' => '#87cefa', + 'lightslategray' => '#778899', + 'lightslategrey' => '#778899', + 'lightsteelblue' => '#b0c4de', + 'lightyellow' => '#ffffe0', + 'lime' => '#00ff00', + 'limegreen' => '#32cd32', + 'linen' => '#faf0e6', + 'magenta' => '#ff00ff', + 'maroon' => '#800000', + 'mediumaquamarine' => '#66cdaa', + 'mediumblue' => '#0000cd', + 'mediumorchid' => '#ba55d3', + 'mediumpurple' => '#9370d8', + 'mediumseagreen' => '#3cb371', + 'mediumslateblue' => '#7b68ee', + 'mediumspringgreen' => '#00fa9a', + 'mediumturquoise' => '#48d1cc', + 'mediumvioletred' => '#c71585', + 'midnightblue' => '#191970', + 'mintcream' => '#f5fffa', + 'mistyrose' => '#ffe4e1', + 'moccasin' => '#ffe4b5', + 'navajowhite' => '#ffdead', + 'navy' => '#000080', + 'oldlace' => '#fdf5e6', + 'olive' => '#808000', + 'olivedrab' => '#6b8e23', + 'orange' => '#ffa500', + 'orangered' => '#ff4500', + 'orchid' => '#da70d6', + 'palegoldenrod' => '#eee8aa', + 'palegreen' => '#98fb98', + 'paleturquoise' => '#afeeee', + 'palevioletred' => '#d87093', + 'papayawhip' => '#ffefd5', + 'peachpuff' => '#ffdab9', + 'peru' => '#cd853f', + 'pink' => '#ffc0cb', + 'plum' => '#dda0dd', + 'powderblue' => '#b0e0e6', + 'purple' => '#800080', + 'red' => '#ff0000', + 'rosybrown' => '#bc8f8f', + 'royalblue' => '#4169e1', + 'saddlebrown' => '#8b4513', + 'salmon' => '#fa8072', + 'sandybrown' => '#f4a460', + 'seagreen' => '#2e8b57', + 'seashell' => '#fff5ee', + 'sienna' => '#a0522d', + 'silver' => '#c0c0c0', + 'skyblue' => '#87ceeb', + 'slateblue' => '#6a5acd', + 'slategray' => '#708090', + 'slategrey' => '#708090', + 'snow' => '#fffafa', + 'springgreen' => '#00ff7f', + 'steelblue' => '#4682b4', + 'tan' => '#d2b48c', + 'teal' => '#008080', + 'thistle' => '#d8bfd8', + 'tomato' => '#ff6347', + 'turquoise' => '#40e0d0', + 'violet' => '#ee82ee', + 'wheat' => '#f5deb3', + 'white' => '#ffffff', + 'whitesmoke' => '#f5f5f5', + 'yellow' => '#ffff00', + 'yellowgreen' => '#9acd32' + ); + + public static function hasOwnProperty( $color ) { + return isset( self::$colors[$color] ); + } + + public static function color( $color ) { + return self::$colors[$color]; + } + +} diff --git a/less.php/lib/Less/Configurable.php b/less.php/lib/Less/Configurable.php new file mode 100755 index 0000000..7f1348b --- /dev/null +++ b/less.php/lib/Less/Configurable.php @@ -0,0 +1,66 @@ +defaultOptions ); + $this->options = array_merge( $this->defaultOptions, $this->options, $options ); + } + + /** + * Get an option value by name + * + * If the option is empty or not set a NULL value will be returned. + * + * @param string $name + * @param mixed $default Default value if confiuration of $name is not present + * @return mixed + */ + public function getOption( $name, $default = null ) { + if ( isset( $this->options[$name] ) ) { + return $this->options[$name]; + } + return $default; + } + + /** + * Set an option + * + * @param string $name + * @param mixed $value + */ + public function setOption( $name, $value ) { + $this->options[$name] = $value; + } + +} diff --git a/less.php/lib/Less/Environment.php b/less.php/lib/Less/Environment.php new file mode 100755 index 0000000..2555d03 --- /dev/null +++ b/less.php/lib/Less/Environment.php @@ -0,0 +1,157 @@ + ',', + ': ' => ':', + '' => '', + ' ' => ' ', + ':' => ' :', + '+' => '+', + '~' => '~', + '>' => '>', + '|' => '|', + '^' => '^', + '^^' => '^^' + ); + + } else { + + Less_Environment::$_outputMap = array( + ',' => ', ', + ': ' => ': ', + '' => '', + ' ' => ' ', + ':' => ' :', + '+' => ' + ', + '~' => ' ~ ', + '>' => ' > ', + '|' => '|', + '^' => ' ^ ', + '^^' => ' ^^ ' + ); + + } + } + + public function copyEvalEnv( $frames = array() ) { + $new_env = new Less_Environment(); + $new_env->frames = $frames; + return $new_env; + } + + public static function isMathOn() { + return !Less_Parser::$options['strictMath'] || Less_Environment::$parensStack; + } + + public static function isPathRelative( $path ) { + return !preg_match( '/^(?:[a-z-]+:|\/)/', $path ); + } + + /** + * Canonicalize a path by resolving references to '/./', '/../' + * Does not remove leading "../" + * @param string path or url + * @return string Canonicalized path + * + */ + public static function normalizePath( $path ) { + $segments = explode( '/', $path ); + $segments = array_reverse( $segments ); + + $path = array(); + $path_len = 0; + + while ( $segments ) { + $segment = array_pop( $segments ); + switch ( $segment ) { + + case '.': + break; + + case '..': + if ( !$path_len || ( $path[$path_len - 1] === '..' ) ) { + $path[] = $segment; + $path_len++; + } else { + array_pop( $path ); + $path_len--; + } + break; + + default: + $path[] = $segment; + $path_len++; + break; + } + } + + return implode( '/', $path ); + } + + public function unshiftFrame( $frame ) { + array_unshift( $this->frames, $frame ); + } + + public function shiftFrame() { + return array_shift( $this->frames ); + } + +} diff --git a/less.php/lib/Less/Exception/Chunk.php b/less.php/lib/Less/Exception/Chunk.php new file mode 100755 index 0000000..1f1dddb --- /dev/null +++ b/less.php/lib/Less/Exception/Chunk.php @@ -0,0 +1,203 @@ +message = 'ParseError: Unexpected input'; // default message + + $this->index = $index; + + $this->currentFile = $currentFile; + + $this->input = $input; + $this->input_len = strlen( $input ); + + $this->Chunks(); + $this->genMessage(); + } + + /** + * See less.js chunks() + * We don't actually need the chunks + * + */ + protected function Chunks() { + $level = 0; + $parenLevel = 0; + $lastMultiCommentEndBrace = null; + $lastOpening = null; + $lastMultiComment = null; + $lastParen = null; + + for ( $this->parserCurrentIndex = 0; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) { + $cc = $this->CharCode( $this->parserCurrentIndex ); + if ( ( ( $cc >= 97 ) && ( $cc <= 122 ) ) || ( $cc < 34 ) ) { + // a-z or whitespace + continue; + } + + switch ( $cc ) { + + // ( + case 40: + $parenLevel++; + $lastParen = $this->parserCurrentIndex; + break; + + // ) + case 41: + $parenLevel--; + if ( $parenLevel < 0 ) { + return $this->fail( "missing opening `(`" ); + } + break; + + // ; + case 59: + // if (!$parenLevel) { $this->emitChunk(); } + break; + + // { + case 123: + $level++; + $lastOpening = $this->parserCurrentIndex; + break; + + // } + case 125: + $level--; + if ( $level < 0 ) { + return $this->fail( "missing opening `{`" ); + + } + // if (!$level && !$parenLevel) { $this->emitChunk(); } + break; + // \ + case 92: + if ( $this->parserCurrentIndex < $this->input_len - 1 ) { $this->parserCurrentIndex++; break; + } + return $this->fail( "unescaped `\\`" ); + + // ", ' and ` + case 34: + case 39: + case 96: + $matched = 0; + $currentChunkStartIndex = $this->parserCurrentIndex; + for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) { + $cc2 = $this->CharCode( $this->parserCurrentIndex ); + if ( $cc2 > 96 ) { continue; + } + if ( $cc2 == $cc ) { $matched = 1; break; + } + if ( $cc2 == 92 ) { // \ + if ( $this->parserCurrentIndex == $this->input_len - 1 ) { + return $this->fail( "unescaped `\\`" ); + } + $this->parserCurrentIndex++; + } + } + if ( $matched ) { break; + } + return $this->fail( "unmatched `" . chr( $cc ) . "`", $currentChunkStartIndex ); + + // /, check for comment + case 47: + if ( $parenLevel || ( $this->parserCurrentIndex == $this->input_len - 1 ) ) { break; + } + $cc2 = $this->CharCode( $this->parserCurrentIndex + 1 ); + if ( $cc2 == 47 ) { + // //, find lnfeed + for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) { + $cc2 = $this->CharCode( $this->parserCurrentIndex ); + if ( ( $cc2 <= 13 ) && ( ( $cc2 == 10 ) || ( $cc2 == 13 ) ) ) { break; + } + } + } else if ( $cc2 == 42 ) { + // /*, find */ + $lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex; + for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++ ) { + $cc2 = $this->CharCode( $this->parserCurrentIndex ); + if ( $cc2 == 125 ) { $lastMultiCommentEndBrace = $this->parserCurrentIndex; + } + if ( $cc2 != 42 ) { continue; + } + if ( $this->CharCode( $this->parserCurrentIndex + 1 ) == 47 ) { break; + } + } + if ( $this->parserCurrentIndex == $this->input_len - 1 ) { + return $this->fail( "missing closing `*/`", $currentChunkStartIndex ); + } + } + break; + + // *, check for unmatched */ + case 42: + if ( ( $this->parserCurrentIndex < $this->input_len - 1 ) && ( $this->CharCode( $this->parserCurrentIndex + 1 ) == 47 ) ) { + return $this->fail( "unmatched `/*`" ); + } + break; + } + } + + if ( $level !== 0 ) { + if ( ( $lastMultiComment > $lastOpening ) && ( $lastMultiCommentEndBrace > $lastMultiComment ) ) { + return $this->fail( "missing closing `}` or `*/`", $lastOpening ); + } else { + return $this->fail( "missing closing `}`", $lastOpening ); + } + } else if ( $parenLevel !== 0 ) { + return $this->fail( "missing closing `)`", $lastParen ); + } + + // chunk didn't fail + + //$this->emitChunk(true); + } + + public function CharCode( $pos ) { + return ord( $this->input[$pos] ); + } + + public function fail( $msg, $index = null ) { + if ( !$index ) { + $this->index = $this->parserCurrentIndex; + } else { + $this->index = $index; + } + $this->message = 'ParseError: '.$msg; + } + + /* + function emitChunk( $force = false ){ + $len = $this->parserCurrentIndex - $this->emitFrom; + if ((($len < 512) && !$force) || !$len) { + return; + } + $chunks[] = substr($this->input, $this->emitFrom, $this->parserCurrentIndex + 1 - $this->emitFrom ); + $this->emitFrom = $this->parserCurrentIndex + 1; + } + */ + +} diff --git a/less.php/lib/Less/Exception/Compiler.php b/less.php/lib/Less/Exception/Compiler.php new file mode 100755 index 0000000..1c3727a --- /dev/null +++ b/less.php/lib/Less/Exception/Compiler.php @@ -0,0 +1,11 @@ +previous = $previous; + parent::__construct( $message, $code ); + } else { + parent::__construct( $message, $code, $previous ); + } + + $this->currentFile = $currentFile; + $this->index = $index; + + $this->genMessage(); + } + + protected function getInput() { + if ( !$this->input && $this->currentFile && $this->currentFile['filename'] && file_exists( $this->currentFile['filename'] ) ) { + $this->input = file_get_contents( $this->currentFile['filename'] ); + } + } + + /** + * Converts the exception to string + * + * @return string + */ + public function genMessage() { + if ( $this->currentFile && $this->currentFile['filename'] ) { + $this->message .= ' in '.basename( $this->currentFile['filename'] ); + } + + if ( $this->index !== null ) { + $this->getInput(); + if ( $this->input ) { + $line = self::getLineNumber(); + $this->message .= ' on line '.$line.', column '.self::getColumn(); + + $lines = explode( "\n", $this->input ); + + $count = count( $lines ); + $start_line = max( 0, $line - 3 ); + $last_line = min( $count, $start_line + 6 ); + $num_len = strlen( $last_line ); + for ( $i = $start_line; $i < $last_line; $i++ ) { + $this->message .= "\n".str_pad( $i + 1, $num_len, '0', STR_PAD_LEFT ).'| '.$lines[$i]; + } + } + } + + } + + /** + * Returns the line number the error was encountered + * + * @return integer + */ + public function getLineNumber() { + if ( $this->index ) { + // https://bugs.php.net/bug.php?id=49790 + if ( ini_get( "mbstring.func_overload" ) ) { + return substr_count( substr( $this->input, 0, $this->index ), "\n" ) + 1; + } else { + return substr_count( $this->input, "\n", 0, $this->index ) + 1; + } + } + return 1; + } + + /** + * Returns the column the error was encountered + * + * @return integer + */ + public function getColumn() { + $part = substr( $this->input, 0, $this->index ); + $pos = strrpos( $part, "\n" ); + return $this->index - $pos; + } + +} diff --git a/less.php/lib/Less/Functions.php b/less.php/lib/Less/Functions.php new file mode 100755 index 0000000..c9e97bd --- /dev/null +++ b/less.php/lib/Less/Functions.php @@ -0,0 +1,1186 @@ +env = $env; + $this->currentFileInfo = $currentFileInfo; + } + + /** + * @param string $op + */ + public static function operate( $op, $a, $b ) { + switch ( $op ) { + case '+': +return $a + $b; + case '-': +return $a - $b; + case '*': +return $a * $b; + case '/': +return $a / $b; + } + } + + public static function clamp( $val, $max = 1 ) { + return min( max( $val, 0 ), $max ); + } + + public static function fround( $value ) { + if ( $value === 0 ) { + return $value; + } + + if ( Less_Parser::$options['numPrecision'] ) { + $p = pow( 10, Less_Parser::$options['numPrecision'] ); + return round( $value * $p ) / $p; + } + return $value; + } + + public static function number( $n ) { + if ( $n instanceof Less_Tree_Dimension ) { + return floatval( $n->unit->is( '%' ) ? $n->value / 100 : $n->value ); + } else if ( is_numeric( $n ) ) { + return $n; + } else { + throw new Less_Exception_Compiler( "color functions take numbers as parameters" ); + } + } + + public static function scaled( $n, $size = 255 ) { + if ( $n instanceof Less_Tree_Dimension && $n->unit->is( '%' ) ) { + return (float)$n->value * $size / 100; + } else { + return Less_Functions::number( $n ); + } + } + + public function rgb( $r = null, $g = null, $b = null ) { + if ( is_null( $r ) || is_null( $g ) || is_null( $b ) ) { + throw new Less_Exception_Compiler( "rgb expects three parameters" ); + } + return $this->rgba( $r, $g, $b, 1.0 ); + } + + public function rgba( $r = null, $g = null, $b = null, $a = null ) { + $rgb = array( $r, $g, $b ); + $rgb = array_map( array( 'Less_Functions','scaled' ), $rgb ); + + $a = self::number( $a ); + return new Less_Tree_Color( $rgb, $a ); + } + + public function hsl( $h, $s, $l ) { + return $this->hsla( $h, $s, $l, 1.0 ); + } + + public function hsla( $h, $s, $l, $a ) { + $h = fmod( self::number( $h ), 360 ) / 360; // Classic % operator will change float to int + $s = self::clamp( self::number( $s ) ); + $l = self::clamp( self::number( $l ) ); + $a = self::clamp( self::number( $a ) ); + + $m2 = $l <= 0.5 ? $l * ( $s + 1 ) : $l + $s - $l * $s; + + $m1 = $l * 2 - $m2; + + return $this->rgba( self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255, + self::hsla_hue( $h, $m1, $m2 ) * 255, + self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255, + $a ); + } + + /** + * @param double $h + */ + public function hsla_hue( $h, $m1, $m2 ) { + $h = $h < 0 ? $h + 1 : ( $h > 1 ? $h - 1 : $h ); + if ( $h * 6 < 1 ) return $m1 + ( $m2 - $m1 ) * $h * 6; else if ( $h * 2 < 1 ) return $m2; else if ( $h * 3 < 2 ) return $m1 + ( $m2 - $m1 ) * ( 2 / 3 - $h ) * 6; else return $m1; + } + + public function hsv( $h, $s, $v ) { + return $this->hsva( $h, $s, $v, 1.0 ); + } + + /** + * @param double $a + */ + public function hsva( $h, $s, $v, $a ) { + $h = ( ( Less_Functions::number( $h ) % 360 ) / 360 ) * 360; + $s = Less_Functions::number( $s ); + $v = Less_Functions::number( $v ); + $a = Less_Functions::number( $a ); + + $i = floor( ( $h / 60 ) % 6 ); + $f = ( $h / 60 ) - $i; + + $vs = array( $v, + $v * ( 1 - $s ), + $v * ( 1 - $f * $s ), + $v * ( 1 - ( 1 - $f ) * $s ) ); + + $perm = array( array( 0, 3, 1 ), + array( 2, 0, 1 ), + array( 1, 0, 3 ), + array( 1, 2, 0 ), + array( 3, 1, 0 ), + array( 0, 1, 2 ) ); + + return $this->rgba( $vs[$perm[$i][0]] * 255, + $vs[$perm[$i][1]] * 255, + $vs[$perm[$i][2]] * 255, + $a ); + } + + public function hue( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to hue must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $c = $color->toHSL(); + return new Less_Tree_Dimension( Less_Parser::round( $c['h'] ) ); + } + + public function saturation( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to saturation must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $c = $color->toHSL(); + return new Less_Tree_Dimension( Less_Parser::round( $c['s'] * 100 ), '%' ); + } + + public function lightness( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to lightness must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $c = $color->toHSL(); + return new Less_Tree_Dimension( Less_Parser::round( $c['l'] * 100 ), '%' ); + } + + public function hsvhue( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to hsvhue must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsv = $color->toHSV(); + return new Less_Tree_Dimension( Less_Parser::round( $hsv['h'] ) ); + } + + public function hsvsaturation( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to hsvsaturation must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsv = $color->toHSV(); + return new Less_Tree_Dimension( Less_Parser::round( $hsv['s'] * 100 ), '%' ); + } + + public function hsvvalue( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to hsvvalue must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsv = $color->toHSV(); + return new Less_Tree_Dimension( Less_Parser::round( $hsv['v'] * 100 ), '%' ); + } + + public function red( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to red must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return new Less_Tree_Dimension( $color->rgb[0] ); + } + + public function green( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to green must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return new Less_Tree_Dimension( $color->rgb[1] ); + } + + public function blue( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to blue must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return new Less_Tree_Dimension( $color->rgb[2] ); + } + + public function alpha( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to alpha must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $c = $color->toHSL(); + return new Less_Tree_Dimension( $c['a'] ); + } + + public function luma( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to luma must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return new Less_Tree_Dimension( Less_Parser::round( $color->luma() * $color->alpha * 100 ), '%' ); + } + + public function luminance( $color = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to luminance must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $luminance = + ( 0.2126 * $color->rgb[0] / 255 ) + + ( 0.7152 * $color->rgb[1] / 255 ) + + ( 0.0722 * $color->rgb[2] / 255 ); + + return new Less_Tree_Dimension( Less_Parser::round( $luminance * $color->alpha * 100 ), '%' ); + } + + public function saturate( $color = null, $amount = null ) { + // filter: saturate(3.2); + // should be kept as is, so check for color + if ( $color instanceof Less_Tree_Dimension ) { + return null; + } + + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to saturate must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to saturate must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + + $hsl['s'] += $amount->value / 100; + $hsl['s'] = self::clamp( $hsl['s'] ); + + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + /** + * @param Less_Tree_Dimension $amount + */ + public function desaturate( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to desaturate must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to desaturate must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + + $hsl['s'] -= $amount->value / 100; + $hsl['s'] = self::clamp( $hsl['s'] ); + + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + public function lighten( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to lighten must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to lighten must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + + $hsl['l'] += $amount->value / 100; + $hsl['l'] = self::clamp( $hsl['l'] ); + + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + public function darken( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to darken must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to darken must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + $hsl['l'] -= $amount->value / 100; + $hsl['l'] = self::clamp( $hsl['l'] ); + + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + public function fadein( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to fadein must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to fadein must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + $hsl['a'] += $amount->value / 100; + $hsl['a'] = self::clamp( $hsl['a'] ); + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + public function fadeout( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to fadeout must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to fadeout must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + $hsl['a'] -= $amount->value / 100; + $hsl['a'] = self::clamp( $hsl['a'] ); + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + public function fade( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to fade must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to fade must be a percentage' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + + $hsl['a'] = $amount->value / 100; + $hsl['a'] = self::clamp( $hsl['a'] ); + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + public function spin( $color = null, $amount = null ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to spin must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$amount instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The second argument to spin must be a number' . ( $amount instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $hsl = $color->toHSL(); + $hue = fmod( $hsl['h'] + $amount->value, 360 ); + + $hsl['h'] = $hue < 0 ? 360 + $hue : $hue; + + return $this->hsla( $hsl['h'], $hsl['s'], $hsl['l'], $hsl['a'] ); + } + + // + // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein + // http://sass-lang.com + // + + /** + * @param Less_Tree_Color $color1 + */ + public function mix( $color1 = null, $color2 = null, $weight = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to mix must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to mix must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$weight ) { + $weight = new Less_Tree_Dimension( '50', '%' ); + } + if ( !$weight instanceof Less_Tree_Dimension ) { + throw new Less_Exception_Compiler( 'The third argument to contrast must be a percentage' . ( $weight instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + $p = $weight->value / 100.0; + $w = $p * 2 - 1; + $hsl1 = $color1->toHSL(); + $hsl2 = $color2->toHSL(); + $a = $hsl1['a'] - $hsl2['a']; + + $w1 = ( ( ( ( $w * $a ) == -1 ) ? $w : ( $w + $a ) / ( 1 + $w * $a ) ) + 1 ) / 2; + $w2 = 1 - $w1; + + $rgb = array( $color1->rgb[0] * $w1 + $color2->rgb[0] * $w2, + $color1->rgb[1] * $w1 + $color2->rgb[1] * $w2, + $color1->rgb[2] * $w1 + $color2->rgb[2] * $w2 ); + + $alpha = $color1->alpha * $p + $color2->alpha * ( 1 - $p ); + + return new Less_Tree_Color( $rgb, $alpha ); + } + + public function greyscale( $color ) { + return $this->desaturate( $color, new Less_Tree_Dimension( 100, '%' ) ); + } + + public function contrast( $color, $dark = null, $light = null, $threshold = null ) { + // filter: contrast(3.2); + // should be kept as is, so check for color + if ( !$color instanceof Less_Tree_Color ) { + return null; + } + if ( !$light ) { + $light = $this->rgba( 255, 255, 255, 1.0 ); + } + if ( !$dark ) { + $dark = $this->rgba( 0, 0, 0, 1.0 ); + } + + if ( !$dark instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to contrast must be a color' . ( $dark instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$light instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The third argument to contrast must be a color' . ( $light instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + // Figure out which is actually light and dark! + if ( $dark->luma() > $light->luma() ) { + $t = $light; + $light = $dark; + $dark = $t; + } + if ( !$threshold ) { + $threshold = 0.43; + } else { + $threshold = Less_Functions::number( $threshold ); + } + + if ( $color->luma() < $threshold ) { + return $light; + } else { + return $dark; + } + } + + public function e( $str ) { + if ( is_string( $str ) ) { + return new Less_Tree_Anonymous( $str ); + } + return new Less_Tree_Anonymous( $str instanceof Less_Tree_JavaScript ? $str->expression : $str->value ); + } + + public function escape( $str ) { + $revert = array( '%21' => '!', '%2A' => '*', '%27' => "'",'%3F' => '?','%26' => '&','%2C' => ',','%2F' => '/','%40' => '@','%2B' => '+','%24' => '$' ); + + return new Less_Tree_Anonymous( strtr( rawurlencode( $str->value ), $revert ) ); + } + + /** + * todo: This function will need some additional work to make it work the same as less.js + * + */ + public function replace( $string, $pattern, $replacement, $flags = null ) { + $result = $string->value; + + $expr = '/'.str_replace( '/', '\\/', $pattern->value ).'/'; + if ( $flags && $flags->value ) { + $expr .= self::replace_flags( $flags->value ); + } + + $result = preg_replace( $expr, $replacement->value, $result ); + + if ( property_exists( $string, 'quote' ) ) { + return new Less_Tree_Quoted( $string->quote, $result, $string->escaped ); + } + return new Less_Tree_Quoted( '', $result ); + } + + public static function replace_flags( $flags ) { + $flags = str_split( $flags, 1 ); + $new_flags = ''; + + foreach ( $flags as $flag ) { + switch ( $flag ) { + case 'e': + case 'g': + break; + + default: + $new_flags .= $flag; + break; + } + } + + return $new_flags; + } + + public function _percent() { + $string = func_get_arg( 0 ); + + $args = func_get_args(); + array_shift( $args ); + $result = $string->value; + + foreach ( $args as $arg ) { + if ( preg_match( '/%[sda]/i', $result, $token ) ) { + $token = $token[0]; + $value = stristr( $token, 's' ) ? $arg->value : $arg->toCSS(); + $value = preg_match( '/[A-Z]$/', $token ) ? urlencode( $value ) : $value; + $result = preg_replace( '/%[sda]/i', $value, $result, 1 ); + } + } + $result = str_replace( '%%', '%', $result ); + + return new Less_Tree_Quoted( $string->quote, $result, $string->escaped ); + } + + public function unit( $val, $unit = null ) { + if ( !( $val instanceof Less_Tree_Dimension ) ) { + throw new Less_Exception_Compiler( 'The first argument to unit must be a number' . ( $val instanceof Less_Tree_Operation ? '. Have you forgotten parenthesis?' : '.' ) ); + } + + if ( $unit ) { + if ( $unit instanceof Less_Tree_Keyword ) { + $unit = $unit->value; + } else { + $unit = $unit->toCSS(); + } + } else { + $unit = ""; + } + return new Less_Tree_Dimension( $val->value, $unit ); + } + + public function convert( $val, $unit ) { + return $val->convertTo( $unit->value ); + } + + public function round( $n, $f = false ) { + $fraction = 0; + if ( $f !== false ) { + $fraction = $f->value; + } + + return $this->_math( 'Less_Parser::round', null, $n, $fraction ); + } + + public function pi() { + return new Less_Tree_Dimension( M_PI ); + } + + public function mod( $a, $b ) { + return new Less_Tree_Dimension( $a->value % $b->value, $a->unit ); + } + + public function pow( $x, $y ) { + if ( is_numeric( $x ) && is_numeric( $y ) ) { + $x = new Less_Tree_Dimension( $x ); + $y = new Less_Tree_Dimension( $y ); + } elseif ( !( $x instanceof Less_Tree_Dimension ) || !( $y instanceof Less_Tree_Dimension ) ) { + throw new Less_Exception_Compiler( 'Arguments must be numbers' ); + } + + return new Less_Tree_Dimension( pow( $x->value, $y->value ), $x->unit ); + } + + // var mathFunctions = [{name:"ce ... + public function ceil( $n ) { + return $this->_math( 'ceil', null, $n ); + } + + public function floor( $n ) { + return $this->_math( 'floor', null, $n ); + } + + public function sqrt( $n ) { + return $this->_math( 'sqrt', null, $n ); + } + + public function abs( $n ) { + return $this->_math( 'abs', null, $n ); + } + + public function tan( $n ) { + return $this->_math( 'tan', '', $n ); + } + + public function sin( $n ) { + return $this->_math( 'sin', '', $n ); + } + + public function cos( $n ) { + return $this->_math( 'cos', '', $n ); + } + + public function atan( $n ) { + return $this->_math( 'atan', 'rad', $n ); + } + + public function asin( $n ) { + return $this->_math( 'asin', 'rad', $n ); + } + + public function acos( $n ) { + return $this->_math( 'acos', 'rad', $n ); + } + + private function _math() { + $args = func_get_args(); + $fn = array_shift( $args ); + $unit = array_shift( $args ); + + if ( $args[0] instanceof Less_Tree_Dimension ) { + + if ( $unit === null ) { + $unit = $args[0]->unit; + } else { + $args[0] = $args[0]->unify(); + } + $args[0] = (float)$args[0]->value; + return new Less_Tree_Dimension( call_user_func_array( $fn, $args ), $unit ); + } else if ( is_numeric( $args[0] ) ) { + return call_user_func_array( $fn, $args ); + } else { + throw new Less_Exception_Compiler( "math functions take numbers as parameters" ); + } + } + + /** + * @param boolean $isMin + */ + private function _minmax( $isMin, $args ) { + $arg_count = count( $args ); + + if ( $arg_count < 1 ) { + throw new Less_Exception_Compiler( 'one or more arguments required' ); + } + + $j = null; + $unitClone = null; + $unitStatic = null; + + $order = array(); // elems only contains original argument values. + $values = array(); // key is the unit.toString() for unified tree.Dimension values, + // value is the index into the order array. + + for ( $i = 0; $i < $arg_count; $i++ ) { + $current = $args[$i]; + if ( !( $current instanceof Less_Tree_Dimension ) ) { + if ( is_array( $args[$i]->value ) ) { + $args[] = $args[$i]->value; + } + continue; + } + + if ( $current->unit->toString() === '' && !$unitClone ) { + $temp = new Less_Tree_Dimension( $current->value, $unitClone ); + $currentUnified = $temp->unify(); + } else { + $currentUnified = $current->unify(); + } + + if ( $currentUnified->unit->toString() === "" && !$unitStatic ) { + $unit = $unitStatic; + } else { + $unit = $currentUnified->unit->toString(); + } + + if ( $unit !== '' && !$unitStatic || $unit !== '' && $order[0]->unify()->unit->toString() === "" ) { + $unitStatic = $unit; + } + + if ( $unit != '' && !$unitClone ) { + $unitClone = $current->unit->toString(); + } + + if ( isset( $values[''] ) && $unit !== '' && $unit === $unitStatic ) { + $j = $values['']; + } elseif ( isset( $values[$unit] ) ) { + $j = $values[$unit]; + } else { + + if ( $unitStatic && $unit !== $unitStatic ) { + throw new Less_Exception_Compiler( 'incompatible types' ); + } + $values[$unit] = count( $order ); + $order[] = $current; + continue; + } + + if ( $order[$j]->unit->toString() === "" && $unitClone ) { + $temp = new Less_Tree_Dimension( $order[$j]->value, $unitClone ); + $referenceUnified = $temp->unify(); + } else { + $referenceUnified = $order[$j]->unify(); + } + if ( ( $isMin && $currentUnified->value < $referenceUnified->value ) || ( !$isMin && $currentUnified->value > $referenceUnified->value ) ) { + $order[$j] = $current; + } + } + + if ( count( $order ) == 1 ) { + return $order[0]; + } + $args = array(); + foreach ( $order as $a ) { + $args[] = $a->toCSS( $this->env ); + } + return new Less_Tree_Anonymous( ( $isMin ? 'min(' : 'max(' ) . implode( Less_Environment::$_outputMap[','], $args ).')' ); + } + + public function min() { + $args = func_get_args(); + return $this->_minmax( true, $args ); + } + + public function max() { + $args = func_get_args(); + return $this->_minmax( false, $args ); + } + + public function getunit( $n ) { + return new Less_Tree_Anonymous( $n->unit ); + } + + public function argb( $color ) { + if ( !$color instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to argb must be a color' . ( $color instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return new Less_Tree_Anonymous( $color->toARGB() ); + } + + public function percentage( $n ) { + return new Less_Tree_Dimension( $n->value * 100, '%' ); + } + + public function color( $n ) { + if ( $n instanceof Less_Tree_Quoted ) { + $colorCandidate = $n->value; + $returnColor = Less_Tree_Color::fromKeyword( $colorCandidate ); + if ( $returnColor ) { + return $returnColor; + } + if ( preg_match( '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/', $colorCandidate ) ) { + return new Less_Tree_Color( substr( $colorCandidate, 1 ) ); + } + throw new Less_Exception_Compiler( "argument must be a color keyword or 3/6 digit hex e.g. #FFF" ); + } else { + throw new Less_Exception_Compiler( "argument must be a string" ); + } + } + + public function iscolor( $n ) { + return $this->_isa( $n, 'Less_Tree_Color' ); + } + + public function isnumber( $n ) { + return $this->_isa( $n, 'Less_Tree_Dimension' ); + } + + public function isstring( $n ) { + return $this->_isa( $n, 'Less_Tree_Quoted' ); + } + + public function iskeyword( $n ) { + return $this->_isa( $n, 'Less_Tree_Keyword' ); + } + + public function isurl( $n ) { + return $this->_isa( $n, 'Less_Tree_Url' ); + } + + public function ispixel( $n ) { + return $this->isunit( $n, 'px' ); + } + + public function ispercentage( $n ) { + return $this->isunit( $n, '%' ); + } + + public function isem( $n ) { + return $this->isunit( $n, 'em' ); + } + + /** + * @param string $unit + */ + public function isunit( $n, $unit ) { + if ( is_object( $unit ) && property_exists( $unit, 'value' ) ) { + $unit = $unit->value; + } + + return ( $n instanceof Less_Tree_Dimension ) && $n->unit->is( $unit ) ? new Less_Tree_Keyword( 'true' ) : new Less_Tree_Keyword( 'false' ); + } + + /** + * @param string $type + */ + private function _isa( $n, $type ) { + return is_a( $n, $type ) ? new Less_Tree_Keyword( 'true' ) : new Less_Tree_Keyword( 'false' ); + } + + public function tint( $color, $amount = null ) { + return $this->mix( $this->rgb( 255, 255, 255 ), $color, $amount ); + } + + public function shade( $color, $amount = null ) { + return $this->mix( $this->rgb( 0, 0, 0 ), $color, $amount ); + } + + public function extract( $values, $index ) { + $index = (int)$index->value - 1; // (1-based index) + // handle non-array values as an array of length 1 + // return 'undefined' if index is invalid + if ( property_exists( $values, 'value' ) && is_array( $values->value ) ) { + if ( isset( $values->value[$index] ) ) { + return $values->value[$index]; + } + return null; + + } elseif ( (int)$index === 0 ) { + return $values; + } + + return null; + } + + public function length( $values ) { + $n = ( property_exists( $values, 'value' ) && is_array( $values->value ) ) ? count( $values->value ) : 1; + return new Less_Tree_Dimension( $n ); + } + + public function datauri( $mimetypeNode, $filePathNode = null ) { + $filePath = ( $filePathNode ? $filePathNode->value : null ); + $mimetype = $mimetypeNode->value; + + $args = 2; + if ( !$filePath ) { + $filePath = $mimetype; + $args = 1; + } + + $filePath = str_replace( '\\', '/', $filePath ); + if ( Less_Environment::isPathRelative( $filePath ) ) { + + if ( Less_Parser::$options['relativeUrls'] ) { + $temp = $this->currentFileInfo['currentDirectory']; + } else { + $temp = $this->currentFileInfo['entryPath']; + } + + if ( !empty( $temp ) ) { + $filePath = Less_Environment::normalizePath( rtrim( $temp, '/' ).'/'.$filePath ); + } + + } + + // detect the mimetype if not given + if ( $args < 2 ) { + + /* incomplete + $mime = require('mime'); + mimetype = mime.lookup(path); + + // use base 64 unless it's an ASCII or UTF-8 format + var charset = mime.charsets.lookup(mimetype); + useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0; + if (useBase64) mimetype += ';base64'; + */ + + $mimetype = Less_Mime::lookup( $filePath ); + + $charset = Less_Mime::charsets_lookup( $mimetype ); + $useBase64 = !in_array( $charset, array( 'US-ASCII', 'UTF-8' ) ); + if ( $useBase64 ) { $mimetype .= ';base64'; + } + + } else { + $useBase64 = preg_match( '/;base64$/', $mimetype ); + } + + if ( file_exists( $filePath ) ) { + $buf = @file_get_contents( $filePath ); + } else { + $buf = false; + } + + // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded + // and the --ieCompat flag is enabled, return a normal url() instead. + $DATA_URI_MAX_KB = 32; + $fileSizeInKB = round( strlen( $buf ) / 1024 ); + if ( $fileSizeInKB >= $DATA_URI_MAX_KB ) { + $url = new Less_Tree_Url( ( $filePathNode ? $filePathNode : $mimetypeNode ), $this->currentFileInfo ); + return $url->compile( $this ); + } + + if ( $buf ) { + $buf = $useBase64 ? base64_encode( $buf ) : rawurlencode( $buf ); + $filePath = '"data:' . $mimetype . ',' . $buf . '"'; + } + + return new Less_Tree_Url( new Less_Tree_Anonymous( $filePath ) ); + } + + // svg-gradient + public function svggradient( $direction ) { + $throw_message = 'svg-gradient expects direction, start_color [start_position], [color position,]..., end_color [end_position]'; + $arguments = func_get_args(); + + if ( count( $arguments ) < 3 ) { + throw new Less_Exception_Compiler( $throw_message ); + } + + $stops = array_slice( $arguments, 1 ); + $gradientType = 'linear'; + $rectangleDimension = 'x="0" y="0" width="1" height="1"'; + $useBase64 = true; + $directionValue = $direction->toCSS(); + + switch ( $directionValue ) { + case "to bottom": + $gradientDirectionSvg = 'x1="0%" y1="0%" x2="0%" y2="100%"'; + break; + case "to right": + $gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="0%"'; + break; + case "to bottom right": + $gradientDirectionSvg = 'x1="0%" y1="0%" x2="100%" y2="100%"'; + break; + case "to top right": + $gradientDirectionSvg = 'x1="0%" y1="100%" x2="100%" y2="0%"'; + break; + case "ellipse": + case "ellipse at center": + $gradientType = "radial"; + $gradientDirectionSvg = 'cx="50%" cy="50%" r="75%"'; + $rectangleDimension = 'x="-50" y="-50" width="101" height="101"'; + break; + default: + throw new Less_Exception_Compiler( "svg-gradient direction must be 'to bottom', 'to right', 'to bottom right', 'to top right' or 'ellipse at center'" ); + } + + $returner = '' . + '' . + '<' . $gradientType . 'Gradient id="gradient" gradientUnits="userSpaceOnUse" ' . $gradientDirectionSvg . '>'; + + for ( $i = 0; $i < count( $stops ); $i++ ) { + if ( is_object( $stops[$i] ) && property_exists( $stops[$i], 'value' ) ) { + $color = $stops[$i]->value[0]; + $position = $stops[$i]->value[1]; + } else { + $color = $stops[$i]; + $position = null; + } + + if ( !( $color instanceof Less_Tree_Color ) || ( !( ( $i === 0 || $i + 1 === count( $stops ) ) && $position === null ) && !( $position instanceof Less_Tree_Dimension ) ) ) { + throw new Less_Exception_Compiler( $throw_message ); + } + if ( $position ) { + $positionValue = $position->toCSS(); + } elseif ( $i === 0 ) { + $positionValue = '0%'; + } else { + $positionValue = '100%'; + } + $alpha = $color->alpha; + $returner .= ''; + } + + $returner .= ''; + + if ( $useBase64 ) { + $returner = "'data:image/svg+xml;base64,".base64_encode( $returner )."'"; + } else { + $returner = "'data:image/svg+xml,".$returner."'"; + } + + return new Less_Tree_URL( new Less_Tree_Anonymous( $returner ) ); + } + + /** + * Php version of javascript's `encodeURIComponent` function + * + * @param string $string The string to encode + * @return string The encoded string + */ + public static function encodeURIComponent( $string ) { + $revert = array( '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' ); + return strtr( rawurlencode( $string ), $revert ); + } + + // Color Blending + // ref: http://www.w3.org/TR/compositing-1 + + public function colorBlend( $mode, $color1, $color2 ) { + $ab = $color1->alpha; // backdrop + $as = $color2->alpha; // source + $r = array(); // result + + $ar = $as + $ab * ( 1 - $as ); + for ( $i = 0; $i < 3; $i++ ) { + $cb = $color1->rgb[$i] / 255; + $cs = $color2->rgb[$i] / 255; + $cr = call_user_func( $mode, $cb, $cs ); + if ( $ar ) { + $cr = ( $as * $cs + $ab * ( $cb - $as * ( $cb + $cs - $cr ) ) ) / $ar; + } + $r[$i] = $cr * 255; + } + + return new Less_Tree_Color( $r, $ar ); + } + + public function multiply( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to multiply must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to multiply must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendMultiply' ), $color1, $color2 ); + } + + private function colorBlendMultiply( $cb, $cs ) { + return $cb * $cs; + } + + public function screen( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to screen must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to screen must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendScreen' ), $color1, $color2 ); + } + + private function colorBlendScreen( $cb, $cs ) { + return $cb + $cs - $cb * $cs; + } + + public function overlay( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to overlay must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to overlay must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendOverlay' ), $color1, $color2 ); + } + + private function colorBlendOverlay( $cb, $cs ) { + $cb *= 2; + return ( $cb <= 1 ) + ? $this->colorBlendMultiply( $cb, $cs ) + : $this->colorBlendScreen( $cb - 1, $cs ); + } + + public function softlight( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to softlight must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to softlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendSoftlight' ), $color1, $color2 ); + } + + private function colorBlendSoftlight( $cb, $cs ) { + $d = 1; + $e = $cb; + if ( $cs > 0.5 ) { + $e = 1; + $d = ( $cb > 0.25 ) ? sqrt( $cb ) + : ( ( 16 * $cb - 12 ) * $cb + 4 ) * $cb; + } + return $cb - ( 1 - 2 * $cs ) * $e * ( $d - $cb ); + } + + public function hardlight( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to hardlight must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to hardlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendHardlight' ), $color1, $color2 ); + } + + private function colorBlendHardlight( $cb, $cs ) { + return $this->colorBlendOverlay( $cs, $cb ); + } + + public function difference( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to difference must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to difference must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendDifference' ), $color1, $color2 ); + } + + private function colorBlendDifference( $cb, $cs ) { + return abs( $cb - $cs ); + } + + public function exclusion( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to exclusion must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to exclusion must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendExclusion' ), $color1, $color2 ); + } + + private function colorBlendExclusion( $cb, $cs ) { + return $cb + $cs - 2 * $cb * $cs; + } + + public function average( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to average must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to average must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendAverage' ), $color1, $color2 ); + } + + // non-w3c functions: + public function colorBlendAverage( $cb, $cs ) { + return ( $cb + $cs ) / 2; + } + + public function negation( $color1 = null, $color2 = null ) { + if ( !$color1 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The first argument to negation must be a color' . ( $color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + if ( !$color2 instanceof Less_Tree_Color ) { + throw new Less_Exception_Compiler( 'The second argument to negation must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); + } + + return $this->colorBlend( array( $this,'colorBlendNegation' ), $color1, $color2 ); + } + + public function colorBlendNegation( $cb, $cs ) { + return 1 - abs( $cb + $cs - 1 ); + } + + // ~ End of Color Blending + +} diff --git a/less.php/lib/Less/Less.php.combine b/less.php/lib/Less/Less.php.combine new file mode 100755 index 0000000..d63cc78 --- /dev/null +++ b/less.php/lib/Less/Less.php.combine @@ -0,0 +1,17 @@ + +./Parser.php +./Colors.php +./Environment.php +./Functions.php +./Mime.php +./Tree.php +./Output.php +./Visitor.php +./VisitorReplacing.php +./Configurable.php +./Tree +./Visitor +./Exception/Parser.php +./Exception/ +./Output +./SourceMap diff --git a/less.php/lib/Less/Mime.php b/less.php/lib/Less/Mime.php new file mode 100755 index 0000000..b4723f9 --- /dev/null +++ b/less.php/lib/Less/Mime.php @@ -0,0 +1,41 @@ + 'text/html', + '.html' => 'text/html', + '.gif' => 'image/gif', + '.jpg' => 'image/jpeg', + '.jpeg' => 'image/jpeg', + '.png' => 'image/png', + '.ttf' => 'application/x-font-ttf', + '.otf' => 'application/x-font-otf', + '.eot' => 'application/vnd.ms-fontobject', + '.woff' => 'application/x-font-woff', + '.svg' => 'image/svg+xml', + ); + + public static function lookup( $filepath ) { + $parts = explode( '.', $filepath ); + $ext = '.'.strtolower( array_pop( $parts ) ); + + if ( !isset( self::$_types[$ext] ) ) { + return null; + } + return self::$_types[$ext]; + } + + public static function charsets_lookup( $type = null ) { + // assumes all text types are UTF-8 + return $type && preg_match( '/^text\//', $type ) ? 'UTF-8' : ''; + } +} diff --git a/less.php/lib/Less/Output.php b/less.php/lib/Less/Output.php new file mode 100755 index 0000000..8f7dd61 --- /dev/null +++ b/less.php/lib/Less/Output.php @@ -0,0 +1,48 @@ +strs[] = $chunk; + } + + /** + * Is the output empty? + * + * @return boolean + */ + public function isEmpty() { + return count( $this->strs ) === 0; + } + + /** + * Converts the output to string + * + * @return string + */ + public function toString() { + return implode( '', $this->strs ); + } + +} diff --git a/less.php/lib/Less/Output/Mapped.php b/less.php/lib/Less/Output/Mapped.php new file mode 100755 index 0000000..e486c98 --- /dev/null +++ b/less.php/lib/Less/Output/Mapped.php @@ -0,0 +1,119 @@ +contentsMap = $contentsMap; + $this->generator = $generator; + } + + /** + * Adds a chunk to the stack + * The $index for less.php may be different from less.js since less.php does not chunkify inputs + * + * @param string $chunk + * @param string $fileInfo + * @param integer $index + * @param mixed $mapLines + */ + public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) { + // ignore adding empty strings + if ( $chunk === '' ) { + return; + } + + $sourceLines = array(); + $sourceColumns = ' '; + + if ( $fileInfo ) { + + $url = $fileInfo['currentUri']; + + if ( isset( $this->contentsMap[$url] ) ) { + $inputSource = substr( $this->contentsMap[$url], 0, $index ); + $sourceLines = explode( "\n", $inputSource ); + $sourceColumns = end( $sourceLines ); + } else { + throw new Exception( 'Filename '.$url.' not in contentsMap' ); + } + + } + + $lines = explode( "\n", $chunk ); + $columns = end( $lines ); + + if ( $fileInfo ) { + + if ( !$mapLines ) { + $this->generator->addMapping( + $this->lineNumber + 1, // generated_line + $this->column, // generated_column + count( $sourceLines ), // original_line + strlen( $sourceColumns ), // original_column + $fileInfo + ); + } else { + for ( $i = 0, $count = count( $lines ); $i < $count; $i++ ) { + $this->generator->addMapping( + $this->lineNumber + $i + 1, // generated_line + $i === 0 ? $this->column : 0, // generated_column + count( $sourceLines ) + $i, // original_line + $i === 0 ? strlen( $sourceColumns ) : 0, // original_column + $fileInfo + ); + } + } + } + + if ( count( $lines ) === 1 ) { + $this->column += strlen( $columns ); + } else { + $this->lineNumber += count( $lines ) - 1; + $this->column = strlen( $columns ); + } + + // add only chunk + parent::add( $chunk ); + } + +} diff --git a/less.php/lib/Less/Parser.php b/less.php/lib/Less/Parser.php new file mode 100755 index 0000000..36e0b3e --- /dev/null +++ b/less.php/lib/Less/Parser.php @@ -0,0 +1,2691 @@ + false, // option - whether to compress + 'strictUnits' => false, // whether units need to evaluate correctly + 'strictMath' => false, // whether math has to be within parenthesis + 'relativeUrls' => true, // option - whether to adjust URL's to be relative + 'urlArgs' => '', // whether to add args into url tokens + 'numPrecision' => 8, + + 'import_dirs' => array(), + 'import_callback' => null, + 'cache_dir' => null, + 'cache_method' => 'php', // false, 'serialize', 'php', 'var_export', 'callback'; + 'cache_callback_get' => null, + 'cache_callback_set' => null, + + 'sourceMap' => false, // whether to output a source map + 'sourceMapBasepath' => null, + 'sourceMapWriteTo' => null, + 'sourceMapURL' => null, + + 'indentation' => ' ', + + 'plugins' => array(), + + ); + + public static $options = array(); + + private $input; // Less input string + private $input_len; // input string length + private $pos; // current index in `input` + private $saveStack = array(); // holds state for backtracking + private $furthest; + private $mb_internal_encoding = ''; // for remember exists value of mbstring.internal_encoding + + /** + * @var Less_Environment + */ + private $env; + + protected $rules = array(); + + private static $imports = array(); + + public static $has_extends = false; + + public static $next_id = 0; + + /** + * Filename to contents of all parsed the files + * + * @var array + */ + public static $contentsMap = array(); + + /** + * @param Less_Environment|array|null $env + */ + public function __construct( $env = null ) { + // Top parser on an import tree must be sure there is one "env" + // which will then be passed around by reference. + if ( $env instanceof Less_Environment ) { + $this->env = $env; + } else { + $this->SetOptions( Less_Parser::$default_options ); + $this->Reset( $env ); + } + + // mbstring.func_overload > 1 bugfix + // The encoding value must be set for each source file, + // therefore, to conserve resources and improve the speed of this design is taken here + if ( ini_get( 'mbstring.func_overload' ) ) { + $this->mb_internal_encoding = ini_get( 'mbstring.internal_encoding' ); + @ini_set( 'mbstring.internal_encoding', 'ascii' ); + } + + } + + /** + * Reset the parser state completely + * + */ + public function Reset( $options = null ) { + $this->rules = array(); + self::$imports = array(); + self::$has_extends = false; + self::$imports = array(); + self::$contentsMap = array(); + + $this->env = new Less_Environment( $options ); + + // set new options + if ( is_array( $options ) ) { + $this->SetOptions( Less_Parser::$default_options ); + $this->SetOptions( $options ); + } + + $this->env->Init(); + } + + /** + * Set one or more compiler options + * options: import_dirs, cache_dir, cache_method + * + */ + public function SetOptions( $options ) { + foreach ( $options as $option => $value ) { + $this->SetOption( $option, $value ); + } + } + + /** + * Set one compiler option + * + */ + public function SetOption( $option, $value ) { + switch ( $option ) { + + case 'import_dirs': + $this->SetImportDirs( $value ); + return; + + case 'cache_dir': + if ( is_string( $value ) ) { + Less_Cache::SetCacheDir( $value ); + Less_Cache::CheckCacheDir(); + } + return; + } + + Less_Parser::$options[$option] = $value; + } + + /** + * Registers a new custom function + * + * @param string $name function name + * @param callable $callback callback + */ + public function registerFunction( $name, $callback ) { + $this->env->functions[$name] = $callback; + } + + /** + * Removed an already registered function + * + * @param string $name function name + */ + public function unregisterFunction( $name ) { + if ( isset( $this->env->functions[$name] ) ) + unset( $this->env->functions[$name] ); + } + + /** + * Get the current css buffer + * + * @return string + */ + public function getCss() { + $precision = ini_get( 'precision' ); + @ini_set( 'precision', 16 ); + $locale = setlocale( LC_NUMERIC, 0 ); + setlocale( LC_NUMERIC, "C" ); + + try { + + $root = new Less_Tree_Ruleset( array(), $this->rules ); + $root->root = true; + $root->firstRoot = true; + + $this->PreVisitors( $root ); + + self::$has_extends = false; + $evaldRoot = $root->compile( $this->env ); + + $this->PostVisitors( $evaldRoot ); + + if ( Less_Parser::$options['sourceMap'] ) { + $generator = new Less_SourceMap_Generator( $evaldRoot, Less_Parser::$contentsMap, Less_Parser::$options ); + // will also save file + // FIXME: should happen somewhere else? + $css = $generator->generateCSS(); + } else { + $css = $evaldRoot->toCSS(); + } + + if ( Less_Parser::$options['compress'] ) { + $css = preg_replace( '/(^(\s)+)|((\s)+$)/', '', $css ); + } + + } catch ( Exception $exc ) { + // Intentional fall-through so we can reset environment + } + + // reset php settings + @ini_set( 'precision', $precision ); + setlocale( LC_NUMERIC, $locale ); + + // If you previously defined $this->mb_internal_encoding + // is required to return the encoding as it was before + if ( $this->mb_internal_encoding != '' ) { + @ini_set( "mbstring.internal_encoding", $this->mb_internal_encoding ); + $this->mb_internal_encoding = ''; + } + + // Rethrow exception after we handled resetting the environment + if ( !empty( $exc ) ) { + throw $exc; + } + + return $css; + } + + public function findValueOf( $varName ) { + foreach ( $this->rules as $rule ) { + if ( isset( $rule->variable ) && ( $rule->variable == true ) && ( str_replace( "@", "", $rule->name ) == $varName ) ) { + return $this->getVariableValue( $rule ); + } + } + return null; + } + + /** + * + * this function gets the private rules variable and returns an array of the found variables + * it uses a helper method getVariableValue() that contains the logic ot fetch the value from the rule object + * + * @return array + */ + public function getVariables() { + $variables = array(); + + $not_variable_type = array( + 'Comment', // this include less comments ( // ) and css comments (/* */) + 'Import', // do not search variables in included files @import + 'Ruleset', // selectors (.someclass, #someid, …) + 'Operation', // + ); + + // @TODO run compilation if not runned yet + foreach ( $this->rules as $key => $rule ) { + if ( in_array( $rule->type, $not_variable_type ) ) { + continue; + } + + // Note: it seems rule->type is always Rule when variable = true + if ( $rule->type == 'Rule' && $rule->variable ) { + $variables[$rule->name] = $this->getVariableValue( $rule ); + } else { + if ( $rule->type == 'Comment' ) { + $variables[] = $this->getVariableValue( $rule ); + } + } + } + return $variables; + } + + public function findVarByName( $var_name ) { + foreach ( $this->rules as $rule ) { + if ( isset( $rule->variable ) && ( $rule->variable == true ) ) { + if ( $rule->name == $var_name ) { + return $this->getVariableValue( $rule ); + } + } + } + return null; + } + + /** + * + * This method gets the value of the less variable from the rules object. + * Since the objects vary here we add the logic for extracting the css/less value. + * + * @param $var + * + * @return bool|string + */ + private function getVariableValue( $var ) { + if ( !is_a( $var, 'Less_Tree' ) ) { + throw new Exception( 'var is not a Less_Tree object' ); + } + + switch ( $var->type ) { + case 'Color': + return $this->rgb2html( $var->rgb ); + case 'Unit': + return $var->value. $var->unit->numerator[0]; + case 'Variable': + return $this->findVarByName( $var->name ); + case 'Keyword': + return $var->value; + case 'Rule': + return $this->getVariableValue( $var->value ); + case 'Value': + $value = ''; + foreach ( $var->value as $sub_value ) { + $value .= $this->getVariableValue( $sub_value ).' '; + } + return $value; + case 'Quoted': + return $var->quote.$var->value.$var->quote; + case 'Dimension': + $value = $var->value; + if ( $var->unit && $var->unit->numerator ) { + $value .= $var->unit->numerator[0]; + } + return $value; + case 'Expression': + $value = ""; + foreach ( $var->value as $item ) { + $value .= $this->getVariableValue( $item )." "; + } + return $value; + case 'Operation': + throw new Exception( 'getVariables() require Less to be compiled. please use $parser->getCss() before calling getVariables()' ); + case 'Comment': + case 'Import': + case 'Ruleset': + default: + throw new Exception( "type missing in switch/case getVariableValue for ".$var->type ); + } + return false; + } + + private function rgb2html( $r, $g = -1, $b = -1 ) { + if ( is_array( $r ) && sizeof( $r ) == 3 ) + list( $r, $g, $b ) = $r; + + $r = intval( $r ); $g = intval( $g ); + $b = intval( $b ); + + $r = dechex( $r < 0 ? 0 : ( $r > 255 ? 255 : $r ) ); + $g = dechex( $g < 0 ? 0 : ( $g > 255 ? 255 : $g ) ); + $b = dechex( $b < 0 ? 0 : ( $b > 255 ? 255 : $b ) ); + + $color = ( strlen( $r ) < 2 ? '0' : '' ).$r; + $color .= ( strlen( $g ) < 2 ? '0' : '' ).$g; + $color .= ( strlen( $b ) < 2 ? '0' : '' ).$b; + return '#'.$color; + } + + /** + * Run pre-compile visitors + * + */ + private function PreVisitors( $root ) { + if ( Less_Parser::$options['plugins'] ) { + foreach ( Less_Parser::$options['plugins'] as $plugin ) { + if ( !empty( $plugin->isPreEvalVisitor ) ) { + $plugin->run( $root ); + } + } + } + } + + /** + * Run post-compile visitors + * + */ + private function PostVisitors( $evaldRoot ) { + $visitors = array(); + $visitors[] = new Less_Visitor_joinSelector(); + if ( self::$has_extends ) { + $visitors[] = new Less_Visitor_processExtends(); + } + $visitors[] = new Less_Visitor_toCSS(); + + if ( Less_Parser::$options['plugins'] ) { + foreach ( Less_Parser::$options['plugins'] as $plugin ) { + if ( property_exists( $plugin, 'isPreEvalVisitor' ) && $plugin->isPreEvalVisitor ) { + continue; + } + + if ( property_exists( $plugin, 'isPreVisitor' ) && $plugin->isPreVisitor ) { + array_unshift( $visitors, $plugin ); + } else { + $visitors[] = $plugin; + } + } + } + + for ( $i = 0; $i < count( $visitors ); $i++ ) { + $visitors[$i]->run( $evaldRoot ); + } + + } + + /** + * Parse a Less string into css + * + * @param string $str The string to convert + * @param string $uri_root The url of the file + * @return Less_Tree_Ruleset|Less_Parser + */ + public function parse( $str, $file_uri = null ) { + if ( !$file_uri ) { + $uri_root = ''; + $filename = 'anonymous-file-'.Less_Parser::$next_id++.'.less'; + } else { + $file_uri = self::WinPath( $file_uri ); + $filename = $file_uri; + $uri_root = dirname( $file_uri ); + } + + $previousFileInfo = $this->env->currentFileInfo; + $uri_root = self::WinPath( $uri_root ); + $this->SetFileInfo( $filename, $uri_root ); + + $this->input = $str; + $this->_parse(); + + if ( $previousFileInfo ) { + $this->env->currentFileInfo = $previousFileInfo; + } + + return $this; + } + + /** + * Parse a Less string from a given file + * + * @throws Less_Exception_Parser + * @param string $filename The file to parse + * @param string $uri_root The url of the file + * @param bool $returnRoot Indicates whether the return value should be a css string a root node + * @return Less_Tree_Ruleset|Less_Parser + */ + public function parseFile( $filename, $uri_root = '', $returnRoot = false ) { + if ( !file_exists( $filename ) ) { + $this->Error( sprintf( 'File `%s` not found.', $filename ) ); + } + + // fix uri_root? + // Instead of The mixture of file path for the first argument and directory path for the second argument has bee + if ( !$returnRoot && !empty( $uri_root ) && basename( $uri_root ) == basename( $filename ) ) { + $uri_root = dirname( $uri_root ); + } + + $previousFileInfo = $this->env->currentFileInfo; + + if ( $filename ) { + $filename = self::AbsPath( $filename, true ); + } + $uri_root = self::WinPath( $uri_root ); + + $this->SetFileInfo( $filename, $uri_root ); + + self::AddParsedFile( $filename ); + + if ( $returnRoot ) { + $rules = $this->GetRules( $filename ); + $return = new Less_Tree_Ruleset( array(), $rules ); + } else { + $this->_parse( $filename ); + $return = $this; + } + + if ( $previousFileInfo ) { + $this->env->currentFileInfo = $previousFileInfo; + } + + return $return; + } + + /** + * Allows a user to set variables values + * @param array $vars + * @return Less_Parser + */ + public function ModifyVars( $vars ) { + $this->input = Less_Parser::serializeVars( $vars ); + $this->_parse(); + + return $this; + } + + /** + * @param string $filename + */ + public function SetFileInfo( $filename, $uri_root = '' ) { + $filename = Less_Environment::normalizePath( $filename ); + $dirname = preg_replace( '/[^\/\\\\]*$/', '', $filename ); + + if ( !empty( $uri_root ) ) { + $uri_root = rtrim( $uri_root, '/' ).'/'; + } + + $currentFileInfo = array(); + + // entry info + if ( isset( $this->env->currentFileInfo ) ) { + $currentFileInfo['entryPath'] = $this->env->currentFileInfo['entryPath']; + $currentFileInfo['entryUri'] = $this->env->currentFileInfo['entryUri']; + $currentFileInfo['rootpath'] = $this->env->currentFileInfo['rootpath']; + + } else { + $currentFileInfo['entryPath'] = $dirname; + $currentFileInfo['entryUri'] = $uri_root; + $currentFileInfo['rootpath'] = $dirname; + } + + $currentFileInfo['currentDirectory'] = $dirname; + $currentFileInfo['currentUri'] = $uri_root.basename( $filename ); + $currentFileInfo['filename'] = $filename; + $currentFileInfo['uri_root'] = $uri_root; + + // inherit reference + if ( isset( $this->env->currentFileInfo['reference'] ) && $this->env->currentFileInfo['reference'] ) { + $currentFileInfo['reference'] = true; + } + + $this->env->currentFileInfo = $currentFileInfo; + } + + /** + * @deprecated 1.5.1.2 + * + */ + public function SetCacheDir( $dir ) { + if ( !file_exists( $dir ) ) { + if ( mkdir( $dir ) ) { + return true; + } + throw new Less_Exception_Parser( 'Less.php cache directory couldn\'t be created: '.$dir ); + + } elseif ( !is_dir( $dir ) ) { + throw new Less_Exception_Parser( 'Less.php cache directory doesn\'t exist: '.$dir ); + + } elseif ( !is_writable( $dir ) ) { + throw new Less_Exception_Parser( 'Less.php cache directory isn\'t writable: '.$dir ); + + } else { + $dir = self::WinPath( $dir ); + Less_Cache::$cache_dir = rtrim( $dir, '/' ).'/'; + return true; + } + } + + /** + * Set a list of directories or callbacks the parser should use for determining import paths + * + * @param array $dirs + */ + public function SetImportDirs( $dirs ) { + Less_Parser::$options['import_dirs'] = array(); + + foreach ( $dirs as $path => $uri_root ) { + + $path = self::WinPath( $path ); + if ( !empty( $path ) ) { + $path = rtrim( $path, '/' ).'/'; + } + + if ( !is_callable( $uri_root ) ) { + $uri_root = self::WinPath( $uri_root ); + if ( !empty( $uri_root ) ) { + $uri_root = rtrim( $uri_root, '/' ).'/'; + } + } + + Less_Parser::$options['import_dirs'][$path] = $uri_root; + } + } + + /** + * @param string $file_path + */ + private function _parse( $file_path = null ) { + $this->rules = array_merge( $this->rules, $this->GetRules( $file_path ) ); + } + + /** + * Return the results of parsePrimary for $file_path + * Use cache and save cached results if possible + * + * @param string|null $file_path + */ + private function GetRules( $file_path ) { + $this->SetInput( $file_path ); + + $cache_file = $this->CacheFile( $file_path ); + if ( $cache_file ) { + if ( Less_Parser::$options['cache_method'] == 'callback' ) { + if ( is_callable( Less_Parser::$options['cache_callback_get'] ) ) { + $cache = call_user_func_array( + Less_Parser::$options['cache_callback_get'], + array( $this, $file_path, $cache_file ) + ); + + if ( $cache ) { + $this->UnsetInput(); + return $cache; + } + } + + } elseif ( file_exists( $cache_file ) ) { + switch ( Less_Parser::$options['cache_method'] ) { + + // Using serialize + // Faster but uses more memory + case 'serialize': + $cache = unserialize( file_get_contents( $cache_file ) ); + if ( $cache ) { + touch( $cache_file ); + $this->UnsetInput(); + return $cache; + } + break; + + // Using generated php code + case 'var_export': + case 'php': + $this->UnsetInput(); + return include $cache_file; + } + } + } + + $rules = $this->parsePrimary(); + + if ( $this->pos < $this->input_len ) { + throw new Less_Exception_Chunk( $this->input, null, $this->furthest, $this->env->currentFileInfo ); + } + + $this->UnsetInput(); + + // save the cache + if ( $cache_file ) { + if ( Less_Parser::$options['cache_method'] == 'callback' ) { + if ( is_callable( Less_Parser::$options['cache_callback_set'] ) ) { + call_user_func_array( + Less_Parser::$options['cache_callback_set'], + array( $this, $file_path, $cache_file, $rules ) + ); + } + + } else { + // msg('write cache file'); + switch ( Less_Parser::$options['cache_method'] ) { + case 'serialize': + file_put_contents( $cache_file, serialize( $rules ) ); + break; + case 'php': + file_put_contents( $cache_file, '' ); + break; + case 'var_export': + // Requires __set_state() + file_put_contents( $cache_file, '' ); + break; + } + + Less_Cache::CleanCache(); + } + } + + return $rules; + } + + /** + * Set up the input buffer + * + */ + public function SetInput( $file_path ) { + if ( $file_path ) { + $this->input = file_get_contents( $file_path ); + } + + $this->pos = $this->furthest = 0; + + // Remove potential UTF Byte Order Mark + $this->input = preg_replace( '/\\G\xEF\xBB\xBF/', '', $this->input ); + $this->input_len = strlen( $this->input ); + + if ( Less_Parser::$options['sourceMap'] && $this->env->currentFileInfo ) { + $uri = $this->env->currentFileInfo['currentUri']; + Less_Parser::$contentsMap[$uri] = $this->input; + } + + } + + /** + * Free up some memory + * + */ + public function UnsetInput() { + unset( $this->input, $this->pos, $this->input_len, $this->furthest ); + $this->saveStack = array(); + } + + public function CacheFile( $file_path ) { + if ( $file_path && $this->CacheEnabled() ) { + + $env = get_object_vars( $this->env ); + unset( $env['frames'] ); + + $parts = array(); + $parts[] = $file_path; + $parts[] = filesize( $file_path ); + $parts[] = filemtime( $file_path ); + $parts[] = $env; + $parts[] = Less_Version::cache_version; + $parts[] = Less_Parser::$options['cache_method']; + return Less_Cache::$cache_dir . Less_Cache::$prefix . base_convert( sha1( json_encode( $parts ) ), 16, 36 ) . '.lesscache'; + } + } + + static function AddParsedFile( $file ) { + self::$imports[] = $file; + } + + static function AllParsedFiles() { + return self::$imports; + } + + /** + * @param string $file + */ + static function FileParsed( $file ) { + return in_array( $file, self::$imports ); + } + + function save() { + $this->saveStack[] = $this->pos; + } + + private function restore() { + $this->pos = array_pop( $this->saveStack ); + } + + private function forget() { + array_pop( $this->saveStack ); + } + + /** + * Determine if the character at the specified offset from the current position is a white space. + * + * @param int $offset + * + * @return bool + */ + private function isWhitespace( $offset = 0 ) { + return strpos( " \t\n\r\v\f", $this->input[$this->pos + $offset] ) !== false; + } + + /** + * Parse from a token, regexp or string, and move forward if match + * + * @param array $toks + * @return array + */ + private function match( $toks ) { + // The match is confirmed, add the match length to `this::pos`, + // and consume any extra white-space characters (' ' || '\n') + // which come after that. The reason for this is that LeSS's + // grammar is mostly white-space insensitive. + // + + foreach ( $toks as $tok ) { + + $char = $tok[0]; + + if ( $char === '/' ) { + $match = $this->MatchReg( $tok ); + + if ( $match ) { + return count( $match ) === 1 ? $match[0] : $match; + } + + } elseif ( $char === '#' ) { + $match = $this->MatchChar( $tok[1] ); + + } else { + // Non-terminal, match using a function call + $match = $this->$tok(); + + } + + if ( $match ) { + return $match; + } + } + } + + /** + * @param string[] $toks + * + * @return string + */ + private function MatchFuncs( $toks ) { + if ( $this->pos < $this->input_len ) { + foreach ( $toks as $tok ) { + $match = $this->$tok(); + if ( $match ) { + return $match; + } + } + } + + } + + // Match a single character in the input, + private function MatchChar( $tok ) { + if ( ( $this->pos < $this->input_len ) && ( $this->input[$this->pos] === $tok ) ) { + $this->skipWhitespace( 1 ); + return $tok; + } + } + + // Match a regexp from the current start point + private function MatchReg( $tok ) { + if ( preg_match( $tok, $this->input, $match, 0, $this->pos ) ) { + $this->skipWhitespace( strlen( $match[0] ) ); + return $match; + } + } + + /** + * Same as match(), but don't change the state of the parser, + * just return the match. + * + * @param string $tok + * @return integer + */ + public function PeekReg( $tok ) { + return preg_match( $tok, $this->input, $match, 0, $this->pos ); + } + + /** + * @param string $tok + */ + public function PeekChar( $tok ) { + // return ($this->input[$this->pos] === $tok ); + return ( $this->pos < $this->input_len ) && ( $this->input[$this->pos] === $tok ); + } + + /** + * @param integer $length + */ + public function skipWhitespace( $length ) { + $this->pos += $length; + + for ( ; $this->pos < $this->input_len; $this->pos++ ) { + $c = $this->input[$this->pos]; + + if ( ( $c !== "\n" ) && ( $c !== "\r" ) && ( $c !== "\t" ) && ( $c !== ' ' ) ) { + break; + } + } + } + + /** + * @param string $tok + * @param string|null $msg + */ + public function expect( $tok, $msg = NULL ) { + $result = $this->match( array( $tok ) ); + if ( !$result ) { + $this->Error( $msg ? "Expected '" . $tok . "' got '" . $this->input[$this->pos] . "'" : $msg ); + } else { + return $result; + } + } + + /** + * @param string $tok + */ + public function expectChar( $tok, $msg = null ) { + $result = $this->MatchChar( $tok ); + if ( !$result ) { + $msg = $msg ? $msg : "Expected '" . $tok . "' got '" . $this->input[$this->pos] . "'"; + $this->Error( $msg ); + } else { + return $result; + } + } + + // + // Here in, the parsing rules/functions + // + // The basic structure of the syntax tree generated is as follows: + // + // Ruleset -> Rule -> Value -> Expression -> Entity + // + // Here's some LESS code: + // + // .class { + // color: #fff; + // border: 1px solid #000; + // width: @w + 4px; + // > .child {...} + // } + // + // And here's what the parse tree might look like: + // + // Ruleset (Selector '.class', [ + // Rule ("color", Value ([Expression [Color #fff]])) + // Rule ("border", Value ([Expression [Dimension 1px][Keyword "solid"][Color #000]])) + // Rule ("width", Value ([Expression [Operation "+" [Variable "@w"][Dimension 4px]]])) + // Ruleset (Selector [Element '>', '.child'], [...]) + // ]) + // + // In general, most rules will try to parse a token with the `$()` function, and if the return + // value is truly, will return a new node, of the relevant type. Sometimes, we need to check + // first, before parsing, that's when we use `peek()`. + // + + // + // The `primary` rule is the *entry* and *exit* point of the parser. + // The rules here can appear at any level of the parse tree. + // + // The recursive nature of the grammar is an interplay between the `block` + // rule, which represents `{ ... }`, the `ruleset` rule, and this `primary` rule, + // as represented by this simplified grammar: + // + // primary → (ruleset | rule)+ + // ruleset → selector+ block + // block → '{' primary '}' + // + // Only at one point is the primary rule not called from the + // block rule: at the root level. + // + private function parsePrimary() { + $root = array(); + + while ( true ) { + + if ( $this->pos >= $this->input_len ) { + break; + } + + $node = $this->parseExtend( true ); + if ( $node ) { + $root = array_merge( $root, $node ); + continue; + } + + // $node = $this->MatchFuncs( array( 'parseMixinDefinition', 'parseRule', 'parseRuleset', 'parseMixinCall', 'parseComment', 'parseDirective')); + $node = $this->MatchFuncs( array( 'parseMixinDefinition', 'parseNameValue', 'parseRule', 'parseRuleset', 'parseMixinCall', 'parseComment', 'parseRulesetCall', 'parseDirective' ) ); + + if ( $node ) { + $root[] = $node; + } elseif ( !$this->MatchReg( '/\\G[\s\n;]+/' ) ) { + break; + } + + if ( $this->PeekChar( '}' ) ) { + break; + } + } + + return $root; + } + + // We create a Comment node for CSS comments `/* */`, + // but keep the LeSS comments `//` silent, by just skipping + // over them. + private function parseComment() { + if ( $this->input[$this->pos] !== '/' ) { + return; + } + + if ( $this->input[$this->pos + 1] === '/' ) { + $match = $this->MatchReg( '/\\G\/\/.*/' ); + return $this->NewObj4( 'Less_Tree_Comment', array( $match[0], true, $this->pos, $this->env->currentFileInfo ) ); + } + + // $comment = $this->MatchReg('/\\G\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/'); + $comment = $this->MatchReg( '/\\G\/\*(?s).*?\*+\/\n?/' );// not the same as less.js to prevent fatal errors + if ( $comment ) { + return $this->NewObj4( 'Less_Tree_Comment', array( $comment[0], false, $this->pos, $this->env->currentFileInfo ) ); + } + } + + private function parseComments() { + $comments = array(); + + while ( $this->pos < $this->input_len ) { + $comment = $this->parseComment(); + if ( !$comment ) { + break; + } + + $comments[] = $comment; + } + + return $comments; + } + + // + // A string, which supports escaping " and ' + // + // "milky way" 'he\'s the one!' + // + private function parseEntitiesQuoted() { + $j = $this->pos; + $e = false; + $index = $this->pos; + + if ( $this->input[$this->pos] === '~' ) { + $j++; + $e = true; // Escaped strings + } + + $char = $this->input[$j]; + if ( $char !== '"' && $char !== "'" ) { + return; + } + + if ( $e ) { + $this->MatchChar( '~' ); + } + + $matched = $this->MatchQuoted( $char, $j + 1 ); + if ( $matched === false ) { + return; + } + + $quoted = $char.$matched.$char; + return $this->NewObj5( 'Less_Tree_Quoted', array( $quoted, $matched, $e, $index, $this->env->currentFileInfo ) ); + } + + /** + * When PCRE JIT is enabled in php, regular expressions don't work for matching quoted strings + * + * $regex = '/\\G\'((?:[^\'\\\\\r\n]|\\\\.|\\\\\r\n|\\\\[\n\r\f])*)\'/'; + * $regex = '/\\G"((?:[^"\\\\\r\n]|\\\\.|\\\\\r\n|\\\\[\n\r\f])*)"/'; + * + */ + private function MatchQuoted( $quote_char, $i ) { + $matched = ''; + while ( $i < $this->input_len ) { + $c = $this->input[$i]; + + // escaped character + if ( $c === '\\' ) { + $matched .= $c . $this->input[$i + 1]; + $i += 2; + continue; + } + + if ( $c === $quote_char ) { + $this->pos = $i + 1; + $this->skipWhitespace( 0 ); + return $matched; + } + + if ( $c === "\r" || $c === "\n" ) { + return false; + } + + $i++; + $matched .= $c; + } + + return false; + } + + // + // A catch-all word, such as: + // + // black border-collapse + // + private function parseEntitiesKeyword() { + // $k = $this->MatchReg('/\\G[_A-Za-z-][_A-Za-z0-9-]*/'); + $k = $this->MatchReg( '/\\G%|\\G[_A-Za-z-][_A-Za-z0-9-]*/' ); + if ( $k ) { + $k = $k[0]; + $color = $this->fromKeyword( $k ); + if ( $color ) { + return $color; + } + return $this->NewObj1( 'Less_Tree_Keyword', $k ); + } + } + + // duplicate of Less_Tree_Color::FromKeyword + private function FromKeyword( $keyword ) { + $keyword = strtolower( $keyword ); + + if ( Less_Colors::hasOwnProperty( $keyword ) ) { + // detect named color + return $this->NewObj1( 'Less_Tree_Color', substr( Less_Colors::color( $keyword ), 1 ) ); + } + + if ( $keyword === 'transparent' ) { + return $this->NewObj3( 'Less_Tree_Color', array( array( 0, 0, 0 ), 0, true ) ); + } + } + + // + // A function call + // + // rgb(255, 0, 255) + // + // We also try to catch IE's `alpha()`, but let the `alpha` parser + // deal with the details. + // + // The arguments are parsed with the `entities.arguments` parser. + // + private function parseEntitiesCall() { + $index = $this->pos; + + if ( !preg_match( '/\\G([\w-]+|%|progid:[\w\.]+)\(/', $this->input, $name, 0, $this->pos ) ) { + return; + } + $name = $name[1]; + $nameLC = strtolower( $name ); + + if ( $nameLC === 'url' ) { + return null; + } + + $this->pos += strlen( $name ); + + if ( $nameLC === 'alpha' ) { + $alpha_ret = $this->parseAlpha(); + if ( $alpha_ret ) { + return $alpha_ret; + } + } + + $this->MatchChar( '(' ); // Parse the '(' and consume whitespace. + + $args = $this->parseEntitiesArguments(); + + if ( !$this->MatchChar( ')' ) ) { + return; + } + + if ( $name ) { + return $this->NewObj4( 'Less_Tree_Call', array( $name, $args, $index, $this->env->currentFileInfo ) ); + } + } + + /** + * Parse a list of arguments + * + * @return array + */ + private function parseEntitiesArguments() { + $args = array(); + while ( true ) { + $arg = $this->MatchFuncs( array( 'parseEntitiesAssignment','parseExpression' ) ); + if ( !$arg ) { + break; + } + + $args[] = $arg; + if ( !$this->MatchChar( ',' ) ) { + break; + } + } + return $args; + } + + private function parseEntitiesLiteral() { + return $this->MatchFuncs( array( 'parseEntitiesDimension','parseEntitiesColor','parseEntitiesQuoted','parseUnicodeDescriptor' ) ); + } + + // Assignments are argument entities for calls. + // They are present in ie filter properties as shown below. + // + // filter: progid:DXImageTransform.Microsoft.Alpha( *opacity=50* ) + // + private function parseEntitiesAssignment() { + $key = $this->MatchReg( '/\\G\w+(?=\s?=)/' ); + if ( !$key ) { + return; + } + + if ( !$this->MatchChar( '=' ) ) { + return; + } + + $value = $this->parseEntity(); + if ( $value ) { + return $this->NewObj2( 'Less_Tree_Assignment', array( $key[0], $value ) ); + } + } + + // + // Parse url() tokens + // + // We use a specific rule for urls, because they don't really behave like + // standard function calls. The difference is that the argument doesn't have + // to be enclosed within a string, so it can't be parsed as an Expression. + // + private function parseEntitiesUrl() { + if ( $this->input[$this->pos] !== 'u' || !$this->matchReg( '/\\Gurl\(/' ) ) { + return; + } + + $value = $this->match( array( 'parseEntitiesQuoted','parseEntitiesVariable','/\\Gdata\:.*?[^\)]+/','/\\G(?:(?:\\\\[\(\)\'"])|[^\(\)\'"])+/' ) ); + if ( !$value ) { + $value = ''; + } + + $this->expectChar( ')' ); + + if ( isset( $value->value ) || $value instanceof Less_Tree_Variable ) { + return $this->NewObj2( 'Less_Tree_Url', array( $value, $this->env->currentFileInfo ) ); + } + + return $this->NewObj2( 'Less_Tree_Url', array( $this->NewObj1( 'Less_Tree_Anonymous', $value ), $this->env->currentFileInfo ) ); + } + + // + // A Variable entity, such as `@fink`, in + // + // width: @fink + 2px + // + // We use a different parser for variable definitions, + // see `parsers.variable`. + // + private function parseEntitiesVariable() { + $index = $this->pos; + if ( $this->PeekChar( '@' ) && ( $name = $this->MatchReg( '/\\G@@?[\w-]+/' ) ) ) { + return $this->NewObj3( 'Less_Tree_Variable', array( $name[0], $index, $this->env->currentFileInfo ) ); + } + } + + // A variable entity using the protective {} e.g. @{var} + private function parseEntitiesVariableCurly() { + $index = $this->pos; + + if ( $this->input_len > ( $this->pos + 1 ) && $this->input[$this->pos] === '@' && ( $curly = $this->MatchReg( '/\\G@\{([\w-]+)\}/' ) ) ) { + return $this->NewObj3( 'Less_Tree_Variable', array( '@'.$curly[1], $index, $this->env->currentFileInfo ) ); + } + } + + // + // A Hexadecimal color + // + // #4F3C2F + // + // `rgb` and `hsl` colors are parsed through the `entities.call` parser. + // + private function parseEntitiesColor() { + if ( $this->PeekChar( '#' ) && ( $rgb = $this->MatchReg( '/\\G#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/' ) ) ) { + return $this->NewObj1( 'Less_Tree_Color', $rgb[1] ); + } + } + + // + // A Dimension, that is, a number and a unit + // + // 0.5em 95% + // + private function parseEntitiesDimension() { + $c = @ord( $this->input[$this->pos] ); + + // Is the first char of the dimension 0-9, '.', '+' or '-' + if ( ( $c > 57 || $c < 43 ) || $c === 47 || $c == 44 ) { + return; + } + + $value = $this->MatchReg( '/\\G([+-]?\d*\.?\d+)(%|[a-z]+)?/' ); + if ( $value ) { + + if ( isset( $value[2] ) ) { + return $this->NewObj2( 'Less_Tree_Dimension', array( $value[1],$value[2] ) ); + } + return $this->NewObj1( 'Less_Tree_Dimension', $value[1] ); + } + } + + // + // A unicode descriptor, as is used in unicode-range + // + // U+0?? or U+00A1-00A9 + // + function parseUnicodeDescriptor() { + $ud = $this->MatchReg( '/\\G(U\+[0-9a-fA-F?]+)(\-[0-9a-fA-F?]+)?/' ); + if ( $ud ) { + return $this->NewObj1( 'Less_Tree_UnicodeDescriptor', $ud[0] ); + } + } + + // + // JavaScript code to be evaluated + // + // `window.location.href` + // + private function parseEntitiesJavascript() { + $e = false; + $j = $this->pos; + if ( $this->input[$j] === '~' ) { + $j++; + $e = true; + } + if ( $this->input[$j] !== '`' ) { + return; + } + if ( $e ) { + $this->MatchChar( '~' ); + } + $str = $this->MatchReg( '/\\G`([^`]*)`/' ); + if ( $str ) { + return $this->NewObj3( 'Less_Tree_Javascript', array( $str[1], $this->pos, $e ) ); + } + } + + // + // The variable part of a variable definition. Used in the `rule` parser + // + // @fink: + // + private function parseVariable() { + if ( $this->PeekChar( '@' ) && ( $name = $this->MatchReg( '/\\G(@[\w-]+)\s*:/' ) ) ) { + return $name[1]; + } + } + + // + // The variable part of a variable definition. Used in the `rule` parser + // + // @fink(); + // + private function parseRulesetCall() { + if ( $this->input[$this->pos] === '@' && ( $name = $this->MatchReg( '/\\G(@[\w-]+)\s*\(\s*\)\s*;/' ) ) ) { + return $this->NewObj1( 'Less_Tree_RulesetCall', $name[1] ); + } + } + + // + // extend syntax - used to extend selectors + // + function parseExtend( $isRule = false ) { + $index = $this->pos; + $extendList = array(); + + if ( !$this->MatchReg( $isRule ? '/\\G&:extend\(/' : '/\\G:extend\(/' ) ) { return; + } + + do{ + $option = null; + $elements = array(); + while ( true ) { + $option = $this->MatchReg( '/\\G(all)(?=\s*(\)|,))/' ); + if ( $option ) { break; + } + $e = $this->parseElement(); + if ( !$e ) { break; + } + $elements[] = $e; + } + + if ( $option ) { + $option = $option[1]; + } + + $extendList[] = $this->NewObj3( 'Less_Tree_Extend', array( $this->NewObj1( 'Less_Tree_Selector', $elements ), $option, $index ) ); + + }while ( $this->MatchChar( "," ) ); + + $this->expect( '/\\G\)/' ); + + if ( $isRule ) { + $this->expect( '/\\G;/' ); + } + + return $extendList; + } + + // + // A Mixin call, with an optional argument list + // + // #mixins > .square(#fff); + // .rounded(4px, black); + // .button; + // + // The `while` loop is there because mixins can be + // namespaced, but we only support the child and descendant + // selector for now. + // + private function parseMixinCall() { + $char = $this->input[$this->pos]; + if ( $char !== '.' && $char !== '#' ) { + return; + } + + $index = $this->pos; + $this->save(); // stop us absorbing part of an invalid selector + + $elements = $this->parseMixinCallElements(); + + if ( $elements ) { + + if ( $this->MatchChar( '(' ) ) { + $returned = $this->parseMixinArgs( true ); + $args = $returned['args']; + $this->expectChar( ')' ); + } else { + $args = array(); + } + + $important = $this->parseImportant(); + + if ( $this->parseEnd() ) { + $this->forget(); + return $this->NewObj5( 'Less_Tree_Mixin_Call', array( $elements, $args, $index, $this->env->currentFileInfo, $important ) ); + } + } + + $this->restore(); + } + + private function parseMixinCallElements() { + $elements = array(); + $c = null; + + while ( true ) { + $elemIndex = $this->pos; + $e = $this->MatchReg( '/\\G[#.](?:[\w-]|\\\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/' ); + if ( !$e ) { + break; + } + $elements[] = $this->NewObj4( 'Less_Tree_Element', array( $c, $e[0], $elemIndex, $this->env->currentFileInfo ) ); + $c = $this->MatchChar( '>' ); + } + + return $elements; + } + + /** + * @param boolean $isCall + */ + private function parseMixinArgs( $isCall ) { + $expressions = array(); + $argsSemiColon = array(); + $isSemiColonSeperated = null; + $argsComma = array(); + $expressionContainsNamed = null; + $name = null; + $returner = array( 'args' => array(), 'variadic' => false ); + + $this->save(); + + while ( true ) { + if ( $isCall ) { + $arg = $this->MatchFuncs( array( 'parseDetachedRuleset','parseExpression' ) ); + } else { + $this->parseComments(); + if ( $this->input[ $this->pos ] === '.' && $this->MatchReg( '/\\G\.{3}/' ) ) { + $returner['variadic'] = true; + if ( $this->MatchChar( ";" ) && !$isSemiColonSeperated ) { + $isSemiColonSeperated = true; + } + + if ( $isSemiColonSeperated ) { + $argsSemiColon[] = array( 'variadic' => true ); + } else { + $argsComma[] = array( 'variadic' => true ); + } + break; + } + $arg = $this->MatchFuncs( array( 'parseEntitiesVariable','parseEntitiesLiteral','parseEntitiesKeyword' ) ); + } + + if ( !$arg ) { + break; + } + + $nameLoop = null; + if ( $arg instanceof Less_Tree_Expression ) { + $arg->throwAwayComments(); + } + $value = $arg; + $val = null; + + if ( $isCall ) { + // Variable + if ( property_exists( $arg, 'value' ) && count( $arg->value ) == 1 ) { + $val = $arg->value[0]; + } + } else { + $val = $arg; + } + + if ( $val instanceof Less_Tree_Variable ) { + + if ( $this->MatchChar( ':' ) ) { + if ( $expressions ) { + if ( $isSemiColonSeperated ) { + $this->Error( 'Cannot mix ; and , as delimiter types' ); + } + $expressionContainsNamed = true; + } + + // we do not support setting a ruleset as a default variable - it doesn't make sense + // However if we do want to add it, there is nothing blocking it, just don't error + // and remove isCall dependency below + $value = null; + if ( $isCall ) { + $value = $this->parseDetachedRuleset(); + } + if ( !$value ) { + $value = $this->parseExpression(); + } + + if ( !$value ) { + if ( $isCall ) { + $this->Error( 'could not understand value for named argument' ); + } else { + $this->restore(); + $returner['args'] = array(); + return $returner; + } + } + + $nameLoop = ( $name = $val->name ); + } elseif ( !$isCall && $this->MatchReg( '/\\G\.{3}/' ) ) { + $returner['variadic'] = true; + if ( $this->MatchChar( ";" ) && !$isSemiColonSeperated ) { + $isSemiColonSeperated = true; + } + if ( $isSemiColonSeperated ) { + $argsSemiColon[] = array( 'name' => $arg->name, 'variadic' => true ); + } else { + $argsComma[] = array( 'name' => $arg->name, 'variadic' => true ); + } + break; + } elseif ( !$isCall ) { + $name = $nameLoop = $val->name; + $value = null; + } + } + + if ( $value ) { + $expressions[] = $value; + } + + $argsComma[] = array( 'name' => $nameLoop, 'value' => $value ); + + if ( $this->MatchChar( ',' ) ) { + continue; + } + + if ( $this->MatchChar( ';' ) || $isSemiColonSeperated ) { + + if ( $expressionContainsNamed ) { + $this->Error( 'Cannot mix ; and , as delimiter types' ); + } + + $isSemiColonSeperated = true; + + if ( count( $expressions ) > 1 ) { + $value = $this->NewObj1( 'Less_Tree_Value', $expressions ); + } + $argsSemiColon[] = array( 'name' => $name, 'value' => $value ); + + $name = null; + $expressions = array(); + $expressionContainsNamed = false; + } + } + + $this->forget(); + $returner['args'] = ( $isSemiColonSeperated ? $argsSemiColon : $argsComma ); + return $returner; + } + + // + // A Mixin definition, with a list of parameters + // + // .rounded (@radius: 2px, @color) { + // ... + // } + // + // Until we have a finer grained state-machine, we have to + // do a look-ahead, to make sure we don't have a mixin call. + // See the `rule` function for more information. + // + // We start by matching `.rounded (`, and then proceed on to + // the argument list, which has optional default values. + // We store the parameters in `params`, with a `value` key, + // if there is a value, such as in the case of `@radius`. + // + // Once we've got our params list, and a closing `)`, we parse + // the `{...}` block. + // + private function parseMixinDefinition() { + $cond = null; + + $char = $this->input[$this->pos]; + if ( ( $char !== '.' && $char !== '#' ) || ( $char === '{' && $this->PeekReg( '/\\G[^{]*\}/' ) ) ) { + return; + } + + $this->save(); + + $match = $this->MatchReg( '/\\G([#.](?:[\w-]|\\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+)\s*\(/' ); + if ( $match ) { + $name = $match[1]; + + $argInfo = $this->parseMixinArgs( false ); + $params = $argInfo['args']; + $variadic = $argInfo['variadic']; + + // .mixincall("@{a}"); + // looks a bit like a mixin definition.. + // also + // .mixincall(@a: {rule: set;}); + // so we have to be nice and restore + if ( !$this->MatchChar( ')' ) ) { + $this->furthest = $this->pos; + $this->restore(); + return; + } + + $this->parseComments(); + + if ( $this->MatchReg( '/\\Gwhen/' ) ) { // Guard + $cond = $this->expect( 'parseConditions', 'Expected conditions' ); + } + + $ruleset = $this->parseBlock(); + + if ( is_array( $ruleset ) ) { + $this->forget(); + return $this->NewObj5( 'Less_Tree_Mixin_Definition', array( $name, $params, $ruleset, $cond, $variadic ) ); + } + + $this->restore(); + } else { + $this->forget(); + } + } + + // + // Entities are the smallest recognized token, + // and can be found inside a rule's value. + // + private function parseEntity() { + return $this->MatchFuncs( array( 'parseEntitiesLiteral','parseEntitiesVariable','parseEntitiesUrl','parseEntitiesCall','parseEntitiesKeyword','parseEntitiesJavascript','parseComment' ) ); + } + + // + // A Rule terminator. Note that we use `peek()` to check for '}', + // because the `block` rule will be expecting it, but we still need to make sure + // it's there, if ';' was omitted. + // + private function parseEnd() { + return $this->MatchChar( ';' ) || $this->PeekChar( '}' ); + } + + // + // IE's alpha function + // + // alpha(opacity=88) + // + private function parseAlpha() { + if ( !$this->MatchReg( '/\\G\(opacity=/i' ) ) { + return; + } + + $value = $this->MatchReg( '/\\G[0-9]+/' ); + if ( $value ) { + $value = $value[0]; + } else { + $value = $this->parseEntitiesVariable(); + if ( !$value ) { + return; + } + } + + $this->expectChar( ')' ); + return $this->NewObj1( 'Less_Tree_Alpha', $value ); + } + + // + // A Selector Element + // + // div + // + h1 + // #socks + // input[type="text"] + // + // Elements are the building blocks for Selectors, + // they are made out of a `Combinator` (see combinator rule), + // and an element name, such as a tag a class, or `*`. + // + private function parseElement() { + $c = $this->parseCombinator(); + $index = $this->pos; + + $e = $this->match( array( '/\\G(?:\d+\.\d+|\d+)%/', '/\\G(?:[.#]?|:*)(?:[\w-]|[^\x00-\x9f]|\\\\(?:[A-Fa-f0-9]{1,6} ?|[^A-Fa-f0-9]))+/', + '#*', '#&', 'parseAttribute', '/\\G\([^()@]+\)/', '/\\G[\.#](?=@)/', 'parseEntitiesVariableCurly' ) ); + + if ( is_null( $e ) ) { + $this->save(); + if ( $this->MatchChar( '(' ) ) { + if ( ( $v = $this->parseSelector() ) && $this->MatchChar( ')' ) ) { + $e = $this->NewObj1( 'Less_Tree_Paren', $v ); + $this->forget(); + } else { + $this->restore(); + } + } else { + $this->forget(); + } + } + + if ( !is_null( $e ) ) { + return $this->NewObj4( 'Less_Tree_Element', array( $c, $e, $index, $this->env->currentFileInfo ) ); + } + } + + // + // Combinators combine elements together, in a Selector. + // + // Because our parser isn't white-space sensitive, special care + // has to be taken, when parsing the descendant combinator, ` `, + // as it's an empty space. We have to check the previous character + // in the input, to see if it's a ` ` character. + // + private function parseCombinator() { + if ( $this->pos < $this->input_len ) { + $c = $this->input[$this->pos]; + if ( $c === '>' || $c === '+' || $c === '~' || $c === '|' || $c === '^' ) { + + $this->pos++; + if ( $this->input[$this->pos] === '^' ) { + $c = '^^'; + $this->pos++; + } + + $this->skipWhitespace( 0 ); + + return $c; + } + + if ( $this->pos > 0 && $this->isWhitespace( -1 ) ) { + return ' '; + } + } + } + + // + // A CSS selector (see selector below) + // with less extensions e.g. the ability to extend and guard + // + private function parseLessSelector() { + return $this->parseSelector( true ); + } + + // + // A CSS Selector + // + // .class > div + h1 + // li a:hover + // + // Selectors are made out of one or more Elements, see above. + // + private function parseSelector( $isLess = false ) { + $elements = array(); + $extendList = array(); + $condition = null; + $when = false; + $extend = false; + $e = null; + $c = null; + $index = $this->pos; + + while ( ( $isLess && ( $extend = $this->parseExtend() ) ) || ( $isLess && ( $when = $this->MatchReg( '/\\Gwhen/' ) ) ) || ( $e = $this->parseElement() ) ) { + if ( $when ) { + $condition = $this->expect( 'parseConditions', 'expected condition' ); + } elseif ( $condition ) { + // error("CSS guard can only be used at the end of selector"); + } elseif ( $extend ) { + $extendList = array_merge( $extendList, $extend ); + } else { + // if( count($extendList) ){ + //error("Extend can only be used at the end of selector"); + //} + if ( $this->pos < $this->input_len ) { + $c = $this->input[ $this->pos ]; + } + $elements[] = $e; + $e = null; + } + + if ( $c === '{' || $c === '}' || $c === ';' || $c === ',' || $c === ')' ) { break; + } + } + + if ( $elements ) { + return $this->NewObj5( 'Less_Tree_Selector', array( $elements, $extendList, $condition, $index, $this->env->currentFileInfo ) ); + } + if ( $extendList ) { + $this->Error( 'Extend must be used to extend a selector, it cannot be used on its own' ); + } + } + + private function parseTag() { + return ( $tag = $this->MatchReg( '/\\G[A-Za-z][A-Za-z-]*[0-9]?/' ) ) ? $tag : $this->MatchChar( '*' ); + } + + private function parseAttribute() { + $val = null; + + if ( !$this->MatchChar( '[' ) ) { + return; + } + + $key = $this->parseEntitiesVariableCurly(); + if ( !$key ) { + $key = $this->expect( '/\\G(?:[_A-Za-z0-9-\*]*\|)?(?:[_A-Za-z0-9-]|\\\\.)+/' ); + } + + $op = $this->MatchReg( '/\\G[|~*$^]?=/' ); + if ( $op ) { + $val = $this->match( array( 'parseEntitiesQuoted','/\\G[0-9]+%/','/\\G[\w-]+/','parseEntitiesVariableCurly' ) ); + } + + $this->expectChar( ']' ); + + return $this->NewObj3( 'Less_Tree_Attribute', array( $key, $op === null ? null : $op[0], $val ) ); + } + + // + // The `block` rule is used by `ruleset` and `mixin.definition`. + // It's a wrapper around the `primary` rule, with added `{}`. + // + private function parseBlock() { + if ( $this->MatchChar( '{' ) ) { + $content = $this->parsePrimary(); + if ( $this->MatchChar( '}' ) ) { + return $content; + } + } + } + + private function parseBlockRuleset() { + $block = $this->parseBlock(); + + if ( $block ) { + $block = $this->NewObj2( 'Less_Tree_Ruleset', array( null, $block ) ); + } + + return $block; + } + + private function parseDetachedRuleset() { + $blockRuleset = $this->parseBlockRuleset(); + if ( $blockRuleset ) { + return $this->NewObj1( 'Less_Tree_DetachedRuleset', $blockRuleset ); + } + } + + // + // div, .class, body > p {...} + // + private function parseRuleset() { + $selectors = array(); + + $this->save(); + + while ( true ) { + $s = $this->parseLessSelector(); + if ( !$s ) { + break; + } + $selectors[] = $s; + $this->parseComments(); + + if ( $s->condition && count( $selectors ) > 1 ) { + $this->Error( 'Guards are only currently allowed on a single selector.' ); + } + + if ( !$this->MatchChar( ',' ) ) { + break; + } + if ( $s->condition ) { + $this->Error( 'Guards are only currently allowed on a single selector.' ); + } + $this->parseComments(); + } + + if ( $selectors ) { + $rules = $this->parseBlock(); + if ( is_array( $rules ) ) { + $this->forget(); + return $this->NewObj2( 'Less_Tree_Ruleset', array( $selectors, $rules ) ); // Less_Environment::$strictImports + } + } + + // Backtrack + $this->furthest = $this->pos; + $this->restore(); + } + + /** + * Custom less.php parse function for finding simple name-value css pairs + * ex: width:100px; + * + */ + private function parseNameValue() { + $index = $this->pos; + $this->save(); + + // $match = $this->MatchReg('/\\G([a-zA-Z\-]+)\s*:\s*((?:\'")?[a-zA-Z0-9\-% \.,!]+?(?:\'")?)\s*([;}])/'); + $match = $this->MatchReg( '/\\G([a-zA-Z\-]+)\s*:\s*([\'"]?[#a-zA-Z0-9\-%\.,]+?[\'"]?) *(! *important)?\s*([;}])/' ); + if ( $match ) { + + if ( $match[4] == '}' ) { + $this->pos = $index + strlen( $match[0] ) - 1; + } + + if ( $match[3] ) { + $match[2] .= ' !important'; + } + + return $this->NewObj4( 'Less_Tree_NameValue', array( $match[1], $match[2], $index, $this->env->currentFileInfo ) ); + } + + $this->restore(); + } + + private function parseRule( $tryAnonymous = null ) { + $merge = false; + $startOfRule = $this->pos; + + $c = $this->input[$this->pos]; + if ( $c === '.' || $c === '#' || $c === '&' ) { + return; + } + + $this->save(); + $name = $this->MatchFuncs( array( 'parseVariable','parseRuleProperty' ) ); + + if ( $name ) { + + $isVariable = is_string( $name ); + + $value = null; + if ( $isVariable ) { + $value = $this->parseDetachedRuleset(); + } + + $important = null; + if ( !$value ) { + + // prefer to try to parse first if its a variable or we are compressing + // but always fallback on the other one + //if( !$tryAnonymous && is_string($name) && $name[0] === '@' ){ + if ( !$tryAnonymous && ( Less_Parser::$options['compress'] || $isVariable ) ) { + $value = $this->MatchFuncs( array( 'parseValue','parseAnonymousValue' ) ); + } else { + $value = $this->MatchFuncs( array( 'parseAnonymousValue','parseValue' ) ); + } + + $important = $this->parseImportant(); + + // a name returned by this.ruleProperty() is always an array of the form: + // [string-1, ..., string-n, ""] or [string-1, ..., string-n, "+"] + // where each item is a tree.Keyword or tree.Variable + if ( !$isVariable && is_array( $name ) ) { + $nm = array_pop( $name ); + if ( $nm->value ) { + $merge = $nm->value; + } + } + } + + if ( $value && $this->parseEnd() ) { + $this->forget(); + return $this->NewObj6( 'Less_Tree_Rule', array( $name, $value, $important, $merge, $startOfRule, $this->env->currentFileInfo ) ); + } else { + $this->furthest = $this->pos; + $this->restore(); + if ( $value && !$tryAnonymous ) { + return $this->parseRule( true ); + } + } + } else { + $this->forget(); + } + } + + function parseAnonymousValue() { + if ( preg_match( '/\\G([^@+\/\'"*`(;{}-]*);/', $this->input, $match, 0, $this->pos ) ) { + $this->pos += strlen( $match[1] ); + return $this->NewObj1( 'Less_Tree_Anonymous', $match[1] ); + } + } + + // + // An @import directive + // + // @import "lib"; + // + // Depending on our environment, importing is done differently: + // In the browser, it's an XHR request, in Node, it would be a + // file-system operation. The function used for importing is + // stored in `import`, which we pass to the Import constructor. + // + private function parseImport() { + $this->save(); + + $dir = $this->MatchReg( '/\\G@import?\s+/' ); + + if ( $dir ) { + $options = $this->parseImportOptions(); + $path = $this->MatchFuncs( array( 'parseEntitiesQuoted','parseEntitiesUrl' ) ); + + if ( $path ) { + $features = $this->parseMediaFeatures(); + if ( $this->MatchChar( ';' ) ) { + if ( $features ) { + $features = $this->NewObj1( 'Less_Tree_Value', $features ); + } + + $this->forget(); + return $this->NewObj5( 'Less_Tree_Import', array( $path, $features, $options, $this->pos, $this->env->currentFileInfo ) ); + } + } + } + + $this->restore(); + } + + private function parseImportOptions() { + $options = array(); + + // list of options, surrounded by parens + if ( !$this->MatchChar( '(' ) ) { + return $options; + } + do{ + $optionName = $this->parseImportOption(); + if ( $optionName ) { + $value = true; + switch ( $optionName ) { + case "css": + $optionName = "less"; + $value = false; + break; + case "once": + $optionName = "multiple"; + $value = false; + break; + } + $options[$optionName] = $value; + if ( !$this->MatchChar( ',' ) ) { break; + } + } + }while ( $optionName ); + $this->expectChar( ')' ); + return $options; + } + + private function parseImportOption() { + $opt = $this->MatchReg( '/\\G(less|css|multiple|once|inline|reference|optional)/' ); + if ( $opt ) { + return $opt[1]; + } + } + + private function parseMediaFeature() { + $nodes = array(); + + do{ + $e = $this->MatchFuncs( array( 'parseEntitiesKeyword','parseEntitiesVariable' ) ); + if ( $e ) { + $nodes[] = $e; + } elseif ( $this->MatchChar( '(' ) ) { + $p = $this->parseProperty(); + $e = $this->parseValue(); + if ( $this->MatchChar( ')' ) ) { + if ( $p && $e ) { + $r = $this->NewObj7( 'Less_Tree_Rule', array( $p, $e, null, null, $this->pos, $this->env->currentFileInfo, true ) ); + $nodes[] = $this->NewObj1( 'Less_Tree_Paren', $r ); + } elseif ( $e ) { + $nodes[] = $this->NewObj1( 'Less_Tree_Paren', $e ); + } else { + return null; + } + } else return null; + } + } while ( $e ); + + if ( $nodes ) { + return $this->NewObj1( 'Less_Tree_Expression', $nodes ); + } + } + + private function parseMediaFeatures() { + $features = array(); + + do{ + $e = $this->parseMediaFeature(); + if ( $e ) { + $features[] = $e; + if ( !$this->MatchChar( ',' ) ) break; + } else { + $e = $this->parseEntitiesVariable(); + if ( $e ) { + $features[] = $e; + if ( !$this->MatchChar( ',' ) ) break; + } + } + } while ( $e ); + + return $features ? $features : null; + } + + private function parseMedia() { + if ( $this->MatchReg( '/\\G@media/' ) ) { + $features = $this->parseMediaFeatures(); + $rules = $this->parseBlock(); + + if ( is_array( $rules ) ) { + return $this->NewObj4( 'Less_Tree_Media', array( $rules, $features, $this->pos, $this->env->currentFileInfo ) ); + } + } + } + + // + // A CSS Directive + // + // @charset "utf-8"; + // + private function parseDirective() { + if ( !$this->PeekChar( '@' ) ) { + return; + } + + $rules = null; + $index = $this->pos; + $hasBlock = true; + $hasIdentifier = false; + $hasExpression = false; + $hasUnknown = false; + + $value = $this->MatchFuncs( array( 'parseImport','parseMedia' ) ); + if ( $value ) { + return $value; + } + + $this->save(); + + $name = $this->MatchReg( '/\\G@[a-z-]+/' ); + + if ( !$name ) return; + $name = $name[0]; + + $nonVendorSpecificName = $name; + $pos = strpos( $name, '-', 2 ); + if ( $name[1] == '-' && $pos > 0 ) { + $nonVendorSpecificName = "@" . substr( $name, $pos + 1 ); + } + + switch ( $nonVendorSpecificName ) { + /* + case "@font-face": + case "@viewport": + case "@top-left": + case "@top-left-corner": + case "@top-center": + case "@top-right": + case "@top-right-corner": + case "@bottom-left": + case "@bottom-left-corner": + case "@bottom-center": + case "@bottom-right": + case "@bottom-right-corner": + case "@left-top": + case "@left-middle": + case "@left-bottom": + case "@right-top": + case "@right-middle": + case "@right-bottom": + hasBlock = true; + break; + */ + case "@charset": + $hasIdentifier = true; + $hasBlock = false; + break; + case "@namespace": + $hasExpression = true; + $hasBlock = false; + break; + case "@keyframes": + $hasIdentifier = true; + break; + case "@host": + case "@page": + case "@document": + case "@supports": + $hasUnknown = true; + break; + } + + if ( $hasIdentifier ) { + $value = $this->parseEntity(); + if ( !$value ) { + $this->error( "expected " . $name . " identifier" ); + } + } else if ( $hasExpression ) { + $value = $this->parseExpression(); + if ( !$value ) { + $this->error( "expected " . $name. " expression" ); + } + } else if ( $hasUnknown ) { + + $value = $this->MatchReg( '/\\G[^{;]+/' ); + if ( $value ) { + $value = $this->NewObj1( 'Less_Tree_Anonymous', trim( $value[0] ) ); + } + } + + if ( $hasBlock ) { + $rules = $this->parseBlockRuleset(); + } + + if ( $rules || ( !$hasBlock && $value && $this->MatchChar( ';' ) ) ) { + $this->forget(); + return $this->NewObj5( 'Less_Tree_Directive', array( $name, $value, $rules, $index, $this->env->currentFileInfo ) ); + } + + $this->restore(); + } + + // + // A Value is a comma-delimited list of Expressions + // + // font-family: Baskerville, Georgia, serif; + // + // In a Rule, a Value represents everything after the `:`, + // and before the `;`. + // + private function parseValue() { + $expressions = array(); + + do{ + $e = $this->parseExpression(); + if ( $e ) { + $expressions[] = $e; + if ( !$this->MatchChar( ',' ) ) { + break; + } + } + }while ( $e ); + + if ( $expressions ) { + return $this->NewObj1( 'Less_Tree_Value', $expressions ); + } + } + + private function parseImportant() { + if ( $this->PeekChar( '!' ) && $this->MatchReg( '/\\G! *important/' ) ) { + return ' !important'; + } + } + + private function parseSub() { + if ( $this->MatchChar( '(' ) ) { + $a = $this->parseAddition(); + if ( $a ) { + $this->expectChar( ')' ); + return $this->NewObj2( 'Less_Tree_Expression', array( array( $a ), true ) ); // instead of $e->parens = true so the value is cached + } + } + } + + /** + * Parses multiplication operation + * + * @return Less_Tree_Operation|null + */ + function parseMultiplication() { + $return = $m = $this->parseOperand(); + if ( $return ) { + while ( true ) { + + $isSpaced = $this->isWhitespace( -1 ); + + if ( $this->PeekReg( '/\\G\/[*\/]/' ) ) { + break; + } + + $op = $this->MatchChar( '/' ); + if ( !$op ) { + $op = $this->MatchChar( '*' ); + if ( !$op ) { + break; + } + } + + $a = $this->parseOperand(); + + if ( !$a ) { break; + } + + $m->parensInOp = true; + $a->parensInOp = true; + $return = $this->NewObj3( 'Less_Tree_Operation', array( $op, array( $return, $a ), $isSpaced ) ); + } + } + return $return; + + } + + /** + * Parses an addition operation + * + * @return Less_Tree_Operation|null + */ + private function parseAddition() { + $return = $m = $this->parseMultiplication(); + if ( $return ) { + while ( true ) { + + $isSpaced = $this->isWhitespace( -1 ); + + $op = $this->MatchReg( '/\\G[-+]\s+/' ); + if ( $op ) { + $op = $op[0]; + } else { + if ( !$isSpaced ) { + $op = $this->match( array( '#+','#-' ) ); + } + if ( !$op ) { + break; + } + } + + $a = $this->parseMultiplication(); + if ( !$a ) { + break; + } + + $m->parensInOp = true; + $a->parensInOp = true; + $return = $this->NewObj3( 'Less_Tree_Operation', array( $op, array( $return, $a ), $isSpaced ) ); + } + } + + return $return; + } + + /** + * Parses the conditions + * + * @return Less_Tree_Condition|null + */ + private function parseConditions() { + $index = $this->pos; + $return = $a = $this->parseCondition(); + if ( $a ) { + while ( true ) { + if ( !$this->PeekReg( '/\\G,\s*(not\s*)?\(/' ) || !$this->MatchChar( ',' ) ) { + break; + } + $b = $this->parseCondition(); + if ( !$b ) { + break; + } + + $return = $this->NewObj4( 'Less_Tree_Condition', array( 'or', $return, $b, $index ) ); + } + return $return; + } + } + + private function parseCondition() { + $index = $this->pos; + $negate = false; + $c = null; + + if ( $this->MatchReg( '/\\Gnot/' ) ) $negate = true; + $this->expectChar( '(' ); + $a = $this->MatchFuncs( array( 'parseAddition','parseEntitiesKeyword','parseEntitiesQuoted' ) ); + + if ( $a ) { + $op = $this->MatchReg( '/\\G(?:>=|<=|=<|[<=>])/' ); + if ( $op ) { + $b = $this->MatchFuncs( array( 'parseAddition','parseEntitiesKeyword','parseEntitiesQuoted' ) ); + if ( $b ) { + $c = $this->NewObj5( 'Less_Tree_Condition', array( $op[0], $a, $b, $index, $negate ) ); + } else { + $this->Error( 'Unexpected expression' ); + } + } else { + $k = $this->NewObj1( 'Less_Tree_Keyword', 'true' ); + $c = $this->NewObj5( 'Less_Tree_Condition', array( '=', $a, $k, $index, $negate ) ); + } + $this->expectChar( ')' ); + return $this->MatchReg( '/\\Gand/' ) ? $this->NewObj3( 'Less_Tree_Condition', array( 'and', $c, $this->parseCondition() ) ) : $c; + } + } + + /** + * An operand is anything that can be part of an operation, + * such as a Color, or a Variable + * + */ + private function parseOperand() { + $negate = false; + $offset = $this->pos + 1; + if ( $offset >= $this->input_len ) { + return; + } + $char = $this->input[$offset]; + if ( $char === '@' || $char === '(' ) { + $negate = $this->MatchChar( '-' ); + } + + $o = $this->MatchFuncs( array( 'parseSub','parseEntitiesDimension','parseEntitiesColor','parseEntitiesVariable','parseEntitiesCall' ) ); + + if ( $negate ) { + $o->parensInOp = true; + $o = $this->NewObj1( 'Less_Tree_Negative', $o ); + } + + return $o; + } + + /** + * Expressions either represent mathematical operations, + * or white-space delimited Entities. + * + * 1px solid black + * @var * 2 + * + * @return Less_Tree_Expression|null + */ + private function parseExpression() { + $entities = array(); + + do{ + $e = $this->MatchFuncs( array( 'parseAddition','parseEntity' ) ); + if ( $e ) { + $entities[] = $e; + // operations do not allow keyword "/" dimension (e.g. small/20px) so we support that here + if ( !$this->PeekReg( '/\\G\/[\/*]/' ) ) { + $delim = $this->MatchChar( '/' ); + if ( $delim ) { + $entities[] = $this->NewObj1( 'Less_Tree_Anonymous', $delim ); + } + } + } + }while ( $e ); + + if ( $entities ) { + return $this->NewObj1( 'Less_Tree_Expression', $entities ); + } + } + + /** + * Parse a property + * eg: 'min-width', 'orientation', etc + * + * @return string + */ + private function parseProperty() { + $name = $this->MatchReg( '/\\G(\*?-?[_a-zA-Z0-9-]+)\s*:/' ); + if ( $name ) { + return $name[1]; + } + } + + /** + * Parse a rule property + * eg: 'color', 'width', 'height', etc + * + * @return string + */ + private function parseRuleProperty() { + $offset = $this->pos; + $name = array(); + $index = array(); + $length = 0; + + $this->rulePropertyMatch( '/\\G(\*?)/', $offset, $length, $index, $name ); + while ( $this->rulePropertyMatch( '/\\G((?:[\w-]+)|(?:@\{[\w-]+\}))/', $offset, $length, $index, $name ) ); // ! + + if ( ( count( $name ) > 1 ) && $this->rulePropertyMatch( '/\\G\s*((?:\+_|\+)?)\s*:/', $offset, $length, $index, $name ) ) { + // at last, we have the complete match now. move forward, + // convert name particles to tree objects and return: + $this->skipWhitespace( $length ); + + if ( $name[0] === '' ) { + array_shift( $name ); + array_shift( $index ); + } + foreach ( $name as $k => $s ) { + if ( !$s || $s[0] !== '@' ) { + $name[$k] = $this->NewObj1( 'Less_Tree_Keyword', $s ); + } else { + $name[$k] = $this->NewObj3( 'Less_Tree_Variable', array( '@' . substr( $s, 2, -1 ), $index[$k], $this->env->currentFileInfo ) ); + } + } + return $name; + } + + } + + private function rulePropertyMatch( $re, &$offset, &$length, &$index, &$name ) { + preg_match( $re, $this->input, $a, 0, $offset ); + if ( $a ) { + $index[] = $this->pos + $length; + $length += strlen( $a[0] ); + $offset += strlen( $a[0] ); + $name[] = $a[1]; + return true; + } + } + + public static function serializeVars( $vars ) { + $s = ''; + + foreach ( $vars as $name => $value ) { + $s .= ( ( $name[0] === '@' ) ? '' : '@' ) . $name .': '. $value . ( ( substr( $value, -1 ) === ';' ) ? '' : ';' ); + } + + return $s; + } + + /** + * Some versions of php have trouble with method_exists($a,$b) if $a is not an object + * + * @param string $b + */ + public static function is_method( $a, $b ) { + return is_object( $a ) && method_exists( $a, $b ); + } + + /** + * Round numbers similarly to javascript + * eg: 1.499999 to 1 instead of 2 + * + */ + public static function round( $i, $precision = 0 ) { + $precision = pow( 10, $precision ); + $i = $i * $precision; + + $ceil = ceil( $i ); + $floor = floor( $i ); + if ( ( $ceil - $i ) <= ( $i - $floor ) ) { + return $ceil / $precision; + } else { + return $floor / $precision; + } + } + + /** + * Create Less_Tree_* objects and optionally generate a cache string + * + * @return mixed + */ + public function NewObj0( $class ) { + $obj = new $class(); + if ( $this->CacheEnabled() ) { + $obj->cache_string = ' new '.$class.'()'; + } + return $obj; + } + + public function NewObj1( $class, $arg ) { + $obj = new $class( $arg ); + if ( $this->CacheEnabled() ) { + $obj->cache_string = ' new '.$class.'('.Less_Parser::ArgString( $arg ).')'; + } + return $obj; + } + + public function NewObj2( $class, $args ) { + $obj = new $class( $args[0], $args[1] ); + if ( $this->CacheEnabled() ) { + $this->ObjCache( $obj, $class, $args ); + } + return $obj; + } + + public function NewObj3( $class, $args ) { + $obj = new $class( $args[0], $args[1], $args[2] ); + if ( $this->CacheEnabled() ) { + $this->ObjCache( $obj, $class, $args ); + } + return $obj; + } + + public function NewObj4( $class, $args ) { + $obj = new $class( $args[0], $args[1], $args[2], $args[3] ); + if ( $this->CacheEnabled() ) { + $this->ObjCache( $obj, $class, $args ); + } + return $obj; + } + + public function NewObj5( $class, $args ) { + $obj = new $class( $args[0], $args[1], $args[2], $args[3], $args[4] ); + if ( $this->CacheEnabled() ) { + $this->ObjCache( $obj, $class, $args ); + } + return $obj; + } + + public function NewObj6( $class, $args ) { + $obj = new $class( $args[0], $args[1], $args[2], $args[3], $args[4], $args[5] ); + if ( $this->CacheEnabled() ) { + $this->ObjCache( $obj, $class, $args ); + } + return $obj; + } + + public function NewObj7( $class, $args ) { + $obj = new $class( $args[0], $args[1], $args[2], $args[3], $args[4], $args[5], $args[6] ); + if ( $this->CacheEnabled() ) { + $this->ObjCache( $obj, $class, $args ); + } + return $obj; + } + + // caching + public function ObjCache( $obj, $class, $args = array() ) { + $obj->cache_string = ' new '.$class.'('. self::ArgCache( $args ).')'; + } + + public function ArgCache( $args ) { + return implode( ',', array_map( array( 'Less_Parser','ArgString' ), $args ) ); + } + + /** + * Convert an argument to a string for use in the parser cache + * + * @return string + */ + public static function ArgString( $arg ) { + $type = gettype( $arg ); + + if ( $type === 'object' ) { + $string = $arg->cache_string; + unset( $arg->cache_string ); + return $string; + + } elseif ( $type === 'array' ) { + $string = ' Array('; + foreach ( $arg as $k => $a ) { + $string .= var_export( $k, true ).' => '.self::ArgString( $a ).','; + } + return $string . ')'; + } + + return var_export( $arg, true ); + } + + public function Error( $msg ) { + throw new Less_Exception_Parser( $msg, null, $this->furthest, $this->env->currentFileInfo ); + } + + public static function WinPath( $path ) { + return str_replace( '\\', '/', $path ); + } + + public static function AbsPath( $path, $winPath = false ) { + if ( strpos( $path, '//' ) !== false && preg_match( '_^(https?:)?//\\w+(\\.\\w+)+/\\w+_i', $path ) ) { + return $winPath ? '' : false; + } else { + $path = realpath( $path ); + if ( $winPath ) { + $path = self::WinPath( $path ); + } + return $path; + } + } + + public function CacheEnabled() { + return ( Less_Parser::$options['cache_method'] && ( Less_Cache::$cache_dir || ( Less_Parser::$options['cache_method'] == 'callback' ) ) ); + } + +} diff --git a/less.php/lib/Less/SourceMap/Base64VLQ.php b/less.php/lib/Less/SourceMap/Base64VLQ.php new file mode 100755 index 0000000..989a4ce --- /dev/null +++ b/less.php/lib/Less/SourceMap/Base64VLQ.php @@ -0,0 +1,187 @@ + 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, + 'H' => 7,'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, + 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, + 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25, 'a' => 26, 'b' => 27, + 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, 'g' => 32, 'h' => 33, 'i' => 34, + 'j' => 35, 'k' => 36, 'l' => 37, 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, + 'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, + 'x' => 49, 'y' => 50, 'z' => 51, 0 => 52, 1 => 53, 2 => 54, 3 => 55, 4 => 56, + 5 => 57, 6 => 58, 7 => 59, 8 => 60, 9 => 61, '+' => 62, '/' => 63, + ); + + /** + * Integer to char map + * + * @var array + */ + private $intToCharMap = array( + 0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', + 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', + 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U', + 21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z', 26 => 'a', 27 => 'b', + 28 => 'c', 29 => 'd', 30 => 'e', 31 => 'f', 32 => 'g', 33 => 'h', 34 => 'i', + 35 => 'j', 36 => 'k', 37 => 'l', 38 => 'm', 39 => 'n', 40 => 'o', 41 => 'p', + 42 => 'q', 43 => 'r', 44 => 's', 45 => 't', 46 => 'u', 47 => 'v', 48 => 'w', + 49 => 'x', 50 => 'y', 51 => 'z', 52 => '0', 53 => '1', 54 => '2', 55 => '3', + 56 => '4', 57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '+', + 63 => '/', + ); + + /** + * Constructor + */ + public function __construct() { + // I leave it here for future reference + // foreach(str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') as $i => $char) + // { + // $this->charToIntMap[$char] = $i; + // $this->intToCharMap[$i] = $char; + // } + } + + /** + * Convert from a two-complement value to a value where the sign bit is + * is placed in the least significant bit. For example, as decimals: + * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) + * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) + * We generate the value for 32 bit machines, hence -2147483648 becomes 1, not 4294967297, + * even on a 64 bit machine. + * @param string $aValue + */ + public function toVLQSigned( $aValue ) { + return 0xffffffff & ( $aValue < 0 ? ( ( -$aValue ) << 1 ) + 1 : ( $aValue << 1 ) + 0 ); + } + + /** + * Convert to a two-complement value from a value where the sign bit is + * is placed in the least significant bit. For example, as decimals: + * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 + * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 + * We assume that the value was generated with a 32 bit machine in mind. + * Hence + * 1 becomes -2147483648 + * even on a 64 bit machine. + * @param integer $aValue + */ + public function fromVLQSigned( $aValue ) { + return $aValue & 1 ? $this->zeroFill( ~$aValue + 2, 1 ) | ( -1 - 0x7fffffff ) : $this->zeroFill( $aValue, 1 ); + } + + /** + * Return the base 64 VLQ encoded value. + * + * @param string $aValue The value to encode + * @return string The encoded value + */ + public function encode( $aValue ) { + $encoded = ''; + $vlq = $this->toVLQSigned( $aValue ); + do + { + $digit = $vlq & $this->mask; + $vlq = $this->zeroFill( $vlq, $this->shift ); + if ( $vlq > 0 ) { + $digit |= $this->continuationBit; + } + $encoded .= $this->base64Encode( $digit ); + } while ( $vlq > 0 ); + + return $encoded; + } + + /** + * Return the value decoded from base 64 VLQ. + * + * @param string $encoded The encoded value to decode + * @return integer The decoded value + */ + public function decode( $encoded ) { + $vlq = 0; + $i = 0; + do + { + $digit = $this->base64Decode( $encoded[$i] ); + $vlq |= ( $digit & $this->mask ) << ( $i * $this->shift ); + $i++; + } while ( $digit & $this->continuationBit ); + + return $this->fromVLQSigned( $vlq ); + } + + /** + * Right shift with zero fill. + * + * @param integer $a number to shift + * @param integer $b number of bits to shift + * @return integer + */ + public function zeroFill( $a, $b ) { + return ( $a >= 0 ) ? ( $a >> $b ) : ( $a >> $b ) & ( PHP_INT_MAX >> ( $b - 1 ) ); + } + + /** + * Encode single 6-bit digit as base64. + * + * @param integer $number + * @return string + * @throws Exception If the number is invalid + */ + public function base64Encode( $number ) { + if ( $number < 0 || $number > 63 ) { + throw new Exception( sprintf( 'Invalid number "%s" given. Must be between 0 and 63.', $number ) ); + } + return $this->intToCharMap[$number]; + } + + /** + * Decode single 6-bit digit from base64 + * + * @param string $char + * @return number + * @throws Exception If the number is invalid + */ + public function base64Decode( $char ) { + if ( !array_key_exists( $char, $this->charToIntMap ) ) { + throw new Exception( sprintf( 'Invalid base 64 digit "%s" given.', $char ) ); + } + return $this->charToIntMap[$char]; + } + +} diff --git a/less.php/lib/Less/SourceMap/Generator.php b/less.php/lib/Less/SourceMap/Generator.php new file mode 100755 index 0000000..eded501 --- /dev/null +++ b/less.php/lib/Less/SourceMap/Generator.php @@ -0,0 +1,354 @@ + '', + + // an optional name of the generated code that this source map is associated with. + 'sourceMapFilename' => null, + + // url of the map + 'sourceMapURL' => null, + + // absolute path to a file to write the map to + 'sourceMapWriteTo' => null, + + // output source contents? + 'outputSourceFiles' => false, + + // base path for filename normalization + 'sourceMapRootpath' => '', + + // base path for filename normalization + 'sourceMapBasepath' => '' + ); + + /** + * The base64 VLQ encoder + * + * @var Less_SourceMap_Base64VLQ + */ + protected $encoder; + + /** + * Array of mappings + * + * @var array + */ + protected $mappings = array(); + + /** + * The root node + * + * @var Less_Tree_Ruleset + */ + protected $root; + + /** + * Array of contents map + * + * @var array + */ + protected $contentsMap = array(); + + /** + * File to content map + * + * @var array + */ + protected $sources = array(); + protected $source_keys = array(); + + /** + * Constructor + * + * @param Less_Tree_Ruleset $root The root node + * @param array $options Array of options + */ + public function __construct( Less_Tree_Ruleset $root, $contentsMap, $options = array() ) { + $this->root = $root; + $this->contentsMap = $contentsMap; + $this->encoder = new Less_SourceMap_Base64VLQ(); + + $this->SetOptions( $options ); + + $this->options['sourceMapRootpath'] = $this->fixWindowsPath( $this->options['sourceMapRootpath'], true ); + $this->options['sourceMapBasepath'] = $this->fixWindowsPath( $this->options['sourceMapBasepath'], true ); + } + + /** + * Generates the CSS + * + * @return string + */ + public function generateCSS() { + $output = new Less_Output_Mapped( $this->contentsMap, $this ); + + // catch the output + $this->root->genCSS( $output ); + + $sourceMapUrl = $this->getOption( 'sourceMapURL' ); + $sourceMapFilename = $this->getOption( 'sourceMapFilename' ); + $sourceMapContent = $this->generateJson(); + $sourceMapWriteTo = $this->getOption( 'sourceMapWriteTo' ); + + if ( !$sourceMapUrl && $sourceMapFilename ) { + $sourceMapUrl = $this->normalizeFilename( $sourceMapFilename ); + } + + // write map to a file + if ( $sourceMapWriteTo ) { + $this->saveMap( $sourceMapWriteTo, $sourceMapContent ); + } + + // inline the map + if ( !$sourceMapUrl ) { + $sourceMapUrl = sprintf( 'data:application/json,%s', Less_Functions::encodeURIComponent( $sourceMapContent ) ); + } + + if ( $sourceMapUrl ) { + $output->add( sprintf( '/*# sourceMappingURL=%s */', $sourceMapUrl ) ); + } + + return $output->toString(); + } + + /** + * Saves the source map to a file + * + * @param string $file The absolute path to a file + * @param string $content The content to write + * @throws Exception If the file could not be saved + */ + protected function saveMap( $file, $content ) { + $dir = dirname( $file ); + // directory does not exist + if ( !is_dir( $dir ) ) { + // FIXME: create the dir automatically? + throw new Exception( sprintf( 'The directory "%s" does not exist. Cannot save the source map.', $dir ) ); + } + // FIXME: proper saving, with dir write check! + if ( file_put_contents( $file, $content ) === false ) { + throw new Exception( sprintf( 'Cannot save the source map to "%s"', $file ) ); + } + return true; + } + + /** + * Normalizes the filename + * + * @param string $filename + * @return string + */ + protected function normalizeFilename( $filename ) { + $filename = $this->fixWindowsPath( $filename ); + + $rootpath = $this->getOption( 'sourceMapRootpath' ); + $basePath = $this->getOption( 'sourceMapBasepath' ); + + // "Trim" the 'sourceMapBasepath' from the output filename. + if ( is_string( $basePath ) && strpos( $filename, $basePath ) === 0 ) { + $filename = substr( $filename, strlen( $basePath ) ); + } + + // Remove extra leading path separators. + if ( strpos( $filename, '\\' ) === 0 || strpos( $filename, '/' ) === 0 ) { + $filename = substr( $filename, 1 ); + } + + return $rootpath . $filename; + } + + /** + * Adds a mapping + * + * @param integer $generatedLine The line number in generated file + * @param integer $generatedColumn The column number in generated file + * @param integer $originalLine The line number in original file + * @param integer $originalColumn The column number in original file + * @param string $sourceFile The original source file + */ + public function addMapping( $generatedLine, $generatedColumn, $originalLine, $originalColumn, $fileInfo ) { + $this->mappings[] = array( + 'generated_line' => $generatedLine, + 'generated_column' => $generatedColumn, + 'original_line' => $originalLine, + 'original_column' => $originalColumn, + 'source_file' => $fileInfo['currentUri'] + ); + + $this->sources[$fileInfo['currentUri']] = $fileInfo['filename']; + } + + /** + * Generates the JSON source map + * + * @return string + * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# + */ + protected function generateJson() { + $sourceMap = array(); + $mappings = $this->generateMappings(); + + // File version (always the first entry in the object) and must be a positive integer. + $sourceMap['version'] = self::VERSION; + + // An optional name of the generated code that this source map is associated with. + $file = $this->getOption( 'sourceMapFilename' ); + if ( $file ) { + $sourceMap['file'] = $file; + } + + // An optional source root, useful for relocating source files on a server or removing repeated values in the 'sources' entry. This value is prepended to the individual entries in the 'source' field. + $root = $this->getOption( 'sourceRoot' ); + if ( $root ) { + $sourceMap['sourceRoot'] = $root; + } + + // A list of original sources used by the 'mappings' entry. + $sourceMap['sources'] = array(); + foreach ( $this->sources as $source_uri => $source_filename ) { + $sourceMap['sources'][] = $this->normalizeFilename( $source_filename ); + } + + // A list of symbol names used by the 'mappings' entry. + $sourceMap['names'] = array(); + + // A string with the encoded mapping data. + $sourceMap['mappings'] = $mappings; + + if ( $this->getOption( 'outputSourceFiles' ) ) { + // An optional list of source content, useful when the 'source' can't be hosted. + // The contents are listed in the same order as the sources above. + // 'null' may be used if some original sources should be retrieved by name. + $sourceMap['sourcesContent'] = $this->getSourcesContent(); + } + + // less.js compat fixes + if ( count( $sourceMap['sources'] ) && empty( $sourceMap['sourceRoot'] ) ) { + unset( $sourceMap['sourceRoot'] ); + } + + return json_encode( $sourceMap ); + } + + /** + * Returns the sources contents + * + * @return array|null + */ + protected function getSourcesContent() { + if ( empty( $this->sources ) ) { + return; + } + $content = array(); + foreach ( $this->sources as $sourceFile ) { + $content[] = file_get_contents( $sourceFile ); + } + return $content; + } + + /** + * Generates the mappings string + * + * @return string + */ + public function generateMappings() { + if ( !count( $this->mappings ) ) { + return ''; + } + + $this->source_keys = array_flip( array_keys( $this->sources ) ); + + // group mappings by generated line number. + $groupedMap = $groupedMapEncoded = array(); + foreach ( $this->mappings as $m ) { + $groupedMap[$m['generated_line']][] = $m; + } + ksort( $groupedMap ); + + $lastGeneratedLine = $lastOriginalIndex = $lastOriginalLine = $lastOriginalColumn = 0; + + foreach ( $groupedMap as $lineNumber => $line_map ) { + while ( ++$lastGeneratedLine < $lineNumber ) { + $groupedMapEncoded[] = ';'; + } + + $lineMapEncoded = array(); + $lastGeneratedColumn = 0; + + foreach ( $line_map as $m ) { + $mapEncoded = $this->encoder->encode( $m['generated_column'] - $lastGeneratedColumn ); + $lastGeneratedColumn = $m['generated_column']; + + // find the index + if ( $m['source_file'] ) { + $index = $this->findFileIndex( $m['source_file'] ); + if ( $index !== false ) { + $mapEncoded .= $this->encoder->encode( $index - $lastOriginalIndex ); + $lastOriginalIndex = $index; + + // lines are stored 0-based in SourceMap spec version 3 + $mapEncoded .= $this->encoder->encode( $m['original_line'] - 1 - $lastOriginalLine ); + $lastOriginalLine = $m['original_line'] - 1; + + $mapEncoded .= $this->encoder->encode( $m['original_column'] - $lastOriginalColumn ); + $lastOriginalColumn = $m['original_column']; + } + } + + $lineMapEncoded[] = $mapEncoded; + } + + $groupedMapEncoded[] = implode( ',', $lineMapEncoded ) . ';'; + } + + return rtrim( implode( $groupedMapEncoded ), ';' ); + } + + /** + * Finds the index for the filename + * + * @param string $filename + * @return integer|false + */ + protected function findFileIndex( $filename ) { + return $this->source_keys[$filename]; + } + + /** + * fix windows paths + * @param string $path + * @return string + */ + public function fixWindowsPath( $path, $addEndSlash = false ) { + $slash = ( $addEndSlash ) ? '/' : ''; + if ( !empty( $path ) ) { + $path = str_replace( '\\', '/', $path ); + $path = rtrim( $path, '/' ) . $slash; + } + + return $path; + } + +} diff --git a/less.php/lib/Less/Tree.php b/less.php/lib/Less/Tree.php new file mode 100755 index 0000000..7b48e9a --- /dev/null +++ b/less.php/lib/Less/Tree.php @@ -0,0 +1,84 @@ +genCSS( $output ); + return $output->toString(); + } + + /** + * Generate CSS by adding it to the output object + * + * @param Less_Output $output The output + * @return void + */ + public function genCSS( $output ) { + } + + /** + * @param Less_Tree_Ruleset[] $rules + */ + public static function outputRuleset( $output, $rules ) { + $ruleCnt = count( $rules ); + Less_Environment::$tabLevel++; + + // Compressed + if ( Less_Parser::$options['compress'] ) { + $output->add( '{' ); + for ( $i = 0; $i < $ruleCnt; $i++ ) { + $rules[$i]->genCSS( $output ); + } + + $output->add( '}' ); + Less_Environment::$tabLevel--; + return; + } + + // Non-compressed + $tabSetStr = "\n".str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 ); + $tabRuleStr = $tabSetStr.Less_Parser::$options['indentation']; + + $output->add( " {" ); + for ( $i = 0; $i < $ruleCnt; $i++ ) { + $output->add( $tabRuleStr ); + $rules[$i]->genCSS( $output ); + } + Less_Environment::$tabLevel--; + $output->add( $tabSetStr.'}' ); + + } + + public function accept( $visitor ) { + } + + public static function ReferencedArray( $rules ) { + foreach ( $rules as $rule ) { + if ( method_exists( $rule, 'markReferenced' ) ) { + $rule->markReferenced(); + } + } + } + + /** + * Requires php 5.3+ + */ + public static function __set_state( $args ) { + $class = get_called_class(); + $obj = new $class( null, null, null, null ); + foreach ( $args as $key => $val ) { + $obj->$key = $val; + } + return $obj; + } + +} diff --git a/less.php/lib/Less/Tree/Alpha.php b/less.php/lib/Less/Tree/Alpha.php new file mode 100755 index 0000000..cd38c91 --- /dev/null +++ b/less.php/lib/Less/Tree/Alpha.php @@ -0,0 +1,48 @@ +value = $val; + } + + // function accept( $visitor ){ + // $this->value = $visitor->visit( $this->value ); + //} + + public function compile( $env ) { + if ( is_object( $this->value ) ) { + $this->value = $this->value->compile( $env ); + } + + return $this; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( "alpha(opacity=" ); + + if ( is_string( $this->value ) ) { + $output->add( $this->value ); + } else { + $this->value->genCSS( $output ); + } + + $output->add( ')' ); + } + + public function toCSS() { + return "alpha(opacity=" . ( is_string( $this->value ) ? $this->value : $this->value->toCSS() ) . ")"; + } + +} diff --git a/less.php/lib/Less/Tree/Anonymous.php b/less.php/lib/Less/Tree/Anonymous.php new file mode 100755 index 0000000..1dc9bab --- /dev/null +++ b/less.php/lib/Less/Tree/Anonymous.php @@ -0,0 +1,58 @@ +value = $value; + $this->index = $index; + $this->mapLines = $mapLines; + $this->currentFileInfo = $currentFileInfo; + } + + public function compile() { + return new Less_Tree_Anonymous( $this->value, $this->index, $this->currentFileInfo, $this->mapLines ); + } + + public function compare( $x ) { + if ( !is_object( $x ) ) { + return -1; + } + + $left = $this->toCSS(); + $right = $x->toCSS(); + + if ( $left === $right ) { + return 0; + } + + return $left < $right ? -1 : 1; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->value, $this->currentFileInfo, $this->index, $this->mapLines ); + } + + public function toCSS() { + return $this->value; + } + +} diff --git a/less.php/lib/Less/Tree/Assignment.php b/less.php/lib/Less/Tree/Assignment.php new file mode 100755 index 0000000..69c7a4e --- /dev/null +++ b/less.php/lib/Less/Tree/Assignment.php @@ -0,0 +1,39 @@ +key = $key; + $this->value = $val; + } + + public function accept( $visitor ) { + $this->value = $visitor->visitObj( $this->value ); + } + + public function compile( $env ) { + return new Less_Tree_Assignment( $this->key, $this->value->compile( $env ) ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->key . '=' ); + $this->value->genCSS( $output ); + } + + public function toCss() { + return $this->key . '=' . $this->value->toCSS(); + } +} diff --git a/less.php/lib/Less/Tree/Attribute.php b/less.php/lib/Less/Tree/Attribute.php new file mode 100755 index 0000000..c47abb9 --- /dev/null +++ b/less.php/lib/Less/Tree/Attribute.php @@ -0,0 +1,53 @@ +key = $key; + $this->op = $op; + $this->value = $value; + } + + public function compile( $env ) { + $key_obj = is_object( $this->key ); + $val_obj = is_object( $this->value ); + + if ( !$key_obj && !$val_obj ) { + return $this; + } + + return new Less_Tree_Attribute( + $key_obj ? $this->key->compile( $env ) : $this->key, + $this->op, + $val_obj ? $this->value->compile( $env ) : $this->value ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->toCSS() ); + } + + public function toCSS() { + $value = $this->key; + + if ( $this->op ) { + $value .= $this->op; + $value .= ( is_object( $this->value ) ? $this->value->toCSS() : $this->value ); + } + + return '[' . $value . ']'; + } +} diff --git a/less.php/lib/Less/Tree/Call.php b/less.php/lib/Less/Tree/Call.php new file mode 100755 index 0000000..5341b74 --- /dev/null +++ b/less.php/lib/Less/Tree/Call.php @@ -0,0 +1,117 @@ +name = $name; + $this->args = $args; + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + } + + public function accept( $visitor ) { + $this->args = $visitor->visitArray( $this->args ); + } + + // + // When evaluating a function call, + // we either find the function in `tree.functions` [1], + // in which case we call it, passing the evaluated arguments, + // or we simply print it out as it appeared originally [2]. + // + // The *functions.js* file contains the built-in functions. + // + // The reason why we evaluate the arguments, is in the case where + // we try to pass a variable to a function, like: `saturate(@color)`. + // The function should receive the value, not the variable. + // + public function compile( $env = null ) { + $args = array(); + foreach ( $this->args as $a ) { + $args[] = $a->compile( $env ); + } + + $nameLC = strtolower( $this->name ); + switch ( $nameLC ) { + case '%': + $nameLC = '_percent'; + break; + + case 'get-unit': + $nameLC = 'getunit'; + break; + + case 'data-uri': + $nameLC = 'datauri'; + break; + + case 'svg-gradient': + $nameLC = 'svggradient'; + break; + } + + $result = null; + if ( $nameLC === 'default' ) { + $result = Less_Tree_DefaultFunc::compile(); + + } else { + + if ( method_exists( 'Less_Functions', $nameLC ) ) { // 1. + try { + + $func = new Less_Functions( $env, $this->currentFileInfo ); + $result = call_user_func_array( array( $func,$nameLC ), $args ); + + } catch ( Exception $e ) { + throw new Less_Exception_Compiler( 'error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index ); + } + } elseif ( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) { + try { + $result = call_user_func_array( $env->functions[$nameLC], $args ); + } catch ( Exception $e ) { + throw new Less_Exception_Compiler( 'error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index ); + } + } + } + + if ( $result !== null ) { + return $result; + } + + return new Less_Tree_Call( $this->name, $args, $this->index, $this->currentFileInfo ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->name . '(', $this->currentFileInfo, $this->index ); + $args_len = count( $this->args ); + for ( $i = 0; $i < $args_len; $i++ ) { + $this->args[$i]->genCSS( $output ); + if ( $i + 1 < $args_len ) { + $output->add( ', ' ); + } + } + + $output->add( ')' ); + } + + // public function toCSS(){ + // return $this->compile()->toCSS(); + //} + +} diff --git a/less.php/lib/Less/Tree/Color.php b/less.php/lib/Less/Tree/Color.php new file mode 100755 index 0000000..b0f79db --- /dev/null +++ b/less.php/lib/Less/Tree/Color.php @@ -0,0 +1,230 @@ +rgb = $rgb; + $this->alpha = $a; + $this->isTransparentKeyword = true; + return; + } + + $this->rgb = array(); + if ( is_array( $rgb ) ) { + $this->rgb = $rgb; + } else if ( strlen( $rgb ) == 6 ) { + foreach ( str_split( $rgb, 2 ) as $c ) { + $this->rgb[] = hexdec( $c ); + } + } else { + foreach ( str_split( $rgb, 1 ) as $c ) { + $this->rgb[] = hexdec( $c.$c ); + } + } + $this->alpha = is_numeric( $a ) ? $a : 1; + } + + public function compile() { + return $this; + } + + public function luma() { + $r = $this->rgb[0] / 255; + $g = $this->rgb[1] / 255; + $b = $this->rgb[2] / 255; + + $r = ( $r <= 0.03928 ) ? $r / 12.92 : pow( ( ( $r + 0.055 ) / 1.055 ), 2.4 ); + $g = ( $g <= 0.03928 ) ? $g / 12.92 : pow( ( ( $g + 0.055 ) / 1.055 ), 2.4 ); + $b = ( $b <= 0.03928 ) ? $b / 12.92 : pow( ( ( $b + 0.055 ) / 1.055 ), 2.4 ); + + return 0.2126 * $r + 0.7152 * $g + 0.0722 * $b; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->toCSS() ); + } + + public function toCSS( $doNotCompress = false ) { + $compress = Less_Parser::$options['compress'] && !$doNotCompress; + $alpha = Less_Functions::fround( $this->alpha ); + + // + // If we have some transparency, the only way to represent it + // is via `rgba`. Otherwise, we use the hex representation, + // which has better compatibility with older browsers. + // Values are capped between `0` and `255`, rounded and zero-padded. + // + if ( $alpha < 1 ) { + if ( ( $alpha === 0 || $alpha === 0.0 ) && isset( $this->isTransparentKeyword ) && $this->isTransparentKeyword ) { + return 'transparent'; + } + + $values = array(); + foreach ( $this->rgb as $c ) { + $values[] = Less_Functions::clamp( round( $c ), 255 ); + } + $values[] = $alpha; + + $glue = ( $compress ? ',' : ', ' ); + return "rgba(" . implode( $glue, $values ) . ")"; + } else { + + $color = $this->toRGB(); + + if ( $compress ) { + + // Convert color to short format + if ( $color[1] === $color[2] && $color[3] === $color[4] && $color[5] === $color[6] ) { + $color = '#'.$color[1] . $color[3] . $color[5]; + } + } + + return $color; + } + } + + // + // Operations have to be done per-channel, if not, + // channels will spill onto each other. Once we have + // our result, in the form of an integer triplet, + // we create a new Color node to hold the result. + // + + /** + * @param string $op + */ + public function operate( $op, $other ) { + $rgb = array(); + $alpha = $this->alpha * ( 1 - $other->alpha ) + $other->alpha; + for ( $c = 0; $c < 3; $c++ ) { + $rgb[$c] = Less_Functions::operate( $op, $this->rgb[$c], $other->rgb[$c] ); + } + return new Less_Tree_Color( $rgb, $alpha ); + } + + public function toRGB() { + return $this->toHex( $this->rgb ); + } + + public function toHSL() { + $r = $this->rgb[0] / 255; + $g = $this->rgb[1] / 255; + $b = $this->rgb[2] / 255; + $a = $this->alpha; + + $max = max( $r, $g, $b ); + $min = min( $r, $g, $b ); + $l = ( $max + $min ) / 2; + $d = $max - $min; + + $h = $s = 0; + if ( $max !== $min ) { + $s = $l > 0.5 ? $d / ( 2 - $max - $min ) : $d / ( $max + $min ); + + switch ( $max ) { + case $r: $h = ( $g - $b ) / $d + ( $g < $b ? 6 : 0 ); +break; + case $g: $h = ( $b - $r ) / $d + 2; +break; + case $b: $h = ( $r - $g ) / $d + 4; +break; + } + $h /= 6; + } + return array( 'h' => $h * 360, 's' => $s, 'l' => $l, 'a' => $a ); + } + + // Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript + public function toHSV() { + $r = $this->rgb[0] / 255; + $g = $this->rgb[1] / 255; + $b = $this->rgb[2] / 255; + $a = $this->alpha; + + $max = max( $r, $g, $b ); + $min = min( $r, $g, $b ); + + $v = $max; + + $d = $max - $min; + if ( $max === 0 ) { + $s = 0; + } else { + $s = $d / $max; + } + + $h = 0; + if ( $max !== $min ) { + switch ( $max ) { + case $r: $h = ( $g - $b ) / $d + ( $g < $b ? 6 : 0 ); +break; + case $g: $h = ( $b - $r ) / $d + 2; +break; + case $b: $h = ( $r - $g ) / $d + 4; +break; + } + $h /= 6; + } + return array( 'h' => $h * 360, 's' => $s, 'v' => $v, 'a' => $a ); + } + + public function toARGB() { + $argb = array_merge( (array)Less_Parser::round( $this->alpha * 255 ), $this->rgb ); + return $this->toHex( $argb ); + } + + public function compare( $x ) { + if ( !property_exists( $x, 'rgb' ) ) { + return -1; + } + + return ( $x->rgb[0] === $this->rgb[0] && + $x->rgb[1] === $this->rgb[1] && + $x->rgb[2] === $this->rgb[2] && + $x->alpha === $this->alpha ) ? 0 : -1; + } + + public function toHex( $v ) { + $ret = '#'; + foreach ( $v as $c ) { + $c = Less_Functions::clamp( Less_Parser::round( $c ), 255 ); + if ( $c < 16 ) { + $ret .= '0'; + } + $ret .= dechex( $c ); + } + + return $ret; + } + + /** + * @param string $keyword + */ + public static function fromKeyword( $keyword ) { + $keyword = strtolower( $keyword ); + + if ( Less_Colors::hasOwnProperty( $keyword ) ) { + // detect named color + return new Less_Tree_Color( substr( Less_Colors::color( $keyword ), 1 ) ); + } + + if ( $keyword === 'transparent' ) { + return new Less_Tree_Color( array( 0, 0, 0 ), 0, true ); + } + } + +} diff --git a/less.php/lib/Less/Tree/Comment.php b/less.php/lib/Less/Tree/Comment.php new file mode 100755 index 0000000..c7e6c0f --- /dev/null +++ b/less.php/lib/Less/Tree/Comment.php @@ -0,0 +1,51 @@ +value = $value; + $this->silent = !!$silent; + $this->currentFileInfo = $currentFileInfo; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + // if( $this->debugInfo ){ + //$output->add( tree.debugInfo($env, $this), $this->currentFileInfo, $this->index); + //} + $output->add( trim( $this->value ) );// TODO shouldn't need to trim, we shouldn't grab the \n + } + + public function toCSS() { + return Less_Parser::$options['compress'] ? '' : $this->value; + } + + public function isSilent() { + $isReference = ( $this->currentFileInfo && isset( $this->currentFileInfo['reference'] ) && ( !isset( $this->isReferenced ) || !$this->isReferenced ) ); + $isCompressed = Less_Parser::$options['compress'] && !preg_match( '/^\/\*!/', $this->value ); + return $this->silent || $isReference || $isCompressed; + } + + public function compile() { + return $this; + } + + public function markReferenced() { + $this->isReferenced = true; + } + +} diff --git a/less.php/lib/Less/Tree/Condition.php b/less.php/lib/Less/Tree/Condition.php new file mode 100755 index 0000000..355de8c --- /dev/null +++ b/less.php/lib/Less/Tree/Condition.php @@ -0,0 +1,72 @@ +op = trim( $op ); + $this->lvalue = $l; + $this->rvalue = $r; + $this->index = $i; + $this->negate = $negate; + } + + public function accept( $visitor ) { + $this->lvalue = $visitor->visitObj( $this->lvalue ); + $this->rvalue = $visitor->visitObj( $this->rvalue ); + } + + public function compile( $env ) { + $a = $this->lvalue->compile( $env ); + $b = $this->rvalue->compile( $env ); + + switch ( $this->op ) { + case 'and': + $result = $a && $b; + break; + + case 'or': + $result = $a || $b; + break; + + default: + if ( Less_Parser::is_method( $a, 'compare' ) ) { + $result = $a->compare( $b ); + } elseif ( Less_Parser::is_method( $b, 'compare' ) ) { + $result = $b->compare( $a ); + } else { + throw new Less_Exception_Compiler( 'Unable to perform comparison', null, $this->index ); + } + + switch ( $result ) { + case -1: + $result = $this->op === '<' || $this->op === '=<' || $this->op === '<='; + break; + + case 0: + $result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<='; + break; + + case 1: + $result = $this->op === '>' || $this->op === '>='; + break; + } + break; + } + + return $this->negate ? !$result : $result; + } + +} diff --git a/less.php/lib/Less/Tree/DefaultFunc.php b/less.php/lib/Less/Tree/DefaultFunc.php new file mode 100755 index 0000000..41b78b6 --- /dev/null +++ b/less.php/lib/Less/Tree/DefaultFunc.php @@ -0,0 +1,34 @@ +ruleset = $ruleset; + $this->frames = $frames; + } + + public function accept( $visitor ) { + $this->ruleset = $visitor->visitObj( $this->ruleset ); + } + + public function compile( $env ) { + if ( $this->frames ) { + $frames = $this->frames; + } else { + $frames = $env->frames; + } + return new Less_Tree_DetachedRuleset( $this->ruleset, $frames ); + } + + public function callEval( $env ) { + if ( $this->frames ) { + return $this->ruleset->compile( $env->copyEvalEnv( array_merge( $this->frames, $env->frames ) ) ); + } + return $this->ruleset->compile( $env ); + } +} diff --git a/less.php/lib/Less/Tree/Dimension.php b/less.php/lib/Less/Tree/Dimension.php new file mode 100755 index 0000000..70c71b4 --- /dev/null +++ b/less.php/lib/Less/Tree/Dimension.php @@ -0,0 +1,198 @@ +value = floatval( $value ); + + if ( $unit && ( $unit instanceof Less_Tree_Unit ) ) { + $this->unit = $unit; + } elseif ( $unit ) { + $this->unit = new Less_Tree_Unit( array( $unit ) ); + } else { + $this->unit = new Less_Tree_Unit(); + } + } + + public function accept( $visitor ) { + $this->unit = $visitor->visitObj( $this->unit ); + } + + public function compile() { + return $this; + } + + public function toColor() { + return new Less_Tree_Color( array( $this->value, $this->value, $this->value ) ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + if ( Less_Parser::$options['strictUnits'] && !$this->unit->isSingular() ) { + throw new Less_Exception_Compiler( "Multiple units in dimension. Correct the units or use the unit function. Bad unit: ".$this->unit->toString() ); + } + + $value = Less_Functions::fround( $this->value ); + $strValue = (string)$value; + + if ( $value !== 0 && $value < 0.000001 && $value > -0.000001 ) { + // would be output 1e-6 etc. + $strValue = number_format( $strValue, 10 ); + $strValue = preg_replace( '/\.?0+$/', '', $strValue ); + } + + if ( Less_Parser::$options['compress'] ) { + // Zero values doesn't need a unit + if ( $value === 0 && $this->unit->isLength() ) { + $output->add( $strValue ); + return $strValue; + } + + // Float values doesn't need a leading zero + if ( $value > 0 && $value < 1 && $strValue[0] === '0' ) { + $strValue = substr( $strValue, 1 ); + } + } + + $output->add( $strValue ); + $this->unit->genCSS( $output ); + } + + public function __toString() { + return $this->toCSS(); + } + + // In an operation between two Dimensions, + // we default to the first Dimension's unit, + // so `1px + 2em` will yield `3px`. + + /** + * @param string $op + */ + public function operate( $op, $other ) { + $value = Less_Functions::operate( $op, $this->value, $other->value ); + $unit = clone $this->unit; + + if ( $op === '+' || $op === '-' ) { + + if ( !$unit->numerator && !$unit->denominator ) { + $unit->numerator = $other->unit->numerator; + $unit->denominator = $other->unit->denominator; + } elseif ( !$other->unit->numerator && !$other->unit->denominator ) { + // do nothing + } else { + $other = $other->convertTo( $this->unit->usedUnits() ); + + if ( Less_Parser::$options['strictUnits'] && $other->unit->toString() !== $unit->toCSS() ) { + throw new Less_Exception_Compiler( "Incompatible units. Change the units or use the unit function. Bad units: '" . $unit->toString() . "' and " . $other->unit->toString() . "'." ); + } + + $value = Less_Functions::operate( $op, $this->value, $other->value ); + } + } elseif ( $op === '*' ) { + $unit->numerator = array_merge( $unit->numerator, $other->unit->numerator ); + $unit->denominator = array_merge( $unit->denominator, $other->unit->denominator ); + sort( $unit->numerator ); + sort( $unit->denominator ); + $unit->cancel(); + } elseif ( $op === '/' ) { + $unit->numerator = array_merge( $unit->numerator, $other->unit->denominator ); + $unit->denominator = array_merge( $unit->denominator, $other->unit->numerator ); + sort( $unit->numerator ); + sort( $unit->denominator ); + $unit->cancel(); + } + return new Less_Tree_Dimension( $value, $unit ); + } + + public function compare( $other ) { + if ( $other instanceof Less_Tree_Dimension ) { + + if ( $this->unit->isEmpty() || $other->unit->isEmpty() ) { + $a = $this; + $b = $other; + } else { + $a = $this->unify(); + $b = $other->unify(); + if ( $a->unit->compare( $b->unit ) !== 0 ) { + return -1; + } + } + $aValue = $a->value; + $bValue = $b->value; + + if ( $bValue > $aValue ) { + return -1; + } elseif ( $bValue < $aValue ) { + return 1; + } else { + return 0; + } + } else { + return -1; + } + } + + public function unify() { + return $this->convertTo( array( 'length' => 'px', 'duration' => 's', 'angle' => 'rad' ) ); + } + + public function convertTo( $conversions ) { + $value = $this->value; + $unit = clone $this->unit; + + if ( is_string( $conversions ) ) { + $derivedConversions = array(); + foreach ( Less_Tree_UnitConversions::$groups as $i ) { + if ( isset( Less_Tree_UnitConversions::${$i}[$conversions] ) ) { + $derivedConversions = array( $i => $conversions ); + } + } + $conversions = $derivedConversions; + } + + foreach ( $conversions as $groupName => $targetUnit ) { + $group = Less_Tree_UnitConversions::${$groupName}; + + // numerator + foreach ( $unit->numerator as $i => $atomicUnit ) { + $atomicUnit = $unit->numerator[$i]; + if ( !isset( $group[$atomicUnit] ) ) { + continue; + } + + $value = $value * ( $group[$atomicUnit] / $group[$targetUnit] ); + + $unit->numerator[$i] = $targetUnit; + } + + // denominator + foreach ( $unit->denominator as $i => $atomicUnit ) { + $atomicUnit = $unit->denominator[$i]; + if ( !isset( $group[$atomicUnit] ) ) { + continue; + } + + $value = $value / ( $group[$atomicUnit] / $group[$targetUnit] ); + + $unit->denominator[$i] = $targetUnit; + } + } + + $unit->cancel(); + + return new Less_Tree_Dimension( $value, $unit ); + } +} diff --git a/less.php/lib/Less/Tree/Directive.php b/less.php/lib/Less/Tree/Directive.php new file mode 100755 index 0000000..6c6a94f --- /dev/null +++ b/less.php/lib/Less/Tree/Directive.php @@ -0,0 +1,96 @@ +name = $name; + $this->value = $value; + if ( $rules ) { + $this->rules = $rules; + $this->rules->allowImports = true; + } + + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + $this->debugInfo = $debugInfo; + } + + public function accept( $visitor ) { + if ( $this->rules ) { + $this->rules = $visitor->visitObj( $this->rules ); + } + if ( $this->value ) { + $this->value = $visitor->visitObj( $this->value ); + } + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $value = $this->value; + $rules = $this->rules; + $output->add( $this->name, $this->currentFileInfo, $this->index ); + if ( $this->value ) { + $output->add( ' ' ); + $this->value->genCSS( $output ); + } + if ( $this->rules ) { + Less_Tree::outputRuleset( $output, array( $this->rules ) ); + } else { + $output->add( ';' ); + } + } + + public function compile( $env ) { + $value = $this->value; + $rules = $this->rules; + if ( $value ) { + $value = $value->compile( $env ); + } + + if ( $rules ) { + $rules = $rules->compile( $env ); + $rules->root = true; + } + + return new Less_Tree_Directive( $this->name, $value, $rules, $this->index, $this->currentFileInfo, $this->debugInfo ); + } + + public function variable( $name ) { + if ( $this->rules ) { + return $this->rules->variable( $name ); + } + } + + public function find( $selector ) { + if ( $this->rules ) { + return $this->rules->find( $selector, $this ); + } + } + + // rulesets: function () { if (this.rules) return tree.Ruleset.prototype.rulesets.apply(this.rules); }, + + public function markReferenced() { + $this->isReferenced = true; + if ( $this->rules ) { + Less_Tree::ReferencedArray( $this->rules->rules ); + } + } + +} diff --git a/less.php/lib/Less/Tree/Element.php b/less.php/lib/Less/Tree/Element.php new file mode 100755 index 0000000..5e25bc0 --- /dev/null +++ b/less.php/lib/Less/Tree/Element.php @@ -0,0 +1,70 @@ +value = $value; + $this->value_is_object = is_object( $value ); + + if ( $combinator ) { + $this->combinator = $combinator; + } + + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + } + + public function accept( $visitor ) { + if ( $this->value_is_object ) { // object or string + $this->value = $visitor->visitObj( $this->value ); + } + } + + public function compile( $env ) { + if ( Less_Environment::$mixin_stack ) { + return new Less_Tree_Element( $this->combinator, ( $this->value_is_object ? $this->value->compile( $env ) : $this->value ), $this->index, $this->currentFileInfo ); + } + + if ( $this->value_is_object ) { + $this->value = $this->value->compile( $env ); + } + + return $this; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->toCSS(), $this->currentFileInfo, $this->index ); + } + + public function toCSS() { + if ( $this->value_is_object ) { + $value = $this->value->toCSS(); + } else { + $value = $this->value; + } + + if ( $value === '' && $this->combinator && $this->combinator === '&' ) { + return ''; + } + + return Less_Environment::$_outputMap[$this->combinator] . $value; + } + +} diff --git a/less.php/lib/Less/Tree/Expression.php b/less.php/lib/Less/Tree/Expression.php new file mode 100755 index 0000000..cf06315 --- /dev/null +++ b/less.php/lib/Less/Tree/Expression.php @@ -0,0 +1,95 @@ +value = $value; + $this->parens = $parens; + } + + public function accept( $visitor ) { + $this->value = $visitor->visitArray( $this->value ); + } + + public function compile( $env ) { + $doubleParen = false; + + if ( $this->parens && !$this->parensInOp ) { + Less_Environment::$parensStack++; + } + + $returnValue = null; + if ( $this->value ) { + + $count = count( $this->value ); + + if ( $count > 1 ) { + + $ret = array(); + foreach ( $this->value as $e ) { + $ret[] = $e->compile( $env ); + } + $returnValue = new Less_Tree_Expression( $ret ); + + } else { + + if ( ( $this->value[0] instanceof Less_Tree_Expression ) && $this->value[0]->parens && !$this->value[0]->parensInOp ) { + $doubleParen = true; + } + + $returnValue = $this->value[0]->compile( $env ); + } + + } else { + $returnValue = $this; + } + + if ( $this->parens ) { + if ( !$this->parensInOp ) { + Less_Environment::$parensStack--; + + } elseif ( !Less_Environment::isMathOn() && !$doubleParen ) { + $returnValue = new Less_Tree_Paren( $returnValue ); + + } + } + return $returnValue; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $val_len = count( $this->value ); + for ( $i = 0; $i < $val_len; $i++ ) { + $this->value[$i]->genCSS( $output ); + if ( $i + 1 < $val_len ) { + $output->add( ' ' ); + } + } + } + + public function throwAwayComments() { + if ( is_array( $this->value ) ) { + $new_value = array(); + foreach ( $this->value as $v ) { + if ( $v instanceof Less_Tree_Comment ) { + continue; + } + $new_value[] = $v; + } + $this->value = $new_value; + } + } +} diff --git a/less.php/lib/Less/Tree/Extend.php b/less.php/lib/Less/Tree/Extend.php new file mode 100755 index 0000000..4dd3665 --- /dev/null +++ b/less.php/lib/Less/Tree/Extend.php @@ -0,0 +1,80 @@ +selector = $selector; + $this->option = $option; + $this->index = $index; + + switch ( $option ) { + case "all": + $this->allowBefore = true; + $this->allowAfter = true; + break; + default: + $this->allowBefore = false; + $this->allowAfter = false; + break; + } + + // This must use a string (instead of int) so that array_merge() + // preserves keys on arrays that use IDs in their keys. + $this->object_id = 'id_' . $i++; + + $this->parent_ids = array( + $this->object_id => true + ); + } + + public function accept( $visitor ) { + $this->selector = $visitor->visitObj( $this->selector ); + } + + public function compile( $env ) { + Less_Parser::$has_extends = true; + $this->selector = $this->selector->compile( $env ); + return $this; + // return new Less_Tree_Extend( $this->selector->compile($env), $this->option, $this->index); + } + + public function findSelfSelectors( $selectors ) { + $selfElements = array(); + + for ( $i = 0, $selectors_len = count( $selectors ); $i < $selectors_len; $i++ ) { + $selectorElements = $selectors[$i]->elements; + // duplicate the logic in genCSS function inside the selector node. + // future TODO - move both logics into the selector joiner visitor + if ( $i && $selectorElements && $selectorElements[0]->combinator === "" ) { + $selectorElements[0]->combinator = ' '; + } + $selfElements = array_merge( $selfElements, $selectors[$i]->elements ); + } + + $this->selfSelectors = array( new Less_Tree_Selector( $selfElements ) ); + } + +} diff --git a/less.php/lib/Less/Tree/Import.php b/less.php/lib/Less/Tree/Import.php new file mode 100755 index 0000000..ffdac64 --- /dev/null +++ b/less.php/lib/Less/Tree/Import.php @@ -0,0 +1,291 @@ +options = $options; + $this->index = $index; + $this->path = $path; + $this->features = $features; + $this->currentFileInfo = $currentFileInfo; + + if ( is_array( $options ) ) { + $this->options += array( 'inline' => false ); + + if ( isset( $this->options['less'] ) || $this->options['inline'] ) { + $this->css = !isset( $this->options['less'] ) || !$this->options['less'] || $this->options['inline']; + } else { + $pathValue = $this->getPath(); + if ( $pathValue && preg_match( '/css([\?;].*)?$/', $pathValue ) ) { + $this->css = true; + } + } + } + } + +// +// The actual import node doesn't return anything, when converted to CSS. +// The reason is that it's used at the evaluation stage, so that the rules +// it imports can be treated like any other rules. +// +// In `eval`, we make sure all Import nodes get evaluated, recursively, so +// we end up with a flat structure, which can easily be imported in the parent +// ruleset. +// + + public function accept( $visitor ) { + if ( $this->features ) { + $this->features = $visitor->visitObj( $this->features ); + } + $this->path = $visitor->visitObj( $this->path ); + + if ( !$this->options['inline'] && $this->root ) { + $this->root = $visitor->visit( $this->root ); + } + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + if ( $this->css ) { + + $output->add( '@import ', $this->currentFileInfo, $this->index ); + + $this->path->genCSS( $output ); + if ( $this->features ) { + $output->add( ' ' ); + $this->features->genCSS( $output ); + } + $output->add( ';' ); + } + } + + public function toCSS() { + $features = $this->features ? ' ' . $this->features->toCSS() : ''; + + if ( $this->css ) { + return "@import " . $this->path->toCSS() . $features . ";\n"; + } else { + return ""; + } + } + + /** + * @return string + */ + public function getPath() { + if ( $this->path instanceof Less_Tree_Quoted ) { + $path = $this->path->value; + $path = ( isset( $this->css ) || preg_match( '/(\.[a-z]*$)|([\?;].*)$/', $path ) ) ? $path : $path . '.less'; + } else if ( $this->path instanceof Less_Tree_URL ) { + $path = $this->path->value->value; + } else { + return null; + } + + // remove query string and fragment + return preg_replace( '/[\?#][^\?]*$/', '', $path ); + } + + public function compileForImport( $env ) { + return new Less_Tree_Import( $this->path->compile( $env ), $this->features, $this->options, $this->index, $this->currentFileInfo ); + } + + public function compilePath( $env ) { + $path = $this->path->compile( $env ); + $rootpath = ''; + if ( $this->currentFileInfo && $this->currentFileInfo['rootpath'] ) { + $rootpath = $this->currentFileInfo['rootpath']; + } + + if ( !( $path instanceof Less_Tree_URL ) ) { + if ( $rootpath ) { + $pathValue = $path->value; + // Add the base path if the import is relative + if ( $pathValue && Less_Environment::isPathRelative( $pathValue ) ) { + $path->value = $this->currentFileInfo['uri_root'].$pathValue; + } + } + $path->value = Less_Environment::normalizePath( $path->value ); + } + + return $path; + } + + public function compile( $env ) { + $evald = $this->compileForImport( $env ); + + // get path & uri + $path_and_uri = null; + if ( is_callable( Less_Parser::$options['import_callback'] ) ) { + $path_and_uri = call_user_func( Less_Parser::$options['import_callback'], $evald ); + } + + if ( !$path_and_uri ) { + $path_and_uri = $evald->PathAndUri(); + } + + if ( $path_and_uri ) { + list( $full_path, $uri ) = $path_and_uri; + } else { + $full_path = $uri = $evald->getPath(); + } + + // import once + if ( $evald->skip( $full_path, $env ) ) { + return array(); + } + + if ( $this->options['inline'] ) { + // todo needs to reference css file not import + //$contents = new Less_Tree_Anonymous($this->root, 0, array('filename'=>$this->importedFilename), true ); + + Less_Parser::AddParsedFile( $full_path ); + $contents = new Less_Tree_Anonymous( file_get_contents( $full_path ), 0, array(), true ); + + if ( $this->features ) { + return new Less_Tree_Media( array( $contents ), $this->features->value ); + } + + return array( $contents ); + } + + // optional (need to be before "CSS" to support optional CSS imports. CSS should be checked only if empty($this->currentFileInfo)) + if ( isset( $this->options['optional'] ) && $this->options['optional'] && !file_exists( $full_path ) && ( !$evald->css || !empty( $this->currentFileInfo ) ) ) { + return array(); + } + + // css ? + if ( $evald->css ) { + $features = ( $evald->features ? $evald->features->compile( $env ) : null ); + return new Less_Tree_Import( $this->compilePath( $env ), $features, $this->options, $this->index ); + } + + return $this->ParseImport( $full_path, $uri, $env ); + } + + /** + * Using the import directories, get the full absolute path and uri of the import + * + * @param Less_Tree_Import $evald + */ + public function PathAndUri() { + $evald_path = $this->getPath(); + + if ( $evald_path ) { + + $import_dirs = array(); + + if ( Less_Environment::isPathRelative( $evald_path ) ) { + // if the path is relative, the file should be in the current directory + if ( $this->currentFileInfo ) { + $import_dirs[ $this->currentFileInfo['currentDirectory'] ] = $this->currentFileInfo['uri_root']; + } + + } else { + // otherwise, the file should be relative to the server root + if ( $this->currentFileInfo ) { + $import_dirs[ $this->currentFileInfo['entryPath'] ] = $this->currentFileInfo['entryUri']; + } + // if the user supplied entryPath isn't the actual root + $import_dirs[ $_SERVER['DOCUMENT_ROOT'] ] = ''; + + } + + // always look in user supplied import directories + $import_dirs = array_merge( $import_dirs, Less_Parser::$options['import_dirs'] ); + + foreach ( $import_dirs as $rootpath => $rooturi ) { + if ( is_callable( $rooturi ) ) { + list( $path, $uri ) = call_user_func( $rooturi, $evald_path ); + if ( is_string( $path ) ) { + $full_path = $path; + return array( $full_path, $uri ); + } + } elseif ( !empty( $rootpath ) ) { + + $path = rtrim( $rootpath, '/\\' ).'/'.ltrim( $evald_path, '/\\' ); + + if ( file_exists( $path ) ) { + $full_path = Less_Environment::normalizePath( $path ); + $uri = Less_Environment::normalizePath( dirname( $rooturi.$evald_path ) ); + return array( $full_path, $uri ); + } elseif ( file_exists( $path.'.less' ) ) { + $full_path = Less_Environment::normalizePath( $path.'.less' ); + $uri = Less_Environment::normalizePath( dirname( $rooturi.$evald_path.'.less' ) ); + return array( $full_path, $uri ); + } + } + } + } + } + + /** + * Parse the import url and return the rules + * + * @return Less_Tree_Media|array + */ + public function ParseImport( $full_path, $uri, $env ) { + $import_env = clone $env; + if ( ( isset( $this->options['reference'] ) && $this->options['reference'] ) || isset( $this->currentFileInfo['reference'] ) ) { + $import_env->currentFileInfo['reference'] = true; + } + + if ( ( isset( $this->options['multiple'] ) && $this->options['multiple'] ) ) { + $import_env->importMultiple = true; + } + + $parser = new Less_Parser( $import_env ); + $root = $parser->parseFile( $full_path, $uri, true ); + + $ruleset = new Less_Tree_Ruleset( array(), $root->rules ); + $ruleset->evalImports( $import_env ); + + return $this->features ? new Less_Tree_Media( $ruleset->rules, $this->features->value ) : $ruleset->rules; + } + + /** + * Should the import be skipped? + * + * @return boolean|null + */ + private function Skip( $path, $env ) { + $path = Less_Parser::AbsPath( $path, true ); + + if ( $path && Less_Parser::FileParsed( $path ) ) { + + if ( isset( $this->currentFileInfo['reference'] ) ) { + return true; + } + + return !isset( $this->options['multiple'] ) && !$env->importMultiple; + } + + } +} diff --git a/less.php/lib/Less/Tree/Javascript.php b/less.php/lib/Less/Tree/Javascript.php new file mode 100755 index 0000000..179781a --- /dev/null +++ b/less.php/lib/Less/Tree/Javascript.php @@ -0,0 +1,30 @@ +escaped = $escaped; + $this->expression = $string; + $this->index = $index; + } + + public function compile() { + return new Less_Tree_Anonymous( '/* Sorry, can not do JavaScript evaluation in PHP... :( */' ); + } + +} diff --git a/less.php/lib/Less/Tree/Keyword.php b/less.php/lib/Less/Tree/Keyword.php new file mode 100755 index 0000000..ed217cf --- /dev/null +++ b/less.php/lib/Less/Tree/Keyword.php @@ -0,0 +1,43 @@ +value = $value; + } + + public function compile() { + return $this; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + if ( $this->value === '%' ) { + throw new Less_Exception_Compiler( "Invalid % without number" ); + } + + $output->add( $this->value ); + } + + public function compare( $other ) { + if ( $other instanceof Less_Tree_Keyword ) { + return $other->value === $this->value ? 0 : 1; + } else { + return -1; + } + } +} diff --git a/less.php/lib/Less/Tree/Media.php b/less.php/lib/Less/Tree/Media.php new file mode 100755 index 0000000..ba25a67 --- /dev/null +++ b/less.php/lib/Less/Tree/Media.php @@ -0,0 +1,173 @@ +index = $index; + $this->currentFileInfo = $currentFileInfo; + + $selectors = $this->emptySelectors(); + + $this->features = new Less_Tree_Value( $features ); + + $this->rules = array( new Less_Tree_Ruleset( $selectors, $value ) ); + $this->rules[0]->allowImports = true; + } + + public function accept( $visitor ) { + $this->features = $visitor->visitObj( $this->features ); + $this->rules = $visitor->visitArray( $this->rules ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( '@media ', $this->currentFileInfo, $this->index ); + $this->features->genCSS( $output ); + Less_Tree::outputRuleset( $output, $this->rules ); + + } + + public function compile( $env ) { + $media = new Less_Tree_Media( array(), array(), $this->index, $this->currentFileInfo ); + + $strictMathBypass = false; + if ( Less_Parser::$options['strictMath'] === false ) { + $strictMathBypass = true; + Less_Parser::$options['strictMath'] = true; + } + + $media->features = $this->features->compile( $env ); + + if ( $strictMathBypass ) { + Less_Parser::$options['strictMath'] = false; + } + + $env->mediaPath[] = $media; + $env->mediaBlocks[] = $media; + + array_unshift( $env->frames, $this->rules[0] ); + $media->rules = array( $this->rules[0]->compile( $env ) ); + array_shift( $env->frames ); + + array_pop( $env->mediaPath ); + + return !$env->mediaPath ? $media->compileTop( $env ) : $media->compileNested( $env ); + } + + public function variable( $name ) { + return $this->rules[0]->variable( $name ); + } + + public function find( $selector ) { + return $this->rules[0]->find( $selector, $this ); + } + + public function emptySelectors() { + $el = new Less_Tree_Element( '', '&', $this->index, $this->currentFileInfo ); + $sels = array( new Less_Tree_Selector( array( $el ), array(), null, $this->index, $this->currentFileInfo ) ); + $sels[0]->mediaEmpty = true; + return $sels; + } + + public function markReferenced() { + $this->rules[0]->markReferenced(); + $this->isReferenced = true; + Less_Tree::ReferencedArray( $this->rules[0]->rules ); + } + + // evaltop + public function compileTop( $env ) { + $result = $this; + + if ( count( $env->mediaBlocks ) > 1 ) { + $selectors = $this->emptySelectors(); + $result = new Less_Tree_Ruleset( $selectors, $env->mediaBlocks ); + $result->multiMedia = true; + } + + $env->mediaBlocks = array(); + $env->mediaPath = array(); + + return $result; + } + + public function compileNested( $env ) { + $path = array_merge( $env->mediaPath, array( $this ) ); + + // Extract the media-query conditions separated with `,` (OR). + foreach ( $path as $key => $p ) { + $value = $p->features instanceof Less_Tree_Value ? $p->features->value : $p->features; + $path[$key] = is_array( $value ) ? $value : array( $value ); + } + + // Trace all permutations to generate the resulting media-query. + // + // (a, b and c) with nested (d, e) -> + // a and d + // a and e + // b and c and d + // b and c and e + + $permuted = $this->permute( $path ); + $expressions = array(); + foreach ( $permuted as $path ) { + + for ( $i = 0, $len = count( $path ); $i < $len; $i++ ) { + $path[$i] = Less_Parser::is_method( $path[$i], 'toCSS' ) ? $path[$i] : new Less_Tree_Anonymous( $path[$i] ); + } + + for ( $i = count( $path ) - 1; $i > 0; $i-- ) { + array_splice( $path, $i, 0, array( new Less_Tree_Anonymous( 'and' ) ) ); + } + + $expressions[] = new Less_Tree_Expression( $path ); + } + $this->features = new Less_Tree_Value( $expressions ); + + // Fake a tree-node that doesn't output anything. + return new Less_Tree_Ruleset( array(), array() ); + } + + public function permute( $arr ) { + if ( !$arr ) + return array(); + + if ( count( $arr ) == 1 ) + return $arr[0]; + + $result = array(); + $rest = $this->permute( array_slice( $arr, 1 ) ); + foreach ( $rest as $r ) { + foreach ( $arr[0] as $a ) { + $result[] = array_merge( + is_array( $a ) ? $a : array( $a ), + is_array( $r ) ? $r : array( $r ) + ); + } + } + + return $result; + } + + public function bubbleSelectors( $selectors ) { + if ( !$selectors ) return; + + $this->rules = array( new Less_Tree_Ruleset( $selectors, array( $this->rules[0] ) ) ); + } + +} diff --git a/less.php/lib/Less/Tree/Mixin/Call.php b/less.php/lib/Less/Tree/Mixin/Call.php new file mode 100755 index 0000000..8e1eece --- /dev/null +++ b/less.php/lib/Less/Tree/Mixin/Call.php @@ -0,0 +1,193 @@ +selector = new Less_Tree_Selector( $elements ); + $this->arguments = $args; + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + $this->important = $important; + } + + // function accept($visitor){ + // $this->selector = $visitor->visit($this->selector); + // $this->arguments = $visitor->visit($this->arguments); + //} + + public function compile( $env ) { + $rules = array(); + $match = false; + $isOneFound = false; + $candidates = array(); + $defaultUsed = false; + $conditionResult = array(); + + $args = array(); + foreach ( $this->arguments as $a ) { + $args[] = array( 'name' => $a['name'], 'value' => $a['value']->compile( $env ) ); + } + + foreach ( $env->frames as $frame ) { + + $mixins = $frame->find( $this->selector ); + + if ( !$mixins ) { + continue; + } + + $isOneFound = true; + $defNone = 0; + $defTrue = 1; + $defFalse = 2; + + // To make `default()` function independent of definition order we have two "subpasses" here. + // At first we evaluate each guard *twice* (with `default() == true` and `default() == false`), + // and build candidate list with corresponding flags. Then, when we know all possible matches, + // we make a final decision. + + $mixins_len = count( $mixins ); + for ( $m = 0; $m < $mixins_len; $m++ ) { + $mixin = $mixins[$m]; + + if ( $this->IsRecursive( $env, $mixin ) ) { + continue; + } + + if ( $mixin->matchArgs( $args, $env ) ) { + + $candidate = array( 'mixin' => $mixin, 'group' => $defNone ); + + if ( $mixin instanceof Less_Tree_Ruleset ) { + + for ( $f = 0; $f < 2; $f++ ) { + Less_Tree_DefaultFunc::value( $f ); + $conditionResult[$f] = $mixin->matchCondition( $args, $env ); + } + if ( $conditionResult[0] || $conditionResult[1] ) { + if ( $conditionResult[0] != $conditionResult[1] ) { + $candidate['group'] = $conditionResult[1] ? $defTrue : $defFalse; + } + + $candidates[] = $candidate; + } + } else { + $candidates[] = $candidate; + } + + $match = true; + } + } + + Less_Tree_DefaultFunc::reset(); + + $count = array( 0, 0, 0 ); + for ( $m = 0; $m < count( $candidates ); $m++ ) { + $count[ $candidates[$m]['group'] ]++; + } + + if ( $count[$defNone] > 0 ) { + $defaultResult = $defFalse; + } else { + $defaultResult = $defTrue; + if ( ( $count[$defTrue] + $count[$defFalse] ) > 1 ) { + throw new Exception( 'Ambiguous use of `default()` found when matching for `' . $this->format( $args ) . '`' ); + } + } + + $candidates_length = count( $candidates ); + $length_1 = ( $candidates_length == 1 ); + + for ( $m = 0; $m < $candidates_length; $m++ ) { + $candidate = $candidates[$m]['group']; + if ( ( $candidate === $defNone ) || ( $candidate === $defaultResult ) ) { + try{ + $mixin = $candidates[$m]['mixin']; + if ( !( $mixin instanceof Less_Tree_Mixin_Definition ) ) { + $mixin = new Less_Tree_Mixin_Definition( '', array(), $mixin->rules, null, false ); + $mixin->originalRuleset = $mixins[$m]->originalRuleset; + } + $rules = array_merge( $rules, $mixin->evalCall( $env, $args, $this->important )->rules ); + } catch ( Exception $e ) { + // throw new Less_Exception_Compiler($e->getMessage(), $e->index, null, $this->currentFileInfo['filename']); + throw new Less_Exception_Compiler( $e->getMessage(), null, null, $this->currentFileInfo ); + } + } + } + + if ( $match ) { + if ( !$this->currentFileInfo || !isset( $this->currentFileInfo['reference'] ) || !$this->currentFileInfo['reference'] ) { + Less_Tree::ReferencedArray( $rules ); + } + + return $rules; + } + } + + if ( $isOneFound ) { + throw new Less_Exception_Compiler( 'No matching definition was found for `'.$this->Format( $args ).'`', null, $this->index, $this->currentFileInfo ); + + } else { + throw new Less_Exception_Compiler( trim( $this->selector->toCSS() ) . " is undefined in ".$this->currentFileInfo['filename'], null, $this->index ); + } + + } + + /** + * Format the args for use in exception messages + * + */ + private function Format( $args ) { + $message = array(); + if ( $args ) { + foreach ( $args as $a ) { + $argValue = ''; + if ( $a['name'] ) { + $argValue .= $a['name'] . ':'; + } + if ( is_object( $a['value'] ) ) { + $argValue .= $a['value']->toCSS(); + } else { + $argValue .= '???'; + } + $message[] = $argValue; + } + } + return implode( ', ', $message ); + } + + /** + * Are we in a recursive mixin call? + * + * @return bool + */ + private function IsRecursive( $env, $mixin ) { + foreach ( $env->frames as $recur_frame ) { + if ( !( $mixin instanceof Less_Tree_Mixin_Definition ) ) { + + if ( $mixin === $recur_frame ) { + return true; + } + + if ( isset( $recur_frame->originalRuleset ) && $mixin->ruleset_id === $recur_frame->originalRuleset ) { + return true; + } + } + } + + return false; + } + +} diff --git a/less.php/lib/Less/Tree/Mixin/Definition.php b/less.php/lib/Less/Tree/Mixin/Definition.php new file mode 100755 index 0000000..f9f2eb4 --- /dev/null +++ b/less.php/lib/Less/Tree/Mixin/Definition.php @@ -0,0 +1,233 @@ +name = $name; + $this->selectors = array( new Less_Tree_Selector( array( new Less_Tree_Element( null, $name ) ) ) ); + + $this->params = $params; + $this->condition = $condition; + $this->variadic = $variadic; + $this->rules = $rules; + + if ( $params ) { + $this->arity = count( $params ); + foreach ( $params as $p ) { + if ( !isset( $p['name'] ) || ( $p['name'] && !isset( $p['value'] ) ) ) { + $this->required++; + } + } + } + + $this->frames = $frames; + $this->SetRulesetIndex(); + } + + // function accept( $visitor ){ + // $this->params = $visitor->visit($this->params); + // $this->rules = $visitor->visit($this->rules); + // $this->condition = $visitor->visit($this->condition); + //} + + public function toCSS() { + return ''; + } + + // less.js : /lib/less/tree/mixin.js : tree.mixin.Definition.evalParams + public function compileParams( $env, $mixinFrames, $args = array(), &$evaldArguments = array() ) { + $frame = new Less_Tree_Ruleset( null, array() ); + $params = $this->params; + $mixinEnv = null; + $argsLength = 0; + + if ( $args ) { + $argsLength = count( $args ); + for ( $i = 0; $i < $argsLength; $i++ ) { + $arg = $args[$i]; + + if ( $arg && $arg['name'] ) { + $isNamedFound = false; + + foreach ( $params as $j => $param ) { + if ( !isset( $evaldArguments[$j] ) && $arg['name'] === $params[$j]['name'] ) { + $evaldArguments[$j] = $arg['value']->compile( $env ); + array_unshift( $frame->rules, new Less_Tree_Rule( $arg['name'], $arg['value']->compile( $env ) ) ); + $isNamedFound = true; + break; + } + } + if ( $isNamedFound ) { + array_splice( $args, $i, 1 ); + $i--; + $argsLength--; + continue; + } else { + throw new Less_Exception_Compiler( "Named argument for " . $this->name .' '.$args[$i]['name'] . ' not found' ); + } + } + } + } + + $argIndex = 0; + foreach ( $params as $i => $param ) { + + if ( isset( $evaldArguments[$i] ) ) { continue; + } + + $arg = null; + if ( isset( $args[$argIndex] ) ) { + $arg = $args[$argIndex]; + } + + if ( isset( $param['name'] ) && $param['name'] ) { + + if ( isset( $param['variadic'] ) ) { + $varargs = array(); + for ( $j = $argIndex; $j < $argsLength; $j++ ) { + $varargs[] = $args[$j]['value']->compile( $env ); + } + $expression = new Less_Tree_Expression( $varargs ); + array_unshift( $frame->rules, new Less_Tree_Rule( $param['name'], $expression->compile( $env ) ) ); + } else { + $val = ( $arg && $arg['value'] ) ? $arg['value'] : false; + + if ( $val ) { + $val = $val->compile( $env ); + } else if ( isset( $param['value'] ) ) { + + if ( !$mixinEnv ) { + $mixinEnv = new Less_Environment(); + $mixinEnv->frames = array_merge( array( $frame ), $mixinFrames ); + } + + $val = $param['value']->compile( $mixinEnv ); + $frame->resetCache(); + } else { + throw new Less_Exception_Compiler( "Wrong number of arguments for " . $this->name . " (" . $argsLength . ' for ' . $this->arity . ")" ); + } + + array_unshift( $frame->rules, new Less_Tree_Rule( $param['name'], $val ) ); + $evaldArguments[$i] = $val; + } + } + + if ( isset( $param['variadic'] ) && $args ) { + for ( $j = $argIndex; $j < $argsLength; $j++ ) { + $evaldArguments[$j] = $args[$j]['value']->compile( $env ); + } + } + $argIndex++; + } + + ksort( $evaldArguments ); + $evaldArguments = array_values( $evaldArguments ); + + return $frame; + } + + public function compile( $env ) { + if ( $this->frames ) { + return new Less_Tree_Mixin_Definition( $this->name, $this->params, $this->rules, $this->condition, $this->variadic, $this->frames ); + } + return new Less_Tree_Mixin_Definition( $this->name, $this->params, $this->rules, $this->condition, $this->variadic, $env->frames ); + } + + public function evalCall( $env, $args = NULL, $important = NULL ) { + Less_Environment::$mixin_stack++; + + $_arguments = array(); + + if ( $this->frames ) { + $mixinFrames = array_merge( $this->frames, $env->frames ); + } else { + $mixinFrames = $env->frames; + } + + $frame = $this->compileParams( $env, $mixinFrames, $args, $_arguments ); + + $ex = new Less_Tree_Expression( $_arguments ); + array_unshift( $frame->rules, new Less_Tree_Rule( '@arguments', $ex->compile( $env ) ) ); + + $ruleset = new Less_Tree_Ruleset( null, $this->rules ); + $ruleset->originalRuleset = $this->ruleset_id; + + $ruleSetEnv = new Less_Environment(); + $ruleSetEnv->frames = array_merge( array( $this, $frame ), $mixinFrames ); + $ruleset = $ruleset->compile( $ruleSetEnv ); + + if ( $important ) { + $ruleset = $ruleset->makeImportant(); + } + + Less_Environment::$mixin_stack--; + + return $ruleset; + } + + public function matchCondition( $args, $env ) { + if ( !$this->condition ) { + return true; + } + + // set array to prevent error on array_merge + if ( !is_array( $this->frames ) ) { + $this->frames = array(); + } + + $frame = $this->compileParams( $env, array_merge( $this->frames, $env->frames ), $args ); + + $compile_env = new Less_Environment(); + $compile_env->frames = array_merge( + array( $frame ), // the parameter variables + $this->frames, // the parent namespace/mixin frames + $env->frames // the current environment frames + ); + + $compile_env->functions = $env->functions; + + return (bool)$this->condition->compile( $compile_env ); + } + + public function matchArgs( $args, $env = NULL ) { + $argsLength = count( $args ); + + if ( !$this->variadic ) { + if ( $argsLength < $this->required ) { + return false; + } + if ( $argsLength > count( $this->params ) ) { + return false; + } + } else { + if ( $argsLength < ( $this->required - 1 ) ) { + return false; + } + } + + $len = min( $argsLength, $this->arity ); + + for ( $i = 0; $i < $len; $i++ ) { + if ( !isset( $this->params[$i]['name'] ) && !isset( $this->params[$i]['variadic'] ) ) { + if ( $args[$i]['value']->compile( $env )->toCSS() != $this->params[$i]['value']->compile( $env )->toCSS() ) { + return false; + } + } + } + + return true; + } + +} diff --git a/less.php/lib/Less/Tree/NameValue.php b/less.php/lib/Less/Tree/NameValue.php new file mode 100755 index 0000000..4f9aa97 --- /dev/null +++ b/less.php/lib/Less/Tree/NameValue.php @@ -0,0 +1,49 @@ + color:#FF0000; + * + * @package Less + * @subpackage tree + */ +class Less_Tree_NameValue extends Less_Tree { + + public $name; + public $value; + public $index; + public $currentFileInfo; + public $type = 'NameValue'; + public $important = ''; + + public function __construct( $name, $value = null, $index = null, $currentFileInfo = null ) { + $this->name = $name; + $this->value = $value; + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + } + + public function genCSS( $output ) { + $output->add( + $this->name + . Less_Environment::$_outputMap[': '] + . $this->value + . $this->important + . ( ( ( Less_Environment::$lastRule && Less_Parser::$options['compress'] ) ) ? "" : ";" ), + $this->currentFileInfo, $this->index ); + } + + public function compile( $env ) { + return $this; + } + + public function makeImportant() { + $new = new Less_Tree_NameValue( $this->name, $this->value, $this->index, $this->currentFileInfo ); + $new->important = ' !important'; + return $new; + } + +} diff --git a/less.php/lib/Less/Tree/Negative.php b/less.php/lib/Less/Tree/Negative.php new file mode 100755 index 0000000..f0f36c8 --- /dev/null +++ b/less.php/lib/Less/Tree/Negative.php @@ -0,0 +1,37 @@ +value = $node; + } + + // function accept($visitor) { + // $this->value = $visitor->visit($this->value); + //} + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( '-' ); + $this->value->genCSS( $output ); + } + + public function compile( $env ) { + if ( Less_Environment::isMathOn() ) { + $ret = new Less_Tree_Operation( '*', array( new Less_Tree_Dimension( -1 ), $this->value ) ); + return $ret->compile( $env ); + } + return new Less_Tree_Negative( $this->value->compile( $env ) ); + } +} diff --git a/less.php/lib/Less/Tree/Operation.php b/less.php/lib/Less/Tree/Operation.php new file mode 100755 index 0000000..d4eb9ac --- /dev/null +++ b/less.php/lib/Less/Tree/Operation.php @@ -0,0 +1,68 @@ +op = trim( $op ); + $this->operands = $operands; + $this->isSpaced = $isSpaced; + } + + public function accept( $visitor ) { + $this->operands = $visitor->visitArray( $this->operands ); + } + + public function compile( $env ) { + $a = $this->operands[0]->compile( $env ); + $b = $this->operands[1]->compile( $env ); + + if ( Less_Environment::isMathOn() ) { + + if ( $a instanceof Less_Tree_Dimension && $b instanceof Less_Tree_Color ) { + $a = $a->toColor(); + + } elseif ( $b instanceof Less_Tree_Dimension && $a instanceof Less_Tree_Color ) { + $b = $b->toColor(); + + } + + if ( !method_exists( $a, 'operate' ) ) { + throw new Less_Exception_Compiler( "Operation on an invalid type" ); + } + + return $a->operate( $this->op, $b ); + } + + return new Less_Tree_Operation( $this->op, array( $a, $b ), $this->isSpaced ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $this->operands[0]->genCSS( $output ); + if ( $this->isSpaced ) { + $output->add( " " ); + } + $output->add( $this->op ); + if ( $this->isSpaced ) { + $output->add( ' ' ); + } + $this->operands[1]->genCSS( $output ); + } + +} diff --git a/less.php/lib/Less/Tree/Paren.php b/less.php/lib/Less/Tree/Paren.php new file mode 100755 index 0000000..77ee48c --- /dev/null +++ b/less.php/lib/Less/Tree/Paren.php @@ -0,0 +1,35 @@ +value = $value; + } + + public function accept( $visitor ) { + $this->value = $visitor->visitObj( $this->value ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( '(' ); + $this->value->genCSS( $output ); + $output->add( ')' ); + } + + public function compile( $env ) { + return new Less_Tree_Paren( $this->value->compile( $env ) ); + } + +} diff --git a/less.php/lib/Less/Tree/Quoted.php b/less.php/lib/Less/Tree/Quoted.php new file mode 100755 index 0000000..d01598b --- /dev/null +++ b/less.php/lib/Less/Tree/Quoted.php @@ -0,0 +1,79 @@ +escaped = $escaped; + $this->value = $content; + if ( $str ) { + $this->quote = $str[0]; + } + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + if ( !$this->escaped ) { + $output->add( $this->quote, $this->currentFileInfo, $this->index ); + } + $output->add( $this->value ); + if ( !$this->escaped ) { + $output->add( $this->quote ); + } + } + + public function compile( $env ) { + $value = $this->value; + if ( preg_match_all( '/`([^`]+)`/', $this->value, $matches ) ) { + foreach ( $matches as $i => $match ) { + $js = new Less_Tree_JavaScript( $matches[1], $this->index, true ); + $js = $js->compile()->value; + $value = str_replace( $matches[0][$i], $js, $value ); + } + } + + if ( preg_match_all( '/@\{([\w-]+)\}/', $value, $matches ) ) { + foreach ( $matches[1] as $i => $match ) { + $v = new Less_Tree_Variable( '@' . $match, $this->index, $this->currentFileInfo ); + $v = $v->compile( $env ); + $v = ( $v instanceof Less_Tree_Quoted ) ? $v->value : $v->toCSS(); + $value = str_replace( $matches[0][$i], $v, $value ); + } + } + + return new Less_Tree_Quoted( $this->quote . $value . $this->quote, $value, $this->escaped, $this->index, $this->currentFileInfo ); + } + + public function compare( $x ) { + if ( !Less_Parser::is_method( $x, 'toCSS' ) ) { + return -1; + } + + $left = $this->toCSS(); + $right = $x->toCSS(); + + if ( $left === $right ) { + return 0; + } + + return $left < $right ? -1 : 1; + } +} diff --git a/less.php/lib/Less/Tree/Rule.php b/less.php/lib/Less/Tree/Rule.php new file mode 100755 index 0000000..5f393eb --- /dev/null +++ b/less.php/lib/Less/Tree/Rule.php @@ -0,0 +1,112 @@ +name = $name; + $this->value = ( $value instanceof Less_Tree_Value || $value instanceof Less_Tree_Ruleset ) ? $value : new Less_Tree_Value( array( $value ) ); + $this->important = $important ? ' ' . trim( $important ) : ''; + $this->merge = $merge; + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + $this->inline = $inline; + $this->variable = ( is_string( $name ) && $name[0] === '@' ); + } + + public function accept( $visitor ) { + $this->value = $visitor->visitObj( $this->value ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->name . Less_Environment::$_outputMap[': '], $this->currentFileInfo, $this->index ); + try{ + $this->value->genCSS( $output ); + + }catch ( Less_Exception_Parser $e ) { + $e->index = $this->index; + $e->currentFile = $this->currentFileInfo; + throw $e; + } + $output->add( $this->important . ( ( $this->inline || ( Less_Environment::$lastRule && Less_Parser::$options['compress'] ) ) ? "" : ";" ), $this->currentFileInfo, $this->index ); + } + + public function compile( $env ) { + $name = $this->name; + if ( is_array( $name ) ) { + // expand 'primitive' name directly to get + // things faster (~10% for benchmark.less): + if ( count( $name ) === 1 && $name[0] instanceof Less_Tree_Keyword ) { + $name = $name[0]->value; + } else { + $name = $this->CompileName( $env, $name ); + } + } + + $strictMathBypass = Less_Parser::$options['strictMath']; + if ( $name === "font" && !Less_Parser::$options['strictMath'] ) { + Less_Parser::$options['strictMath'] = true; + } + + try { + $evaldValue = $this->value->compile( $env ); + + if ( !$this->variable && $evaldValue->type === "DetachedRuleset" ) { + throw new Less_Exception_Compiler( "Rulesets cannot be evaluated on a property.", null, $this->index, $this->currentFileInfo ); + } + + if ( Less_Environment::$mixin_stack ) { + $return = new Less_Tree_Rule( $name, $evaldValue, $this->important, $this->merge, $this->index, $this->currentFileInfo, $this->inline ); + } else { + $this->name = $name; + $this->value = $evaldValue; + $return = $this; + } + + }catch ( Less_Exception_Parser $e ) { + if ( !is_numeric( $e->index ) ) { + $e->index = $this->index; + $e->currentFile = $this->currentFileInfo; + } + throw $e; + } + + Less_Parser::$options['strictMath'] = $strictMathBypass; + + return $return; + } + + public function CompileName( $env, $name ) { + $output = new Less_Output(); + foreach ( $name as $n ) { + $n->compile( $env )->genCSS( $output ); + } + return $output->toString(); + } + + public function makeImportant() { + return new Less_Tree_Rule( $this->name, $this->value, '!important', $this->merge, $this->index, $this->currentFileInfo, $this->inline ); + } + +} diff --git a/less.php/lib/Less/Tree/Ruleset.php b/less.php/lib/Less/Tree/Ruleset.php new file mode 100755 index 0000000..9bc8243 --- /dev/null +++ b/less.php/lib/Less/Tree/Ruleset.php @@ -0,0 +1,621 @@ +ruleset_id = Less_Parser::$next_id++; + $this->originalRuleset = $this->ruleset_id; + + if ( $this->selectors ) { + foreach ( $this->selectors as $sel ) { + if ( $sel->_oelements ) { + $this->first_oelements[$sel->_oelements[0]] = true; + } + } + } + } + + public function __construct( $selectors, $rules, $strictImports = null ) { + $this->selectors = $selectors; + $this->rules = $rules; + $this->lookups = array(); + $this->strictImports = $strictImports; + $this->SetRulesetIndex(); + } + + public function accept( $visitor ) { + if ( $this->paths ) { + $paths_len = count( $this->paths ); + for ( $i = 0,$paths_len; $i < $paths_len; $i++ ) { + $this->paths[$i] = $visitor->visitArray( $this->paths[$i] ); + } + } elseif ( $this->selectors ) { + $this->selectors = $visitor->visitArray( $this->selectors ); + } + + if ( $this->rules ) { + $this->rules = $visitor->visitArray( $this->rules ); + } + } + + public function compile( $env ) { + $ruleset = $this->PrepareRuleset( $env ); + + // Store the frames around mixin definitions, + // so they can be evaluated like closures when the time comes. + $rsRuleCnt = count( $ruleset->rules ); + for ( $i = 0; $i < $rsRuleCnt; $i++ ) { + if ( $ruleset->rules[$i] instanceof Less_Tree_Mixin_Definition || $ruleset->rules[$i] instanceof Less_Tree_DetachedRuleset ) { + $ruleset->rules[$i] = $ruleset->rules[$i]->compile( $env ); + } + } + + $mediaBlockCount = 0; + if ( $env instanceof Less_Environment ) { + $mediaBlockCount = count( $env->mediaBlocks ); + } + + // Evaluate mixin calls. + $this->EvalMixinCalls( $ruleset, $env, $rsRuleCnt ); + + // Evaluate everything else + for ( $i = 0; $i < $rsRuleCnt; $i++ ) { + if ( !( $ruleset->rules[$i] instanceof Less_Tree_Mixin_Definition || $ruleset->rules[$i] instanceof Less_Tree_DetachedRuleset ) ) { + $ruleset->rules[$i] = $ruleset->rules[$i]->compile( $env ); + } + } + + // Evaluate everything else + for ( $i = 0; $i < $rsRuleCnt; $i++ ) { + $rule = $ruleset->rules[$i]; + + // for rulesets, check if it is a css guard and can be removed + if ( $rule instanceof Less_Tree_Ruleset && $rule->selectors && count( $rule->selectors ) === 1 ) { + + // check if it can be folded in (e.g. & where) + if ( $rule->selectors[0]->isJustParentSelector() ) { + array_splice( $ruleset->rules, $i--, 1 ); + $rsRuleCnt--; + + for ( $j = 0; $j < count( $rule->rules ); $j++ ) { + $subRule = $rule->rules[$j]; + if ( !( $subRule instanceof Less_Tree_Rule ) || !$subRule->variable ) { + array_splice( $ruleset->rules, ++$i, 0, array( $subRule ) ); + $rsRuleCnt++; + } + } + + } + } + } + + // Pop the stack + $env->shiftFrame(); + + if ( $mediaBlockCount ) { + $len = count( $env->mediaBlocks ); + for ( $i = $mediaBlockCount; $i < $len; $i++ ) { + $env->mediaBlocks[$i]->bubbleSelectors( $ruleset->selectors ); + } + } + + return $ruleset; + } + + /** + * Compile Less_Tree_Mixin_Call objects + * + * @param Less_Tree_Ruleset $ruleset + * @param integer $rsRuleCnt + */ + private function EvalMixinCalls( $ruleset, $env, &$rsRuleCnt ) { + for ( $i = 0; $i < $rsRuleCnt; $i++ ) { + $rule = $ruleset->rules[$i]; + + if ( $rule instanceof Less_Tree_Mixin_Call ) { + $rule = $rule->compile( $env ); + + $temp = array(); + foreach ( $rule as $r ) { + if ( ( $r instanceof Less_Tree_Rule ) && $r->variable ) { + // do not pollute the scope if the variable is + // already there. consider returning false here + // but we need a way to "return" variable from mixins + if ( !$ruleset->variable( $r->name ) ) { + $temp[] = $r; + } + } else { + $temp[] = $r; + } + } + $temp_count = count( $temp ) - 1; + array_splice( $ruleset->rules, $i, 1, $temp ); + $rsRuleCnt += $temp_count; + $i += $temp_count; + $ruleset->resetCache(); + + } elseif ( $rule instanceof Less_Tree_RulesetCall ) { + + $rule = $rule->compile( $env ); + $rules = array(); + foreach ( $rule->rules as $r ) { + if ( ( $r instanceof Less_Tree_Rule ) && $r->variable ) { + continue; + } + $rules[] = $r; + } + + array_splice( $ruleset->rules, $i, 1, $rules ); + $temp_count = count( $rules ); + $rsRuleCnt += $temp_count - 1; + $i += $temp_count - 1; + $ruleset->resetCache(); + } + + } + } + + /** + * Compile the selectors and create a new ruleset object for the compile() method + * + */ + private function PrepareRuleset( $env ) { + $hasOnePassingSelector = false; + $selectors = array(); + if ( $this->selectors ) { + Less_Tree_DefaultFunc::error( "it is currently only allowed in parametric mixin guards," ); + + foreach ( $this->selectors as $s ) { + $selector = $s->compile( $env ); + $selectors[] = $selector; + if ( $selector->evaldCondition ) { + $hasOnePassingSelector = true; + } + } + + Less_Tree_DefaultFunc::reset(); + } else { + $hasOnePassingSelector = true; + } + + if ( $this->rules && $hasOnePassingSelector ) { + $rules = $this->rules; + } else { + $rules = array(); + } + + $ruleset = new Less_Tree_Ruleset( $selectors, $rules, $this->strictImports ); + + $ruleset->originalRuleset = $this->ruleset_id; + + $ruleset->root = $this->root; + $ruleset->firstRoot = $this->firstRoot; + $ruleset->allowImports = $this->allowImports; + + // push the current ruleset to the frames stack + $env->unshiftFrame( $ruleset ); + + // Evaluate imports + if ( $ruleset->root || $ruleset->allowImports || !$ruleset->strictImports ) { + $ruleset->evalImports( $env ); + } + + return $ruleset; + } + + function evalImports( $env ) { + $rules_len = count( $this->rules ); + for ( $i = 0; $i < $rules_len; $i++ ) { + $rule = $this->rules[$i]; + + if ( $rule instanceof Less_Tree_Import ) { + $rules = $rule->compile( $env ); + if ( is_array( $rules ) ) { + array_splice( $this->rules, $i, 1, $rules ); + $temp_count = count( $rules ) - 1; + $i += $temp_count; + $rules_len += $temp_count; + } else { + array_splice( $this->rules, $i, 1, array( $rules ) ); + } + + $this->resetCache(); + } + } + } + + function makeImportant() { + $important_rules = array(); + foreach ( $this->rules as $rule ) { + if ( $rule instanceof Less_Tree_Rule || $rule instanceof Less_Tree_Ruleset || $rule instanceof Less_Tree_NameValue ) { + $important_rules[] = $rule->makeImportant(); + } else { + $important_rules[] = $rule; + } + } + + return new Less_Tree_Ruleset( $this->selectors, $important_rules, $this->strictImports ); + } + + public function matchArgs( $args ) { + return !$args; + } + + // lets you call a css selector with a guard + public function matchCondition( $args, $env ) { + $lastSelector = end( $this->selectors ); + + if ( !$lastSelector->evaldCondition ) { + return false; + } + if ( $lastSelector->condition && !$lastSelector->condition->compile( $env->copyEvalEnv( $env->frames ) ) ) { + return false; + } + return true; + } + + function resetCache() { + $this->_rulesets = null; + $this->_variables = null; + $this->lookups = array(); + } + + public function variables() { + $this->_variables = array(); + foreach ( $this->rules as $r ) { + if ( $r instanceof Less_Tree_Rule && $r->variable === true ) { + $this->_variables[$r->name] = $r; + } + } + } + + public function variable( $name ) { + if ( is_null( $this->_variables ) ) { + $this->variables(); + } + return isset( $this->_variables[$name] ) ? $this->_variables[$name] : null; + } + + public function find( $selector, $self = null ) { + $key = implode( ' ', $selector->_oelements ); + + if ( !isset( $this->lookups[$key] ) ) { + + if ( !$self ) { + $self = $this->ruleset_id; + } + + $this->lookups[$key] = array(); + + $first_oelement = $selector->_oelements[0]; + + foreach ( $this->rules as $rule ) { + if ( $rule instanceof Less_Tree_Ruleset && $rule->ruleset_id != $self ) { + + if ( isset( $rule->first_oelements[$first_oelement] ) ) { + + foreach ( $rule->selectors as $ruleSelector ) { + $match = $selector->match( $ruleSelector ); + if ( $match ) { + if ( $selector->elements_len > $match ) { + $this->lookups[$key] = array_merge( $this->lookups[$key], $rule->find( new Less_Tree_Selector( array_slice( $selector->elements, $match ) ), $self ) ); + } else { + $this->lookups[$key][] = $rule; + } + break; + } + } + } + } + } + } + + return $this->lookups[$key]; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + if ( !$this->root ) { + Less_Environment::$tabLevel++; + } + + $tabRuleStr = $tabSetStr = ''; + if ( !Less_Parser::$options['compress'] ) { + if ( Less_Environment::$tabLevel ) { + $tabRuleStr = "\n".str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel ); + $tabSetStr = "\n".str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 ); + } else { + $tabSetStr = $tabRuleStr = "\n"; + } + } + + $ruleNodes = array(); + $rulesetNodes = array(); + foreach ( $this->rules as $rule ) { + + $class = get_class( $rule ); + if ( ( $class === 'Less_Tree_Media' ) || ( $class === 'Less_Tree_Directive' ) || ( $this->root && $class === 'Less_Tree_Comment' ) || ( $class === 'Less_Tree_Ruleset' && $rule->rules ) ) { + $rulesetNodes[] = $rule; + } else { + $ruleNodes[] = $rule; + } + } + + // If this is the root node, we don't render + // a selector, or {}. + if ( !$this->root ) { + + /* + debugInfo = tree.debugInfo(env, this, tabSetStr); + + if (debugInfo) { + output.add(debugInfo); + output.add(tabSetStr); + } + */ + + $paths_len = count( $this->paths ); + for ( $i = 0; $i < $paths_len; $i++ ) { + $path = $this->paths[$i]; + $firstSelector = true; + + foreach ( $path as $p ) { + $p->genCSS( $output, $firstSelector ); + $firstSelector = false; + } + + if ( $i + 1 < $paths_len ) { + $output->add( ',' . $tabSetStr ); + } + } + + $output->add( ( Less_Parser::$options['compress'] ? '{' : " {" ) . $tabRuleStr ); + } + + // Compile rules and rulesets + $ruleNodes_len = count( $ruleNodes ); + $rulesetNodes_len = count( $rulesetNodes ); + for ( $i = 0; $i < $ruleNodes_len; $i++ ) { + $rule = $ruleNodes[$i]; + + // @page{ directive ends up with root elements inside it, a mix of rules and rulesets + // In this instance we do not know whether it is the last property + if ( $i + 1 === $ruleNodes_len && ( !$this->root || $rulesetNodes_len === 0 || $this->firstRoot ) ) { + Less_Environment::$lastRule = true; + } + + $rule->genCSS( $output ); + + if ( !Less_Environment::$lastRule ) { + $output->add( $tabRuleStr ); + } else { + Less_Environment::$lastRule = false; + } + } + + if ( !$this->root ) { + $output->add( $tabSetStr . '}' ); + Less_Environment::$tabLevel--; + } + + $firstRuleset = true; + $space = ( $this->root ? $tabRuleStr : $tabSetStr ); + for ( $i = 0; $i < $rulesetNodes_len; $i++ ) { + + if ( $ruleNodes_len && $firstRuleset ) { + $output->add( $space ); + } elseif ( !$firstRuleset ) { + $output->add( $space ); + } + $firstRuleset = false; + $rulesetNodes[$i]->genCSS( $output ); + } + + if ( !Less_Parser::$options['compress'] && $this->firstRoot ) { + $output->add( "\n" ); + } + + } + + function markReferenced() { + if ( !$this->selectors ) { + return; + } + foreach ( $this->selectors as $selector ) { + $selector->markReferenced(); + } + } + + public function joinSelectors( $context, $selectors ) { + $paths = array(); + if ( is_array( $selectors ) ) { + foreach ( $selectors as $selector ) { + $this->joinSelector( $paths, $context, $selector ); + } + } + return $paths; + } + + public function joinSelector( &$paths, $context, $selector ) { + $hasParentSelector = false; + + foreach ( $selector->elements as $el ) { + if ( $el->value === '&' ) { + $hasParentSelector = true; + } + } + + if ( !$hasParentSelector ) { + if ( $context ) { + foreach ( $context as $context_el ) { + $paths[] = array_merge( $context_el, array( $selector ) ); + } + } else { + $paths[] = array( $selector ); + } + return; + } + + // The paths are [[Selector]] + // The first list is a list of comma separated selectors + // The inner list is a list of inheritance separated selectors + // e.g. + // .a, .b { + // .c { + // } + // } + // == [[.a] [.c]] [[.b] [.c]] + // + + // the elements from the current selector so far + $currentElements = array(); + // the current list of new selectors to add to the path. + // We will build it up. We initiate it with one empty selector as we "multiply" the new selectors + // by the parents + $newSelectors = array( array() ); + + foreach ( $selector->elements as $el ) { + + // non parent reference elements just get added + if ( $el->value !== '&' ) { + $currentElements[] = $el; + } else { + // the new list of selectors to add + $selectorsMultiplied = array(); + + // merge the current list of non parent selector elements + // on to the current list of selectors to add + if ( $currentElements ) { + $this->mergeElementsOnToSelectors( $currentElements, $newSelectors ); + } + + // loop through our current selectors + foreach ( $newSelectors as $sel ) { + + // if we don't have any parent paths, the & might be in a mixin so that it can be used + // whether there are parents or not + if ( !$context ) { + // the combinator used on el should now be applied to the next element instead so that + // it is not lost + if ( $sel ) { + $sel[0]->elements = array_slice( $sel[0]->elements, 0 ); + $sel[0]->elements[] = new Less_Tree_Element( $el->combinator, '', $el->index, $el->currentFileInfo ); + } + $selectorsMultiplied[] = $sel; + } else { + + // and the parent selectors + foreach ( $context as $parentSel ) { + // We need to put the current selectors + // then join the last selector's elements on to the parents selectors + + // our new selector path + $newSelectorPath = array(); + // selectors from the parent after the join + $afterParentJoin = array(); + $newJoinedSelectorEmpty = true; + + // construct the joined selector - if & is the first thing this will be empty, + // if not newJoinedSelector will be the last set of elements in the selector + if ( $sel ) { + $newSelectorPath = $sel; + $lastSelector = array_pop( $newSelectorPath ); + $newJoinedSelector = $selector->createDerived( array_slice( $lastSelector->elements, 0 ) ); + $newJoinedSelectorEmpty = false; + } else { + $newJoinedSelector = $selector->createDerived( array() ); + } + + // put together the parent selectors after the join + if ( count( $parentSel ) > 1 ) { + $afterParentJoin = array_merge( $afterParentJoin, array_slice( $parentSel, 1 ) ); + } + + if ( $parentSel ) { + $newJoinedSelectorEmpty = false; + + // join the elements so far with the first part of the parent + $newJoinedSelector->elements[] = new Less_Tree_Element( $el->combinator, $parentSel[0]->elements[0]->value, $el->index, $el->currentFileInfo ); + + $newJoinedSelector->elements = array_merge( $newJoinedSelector->elements, array_slice( $parentSel[0]->elements, 1 ) ); + } + + if ( !$newJoinedSelectorEmpty ) { + // now add the joined selector + $newSelectorPath[] = $newJoinedSelector; + } + + // and the rest of the parent + $newSelectorPath = array_merge( $newSelectorPath, $afterParentJoin ); + + // add that to our new set of selectors + $selectorsMultiplied[] = $newSelectorPath; + } + } + } + + // our new selectors has been multiplied, so reset the state + $newSelectors = $selectorsMultiplied; + $currentElements = array(); + } + } + + // if we have any elements left over (e.g. .a& .b == .b) + // add them on to all the current selectors + if ( $currentElements ) { + $this->mergeElementsOnToSelectors( $currentElements, $newSelectors ); + } + foreach ( $newSelectors as $new_sel ) { + if ( $new_sel ) { + $paths[] = $new_sel; + } + } + } + + function mergeElementsOnToSelectors( $elements, &$selectors ) { + if ( !$selectors ) { + $selectors[] = array( new Less_Tree_Selector( $elements ) ); + return; + } + + foreach ( $selectors as &$sel ) { + + // if the previous thing in sel is a parent this needs to join on to it + if ( $sel ) { + $last = count( $sel ) - 1; + $sel[$last] = $sel[$last]->createDerived( array_merge( $sel[$last]->elements, $elements ) ); + } else { + $sel[] = new Less_Tree_Selector( $elements ); + } + } + } +} diff --git a/less.php/lib/Less/Tree/RulesetCall.php b/less.php/lib/Less/Tree/RulesetCall.php new file mode 100755 index 0000000..079ff4f --- /dev/null +++ b/less.php/lib/Less/Tree/RulesetCall.php @@ -0,0 +1,26 @@ +variable = $variable; + } + + public function accept( $visitor ) { + } + + public function compile( $env ) { + $variable = new Less_Tree_Variable( $this->variable ); + $detachedRuleset = $variable->compile( $env ); + return $detachedRuleset->callEval( $env ); + } +} diff --git a/less.php/lib/Less/Tree/Selector.php b/less.php/lib/Less/Tree/Selector.php new file mode 100755 index 0000000..7ecfc6a --- /dev/null +++ b/less.php/lib/Less/Tree/Selector.php @@ -0,0 +1,165 @@ +elements = $elements; + $this->elements_len = count( $elements ); + $this->extendList = $extendList; + $this->condition = $condition; + if ( $currentFileInfo ) { + $this->currentFileInfo = $currentFileInfo; + } + $this->isReferenced = $isReferenced; + if ( !$condition ) { + $this->evaldCondition = true; + } + + $this->CacheElements(); + } + + public function accept( $visitor ) { + $this->elements = $visitor->visitArray( $this->elements ); + $this->extendList = $visitor->visitArray( $this->extendList ); + if ( $this->condition ) { + $this->condition = $visitor->visitObj( $this->condition ); + } + + if ( $visitor instanceof Less_Visitor_extendFinder ) { + $this->CacheElements(); + } + } + + public function createDerived( $elements, $extendList = null, $evaldCondition = null ) { + $newSelector = new Less_Tree_Selector( $elements, ( $extendList ? $extendList : $this->extendList ), null, $this->index, $this->currentFileInfo, $this->isReferenced ); + $newSelector->evaldCondition = $evaldCondition ? $evaldCondition : $this->evaldCondition; + return $newSelector; + } + + public function match( $other ) { + if ( !$other->_oelements || ( $this->elements_len < $other->_oelements_len ) ) { + return 0; + } + + for ( $i = 0; $i < $other->_oelements_len; $i++ ) { + if ( $this->elements[$i]->value !== $other->_oelements[$i] ) { + return 0; + } + } + + return $other->_oelements_len; // return number of matched elements + } + + public function CacheElements() { + $this->_oelements = array(); + $this->_oelements_assoc = array(); + + $css = ''; + + foreach ( $this->elements as $v ) { + + $css .= $v->combinator; + if ( !$v->value_is_object ) { + $css .= $v->value; + continue; + } + + if ( !property_exists( $v->value, 'value' ) || !is_string( $v->value->value ) ) { + $this->cacheable = false; + return; + } + $css .= $v->value->value; + } + + $this->_oelements_len = preg_match_all( '/[,&#\.\w-](?:[\w-]|(?:\\\\.))*/', $css, $matches ); + if ( $this->_oelements_len ) { + $this->_oelements = $matches[0]; + + if ( $this->_oelements[0] === '&' ) { + array_shift( $this->_oelements ); + $this->_oelements_len--; + } + + $this->_oelements_assoc = array_fill_keys( $this->_oelements, true ); + } + } + + public function isJustParentSelector() { + return !$this->mediaEmpty && + count( $this->elements ) === 1 && + $this->elements[0]->value === '&' && + ( $this->elements[0]->combinator === ' ' || $this->elements[0]->combinator === '' ); + } + + public function compile( $env ) { + $elements = array(); + foreach ( $this->elements as $el ) { + $elements[] = $el->compile( $env ); + } + + $extendList = array(); + foreach ( $this->extendList as $el ) { + $extendList[] = $el->compile( $el ); + } + + $evaldCondition = false; + if ( $this->condition ) { + $evaldCondition = $this->condition->compile( $env ); + } + + return $this->createDerived( $elements, $extendList, $evaldCondition ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output, $firstSelector = true ) { + if ( !$firstSelector && $this->elements[0]->combinator === "" ) { + $output->add( ' ', $this->currentFileInfo, $this->index ); + } + + foreach ( $this->elements as $element ) { + $element->genCSS( $output ); + } + } + + public function markReferenced() { + $this->isReferenced = true; + } + + public function getIsReferenced() { + return !isset( $this->currentFileInfo['reference'] ) || !$this->currentFileInfo['reference'] || $this->isReferenced; + } + + public function getIsOutput() { + return $this->evaldCondition; + } + +} diff --git a/less.php/lib/Less/Tree/UnicodeDescriptor.php b/less.php/lib/Less/Tree/UnicodeDescriptor.php new file mode 100755 index 0000000..38e0526 --- /dev/null +++ b/less.php/lib/Less/Tree/UnicodeDescriptor.php @@ -0,0 +1,28 @@ +value = $value; + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( $this->value ); + } + + public function compile() { + return $this; + } +} diff --git a/less.php/lib/Less/Tree/Unit.php b/less.php/lib/Less/Tree/Unit.php new file mode 100755 index 0000000..29230df --- /dev/null +++ b/less.php/lib/Less/Tree/Unit.php @@ -0,0 +1,142 @@ +numerator = $numerator; + $this->denominator = $denominator; + $this->backupUnit = $backupUnit; + } + + public function __clone() { + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + if ( $this->numerator ) { + $output->add( $this->numerator[0] ); + } elseif ( $this->denominator ) { + $output->add( $this->denominator[0] ); + } elseif ( !Less_Parser::$options['strictUnits'] && $this->backupUnit ) { + $output->add( $this->backupUnit ); + return; + } + } + + public function toString() { + $returnStr = implode( '*', $this->numerator ); + foreach ( $this->denominator as $d ) { + $returnStr .= '/'.$d; + } + return $returnStr; + } + + public function __toString() { + return $this->toString(); + } + + /** + * @param Less_Tree_Unit $other + */ + public function compare( $other ) { + return $this->is( $other->toString() ) ? 0 : -1; + } + + public function is( $unitString ) { + return $this->toString() === $unitString; + } + + public function isLength() { + $css = $this->toCSS(); + return !!preg_match( '/px|em|%|in|cm|mm|pc|pt|ex/', $css ); + } + + public function isAngle() { + return isset( Less_Tree_UnitConversions::$angle[$this->toCSS()] ); + } + + public function isEmpty() { + return !$this->numerator && !$this->denominator; + } + + public function isSingular() { + return count( $this->numerator ) <= 1 && !$this->denominator; + } + + public function usedUnits() { + $result = array(); + + foreach ( Less_Tree_UnitConversions::$groups as $groupName ) { + $group = Less_Tree_UnitConversions::${$groupName}; + + foreach ( $this->numerator as $atomicUnit ) { + if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) { + $result[$groupName] = $atomicUnit; + } + } + + foreach ( $this->denominator as $atomicUnit ) { + if ( isset( $group[$atomicUnit] ) && !isset( $result[$groupName] ) ) { + $result[$groupName] = $atomicUnit; + } + } + } + + return $result; + } + + public function cancel() { + $counter = array(); + $backup = null; + + foreach ( $this->numerator as $atomicUnit ) { + if ( !$backup ) { + $backup = $atomicUnit; + } + $counter[$atomicUnit] = ( isset( $counter[$atomicUnit] ) ? $counter[$atomicUnit] : 0 ) + 1; + } + + foreach ( $this->denominator as $atomicUnit ) { + if ( !$backup ) { + $backup = $atomicUnit; + } + $counter[$atomicUnit] = ( isset( $counter[$atomicUnit] ) ? $counter[$atomicUnit] : 0 ) - 1; + } + + $this->numerator = array(); + $this->denominator = array(); + + foreach ( $counter as $atomicUnit => $count ) { + if ( $count > 0 ) { + for ( $i = 0; $i < $count; $i++ ) { + $this->numerator[] = $atomicUnit; + } + } elseif ( $count < 0 ) { + for ( $i = 0; $i < -$count; $i++ ) { + $this->denominator[] = $atomicUnit; + } + } + } + + if ( !$this->numerator && !$this->denominator && $backup ) { + $this->backupUnit = $backup; + } + + sort( $this->numerator ); + sort( $this->denominator ); + } + +} diff --git a/less.php/lib/Less/Tree/UnitConversions.php b/less.php/lib/Less/Tree/UnitConversions.php new file mode 100755 index 0000000..7f857fb --- /dev/null +++ b/less.php/lib/Less/Tree/UnitConversions.php @@ -0,0 +1,35 @@ + 1, + 'cm' => 0.01, + 'mm' => 0.001, + 'in' => 0.0254, + 'px' => 0.000264583, // 0.0254 / 96, + 'pt' => 0.000352778, // 0.0254 / 72, + 'pc' => 0.004233333, // 0.0254 / 72 * 12 + ); + + public static $duration = array( + 's' => 1, + 'ms' => 0.001 + ); + + public static $angle = array( + 'rad' => 0.1591549430919, // 1/(2*M_PI), + 'deg' => 0.002777778, // 1/360, + 'grad' => 0.0025, // 1/400, + 'turn' => 1 + ); + +} diff --git a/less.php/lib/Less/Tree/Url.php b/less.php/lib/Less/Tree/Url.php new file mode 100755 index 0000000..4e7f114 --- /dev/null +++ b/less.php/lib/Less/Tree/Url.php @@ -0,0 +1,76 @@ +value = $value; + $this->currentFileInfo = $currentFileInfo; + $this->isEvald = $isEvald; + } + + public function accept( $visitor ) { + $this->value = $visitor->visitObj( $this->value ); + } + + /** + * @see Less_Tree::genCSS + */ + public function genCSS( $output ) { + $output->add( 'url(' ); + $this->value->genCSS( $output ); + $output->add( ')' ); + } + + /** + * @param Less_Functions $ctx + */ + public function compile( $ctx ) { + $val = $this->value->compile( $ctx ); + + if ( !$this->isEvald ) { + // Add the base path if the URL is relative + if ( Less_Parser::$options['relativeUrls'] + && $this->currentFileInfo + && is_string( $val->value ) + && Less_Environment::isPathRelative( $val->value ) + ) { + $rootpath = $this->currentFileInfo['uri_root']; + if ( !$val->quote ) { + $rootpath = preg_replace( '/[\(\)\'"\s]/', '\\$1', $rootpath ); + } + $val->value = $rootpath . $val->value; + } + + $val->value = Less_Environment::normalizePath( $val->value ); + } + + // Add cache buster if enabled + if ( Less_Parser::$options['urlArgs'] ) { + if ( !preg_match( '/^\s*data:/', $val->value ) ) { + $delimiter = strpos( $val->value, '?' ) === false ? '?' : '&'; + $urlArgs = $delimiter . Less_Parser::$options['urlArgs']; + $hash_pos = strpos( $val->value, '#' ); + if ( $hash_pos !== false ) { + $val->value = substr_replace( $val->value, $urlArgs, $hash_pos, 0 ); + } else { + $val->value .= $urlArgs; + } + } + } + + return new Less_Tree_URL( $val, $this->currentFileInfo, true ); + } + +} diff --git a/less.php/lib/Less/Tree/Value.php b/less.php/lib/Less/Tree/Value.php new file mode 100755 index 0000000..a4c0d85 --- /dev/null +++ b/less.php/lib/Less/Tree/Value.php @@ -0,0 +1,47 @@ +value = $value; + } + + public function accept( $visitor ) { + $this->value = $visitor->visitArray( $this->value ); + } + + public function compile( $env ) { + $ret = array(); + $i = 0; + foreach ( $this->value as $i => $v ) { + $ret[] = $v->compile( $env ); + } + if ( $i > 0 ) { + return new Less_Tree_Value( $ret ); + } + return $ret[0]; + } + + /** + * @see Less_Tree::genCSS + */ + function genCSS( $output ) { + $len = count( $this->value ); + for ( $i = 0; $i < $len; $i++ ) { + $this->value[$i]->genCSS( $output ); + if ( $i + 1 < $len ) { + $output->add( Less_Environment::$_outputMap[','] ); + } + } + } + +} diff --git a/less.php/lib/Less/Tree/Variable.php b/less.php/lib/Less/Tree/Variable.php new file mode 100755 index 0000000..e36d3d4 --- /dev/null +++ b/less.php/lib/Less/Tree/Variable.php @@ -0,0 +1,51 @@ +name = $name; + $this->index = $index; + $this->currentFileInfo = $currentFileInfo; + } + + public function compile( $env ) { + if ( $this->name[1] === '@' ) { + $v = new Less_Tree_Variable( substr( $this->name, 1 ), $this->index + 1, $this->currentFileInfo ); + $name = '@' . $v->compile( $env )->value; + } else { + $name = $this->name; + } + + if ( $this->evaluating ) { + throw new Less_Exception_Compiler( "Recursive variable definition for " . $name, null, $this->index, $this->currentFileInfo ); + } + + $this->evaluating = true; + + foreach ( $env->frames as $frame ) { + if ( $v = $frame->variable( $name ) ) { + $r = $v->value->compile( $env ); + $this->evaluating = false; + return $r; + } + } + + throw new Less_Exception_Compiler( "variable " . $name . " is undefined in file ".$this->currentFileInfo["filename"], null, $this->index, $this->currentFileInfo ); + } + +} diff --git a/less.php/lib/Less/Version.php b/less.php/lib/Less/Version.php new file mode 100755 index 0000000..2d05c38 --- /dev/null +++ b/less.php/lib/Less/Version.php @@ -0,0 +1,15 @@ +_visitFnCache = get_class_methods( get_class( $this ) ); + $this->_visitFnCache = array_flip( $this->_visitFnCache ); + } + + public function visitObj( $node ) { + $funcName = 'visit'.$node->type; + if ( isset( $this->_visitFnCache[$funcName] ) ) { + + $visitDeeper = true; + $this->$funcName( $node, $visitDeeper ); + + if ( $visitDeeper ) { + $node->accept( $this ); + } + + $funcName = $funcName . "Out"; + if ( isset( $this->_visitFnCache[$funcName] ) ) { + $this->$funcName( $node ); + } + + } else { + $node->accept( $this ); + } + + return $node; + } + + public function visitArray( $nodes ) { + array_map( array( $this,'visitObj' ), $nodes ); + return $nodes; + } +} diff --git a/less.php/lib/Less/Visitor/extendFinder.php b/less.php/lib/Less/Visitor/extendFinder.php new file mode 100755 index 0000000..7d5d3fd --- /dev/null +++ b/less.php/lib/Less/Visitor/extendFinder.php @@ -0,0 +1,109 @@ +contexts = array(); + $this->allExtendsStack = array( array() ); + parent::__construct(); + } + + /** + * @param Less_Tree_Ruleset $root + */ + public function run( $root ) { + $root = $this->visitObj( $root ); + $root->allExtends =& $this->allExtendsStack[0]; + return $root; + } + + public function visitRule( $ruleNode, &$visitDeeper ) { + $visitDeeper = false; + } + + public function visitMixinDefinition( $mixinDefinitionNode, &$visitDeeper ) { + $visitDeeper = false; + } + + public function visitRuleset( $rulesetNode ) { + if ( $rulesetNode->root ) { + return; + } + + $allSelectorsExtendList = array(); + + // get &:extend(.a); rules which apply to all selectors in this ruleset + if ( $rulesetNode->rules ) { + foreach ( $rulesetNode->rules as $rule ) { + if ( $rule instanceof Less_Tree_Extend ) { + $allSelectorsExtendList[] = $rule; + $rulesetNode->extendOnEveryPath = true; + } + } + } + + // now find every selector and apply the extends that apply to all extends + // and the ones which apply to an individual extend + foreach ( $rulesetNode->paths as $selectorPath ) { + $selector = end( $selectorPath ); // $selectorPath[ count($selectorPath)-1]; + + $j = 0; + foreach ( $selector->extendList as $extend ) { + $this->allExtendsStackPush( $rulesetNode, $selectorPath, $extend, $j ); + } + foreach ( $allSelectorsExtendList as $extend ) { + $this->allExtendsStackPush( $rulesetNode, $selectorPath, $extend, $j ); + } + } + + $this->contexts[] = $rulesetNode->selectors; + } + + public function allExtendsStackPush( $rulesetNode, $selectorPath, $extend, &$j ) { + $this->foundExtends = true; + $extend = clone $extend; + $extend->findSelfSelectors( $selectorPath ); + $extend->ruleset = $rulesetNode; + if ( $j === 0 ) { + $extend->firstExtendOnThisSelectorPath = true; + } + + $end_key = count( $this->allExtendsStack ) - 1; + $this->allExtendsStack[$end_key][] = $extend; + $j++; + } + + public function visitRulesetOut( $rulesetNode ) { + if ( !is_object( $rulesetNode ) || !$rulesetNode->root ) { + array_pop( $this->contexts ); + } + } + + public function visitMedia( $mediaNode ) { + $mediaNode->allExtends = array(); + $this->allExtendsStack[] =& $mediaNode->allExtends; + } + + public function visitMediaOut() { + array_pop( $this->allExtendsStack ); + } + + public function visitDirective( $directiveNode ) { + $directiveNode->allExtends = array(); + $this->allExtendsStack[] =& $directiveNode->allExtends; + } + + public function visitDirectiveOut() { + array_pop( $this->allExtendsStack ); + } +} diff --git a/less.php/lib/Less/Visitor/import.php b/less.php/lib/Less/Visitor/import.php new file mode 100755 index 0000000..7af96eb --- /dev/null +++ b/less.php/lib/Less/Visitor/import.php @@ -0,0 +1,137 @@ +env = $evalEnv; + $this->importCount = 0; + parent::__construct(); + } + + + function run( $root ){ + $root = $this->visitObj($root); + $this->isFinished = true; + + //if( $this->importCount === 0) { + // $this->_finish(); + //} + } + + function visitImport($importNode, &$visitDeeper ){ + $importVisitor = $this; + $inlineCSS = $importNode->options['inline']; + + if( !$importNode->css || $inlineCSS ){ + $evaldImportNode = $importNode->compileForImport($this->env); + + if( $evaldImportNode && (!$evaldImportNode->css || $inlineCSS) ){ + $importNode = $evaldImportNode; + $this->importCount++; + $env = clone $this->env; + + if( (isset($importNode->options['multiple']) && $importNode->options['multiple']) ){ + $env->importMultiple = true; + } + + //get path & uri + $path_and_uri = null; + if( is_callable(Less_Parser::$options['import_callback']) ){ + $path_and_uri = call_user_func(Less_Parser::$options['import_callback'],$importNode); + } + + if( !$path_and_uri ){ + $path_and_uri = $importNode->PathAndUri(); + } + + if( $path_and_uri ){ + list($full_path, $uri) = $path_and_uri; + }else{ + $full_path = $uri = $importNode->getPath(); + } + + + //import once + if( $importNode->skip( $full_path, $env) ){ + return array(); + } + + if( $importNode->options['inline'] ){ + //todo needs to reference css file not import + //$contents = new Less_Tree_Anonymous($importNode->root, 0, array('filename'=>$importNode->importedFilename), true ); + + Less_Parser::AddParsedFile($full_path); + $contents = new Less_Tree_Anonymous( file_get_contents($full_path), 0, array(), true ); + + if( $importNode->features ){ + return new Less_Tree_Media( array($contents), $importNode->features->value ); + } + + return array( $contents ); + } + + + // css ? + if( $importNode->css ){ + $features = ( $importNode->features ? $importNode->features->compile($env) : null ); + return new Less_Tree_Import( $importNode->compilePath( $env), $features, $importNode->options, $this->index); + } + + return $importNode->ParseImport( $full_path, $uri, $env ); + } + + } + + $visitDeeper = false; + return $importNode; + } + + + function visitRule( $ruleNode, &$visitDeeper ){ + $visitDeeper = false; + return $ruleNode; + } + + function visitDirective($directiveNode, $visitArgs){ + array_unshift($this->env->frames,$directiveNode); + return $directiveNode; + } + + function visitDirectiveOut($directiveNode) { + array_shift($this->env->frames); + } + + function visitMixinDefinition($mixinDefinitionNode, $visitArgs) { + array_unshift($this->env->frames,$mixinDefinitionNode); + return $mixinDefinitionNode; + } + + function visitMixinDefinitionOut($mixinDefinitionNode) { + array_shift($this->env->frames); + } + + function visitRuleset($rulesetNode, $visitArgs) { + array_unshift($this->env->frames,$rulesetNode); + return $rulesetNode; + } + + function visitRulesetOut($rulesetNode) { + array_shift($this->env->frames); + } + + function visitMedia($mediaNode, $visitArgs) { + array_unshift($this->env->frames, $mediaNode->ruleset); + return $mediaNode; + } + + function visitMediaOut($mediaNode) { + array_shift($this->env->frames); + } + +} +*/ diff --git a/less.php/lib/Less/Visitor/joinSelector.php b/less.php/lib/Less/Visitor/joinSelector.php new file mode 100755 index 0000000..bb08ece --- /dev/null +++ b/less.php/lib/Less/Visitor/joinSelector.php @@ -0,0 +1,68 @@ +visitObj( $root ); + } + + public function visitRule( $ruleNode, &$visitDeeper ) { + $visitDeeper = false; + } + + public function visitMixinDefinition( $mixinDefinitionNode, &$visitDeeper ) { + $visitDeeper = false; + } + + public function visitRuleset( $rulesetNode ) { + $paths = array(); + + if ( !$rulesetNode->root ) { + $selectors = array(); + + if ( $rulesetNode->selectors && $rulesetNode->selectors ) { + foreach ( $rulesetNode->selectors as $selector ) { + if ( $selector->getIsOutput() ) { + $selectors[] = $selector; + } + } + } + + if ( !$selectors ) { + $rulesetNode->selectors = null; + $rulesetNode->rules = null; + } else { + $context = end( $this->contexts ); // $context = $this->contexts[ count($this->contexts) - 1]; + $paths = $rulesetNode->joinSelectors( $context, $selectors ); + } + + $rulesetNode->paths = $paths; + } + + $this->contexts[] = $paths; // different from less.js. Placed after joinSelectors() so that $this->contexts will get correct $paths + } + + public function visitRulesetOut() { + array_pop( $this->contexts ); + } + + public function visitMedia( $mediaNode ) { + $context = end( $this->contexts ); // $context = $this->contexts[ count($this->contexts) - 1]; + + if ( !count( $context ) || ( is_object( $context[0] ) && $context[0]->multiMedia ) ) { + $mediaNode->rules[0]->root = true; + } + } + +} diff --git a/less.php/lib/Less/Visitor/processExtends.php b/less.php/lib/Less/Visitor/processExtends.php new file mode 100755 index 0000000..65082a4 --- /dev/null +++ b/less.php/lib/Less/Visitor/processExtends.php @@ -0,0 +1,441 @@ +run( $root ); + if ( !$extendFinder->foundExtends ) { + return $root; + } + + $root->allExtends = $this->doExtendChaining( $root->allExtends, $root->allExtends ); + + $this->allExtendsStack = array(); + $this->allExtendsStack[] = &$root->allExtends; + + return $this->visitObj( $root ); + } + + private function doExtendChaining( $extendsList, $extendsListTarget, $iterationCount = 0 ) { + // + // chaining is different from normal extension.. if we extend an extend then we are not just copying, altering and pasting + // the selector we would do normally, but we are also adding an extend with the same target selector + // this means this new extend can then go and alter other extends + // + // this method deals with all the chaining work - without it, extend is flat and doesn't work on other extend selectors + // this is also the most expensive.. and a match on one selector can cause an extension of a selector we had already processed if + // we look at each selector at a time, as is done in visitRuleset + + $extendsToAdd = array(); + + // loop through comparing every extend with every target extend. + // a target extend is the one on the ruleset we are looking at copy/edit/pasting in place + // e.g. .a:extend(.b) {} and .b:extend(.c) {} then the first extend extends the second one + // and the second is the target. + // the separation into two lists allows us to process a subset of chains with a bigger set, as is the + // case when processing media queries + for ( $extendIndex = 0, $extendsList_len = count( $extendsList ); $extendIndex < $extendsList_len; $extendIndex++ ) { + for ( $targetExtendIndex = 0; $targetExtendIndex < count( $extendsListTarget ); $targetExtendIndex++ ) { + + $extend = $extendsList[$extendIndex]; + $targetExtend = $extendsListTarget[$targetExtendIndex]; + + // Optimisation: Explicit reference,  + if ( \array_key_exists( $targetExtend->object_id, $extend->parent_ids ) ) { + // ignore circular references + continue; + } + + // find a match in the target extends self selector (the bit before :extend) + $selectorPath = array( $targetExtend->selfSelectors[0] ); + $matches = $this->findMatch( $extend, $selectorPath ); + + if ( $matches ) { + + // we found a match, so for each self selector.. + foreach ( $extend->selfSelectors as $selfSelector ) { + + // process the extend as usual + $newSelector = $this->extendSelector( $matches, $selectorPath, $selfSelector ); + + // but now we create a new extend from it + $newExtend = new Less_Tree_Extend( $targetExtend->selector, $targetExtend->option, 0 ); + $newExtend->selfSelectors = $newSelector; + + // add the extend onto the list of extends for that selector + end( $newSelector )->extendList = array( $newExtend ); + // $newSelector[ count($newSelector)-1]->extendList = array($newExtend); + + // record that we need to add it. + $extendsToAdd[] = $newExtend; + $newExtend->ruleset = $targetExtend->ruleset; + + // remember its parents for circular references + $newExtend->parent_ids = array_merge( $newExtend->parent_ids, $targetExtend->parent_ids, $extend->parent_ids ); + + // only process the selector once.. if we have :extend(.a,.b) then multiple + // extends will look at the same selector path, so when extending + // we know that any others will be duplicates in terms of what is added to the css + if ( $targetExtend->firstExtendOnThisSelectorPath ) { + $newExtend->firstExtendOnThisSelectorPath = true; + $targetExtend->ruleset->paths[] = $newSelector; + } + } + } + } + } + + if ( $extendsToAdd ) { + // try to detect circular references to stop a stack overflow. + // may no longer be needed. $this->extendChainCount++; + if ( $iterationCount > 100 ) { + + try{ + $selectorOne = $extendsToAdd[0]->selfSelectors[0]->toCSS(); + $selectorTwo = $extendsToAdd[0]->selector->toCSS(); + }catch ( Exception $e ) { + $selectorOne = "{unable to calculate}"; + $selectorTwo = "{unable to calculate}"; + } + + throw new Less_Exception_Parser( "extend circular reference detected. One of the circular extends is currently:" . $selectorOne . ":extend(" . $selectorTwo . ")" ); + } + + // now process the new extends on the existing rules so that we can handle a extending b extending c ectending d extending e... + $extendsToAdd = $this->doExtendChaining( $extendsToAdd, $extendsListTarget, $iterationCount + 1 ); + } + + return array_merge( $extendsList, $extendsToAdd ); + } + + protected function visitRule( $ruleNode, &$visitDeeper ) { + $visitDeeper = false; + } + + protected function visitMixinDefinition( $mixinDefinitionNode, &$visitDeeper ) { + $visitDeeper = false; + } + + protected function visitSelector( $selectorNode, &$visitDeeper ) { + $visitDeeper = false; + } + + protected function visitRuleset( $rulesetNode ) { + if ( $rulesetNode->root ) { + return; + } + + $allExtends = end( $this->allExtendsStack ); + $paths_len = count( $rulesetNode->paths ); + + // look at each selector path in the ruleset, find any extend matches and then copy, find and replace + foreach ( $allExtends as $allExtend ) { + for ( $pathIndex = 0; $pathIndex < $paths_len; $pathIndex++ ) { + + // extending extends happens initially, before the main pass + if ( isset( $rulesetNode->extendOnEveryPath ) && $rulesetNode->extendOnEveryPath ) { + continue; + } + + $selectorPath = $rulesetNode->paths[$pathIndex]; + + if ( end( $selectorPath )->extendList ) { + continue; + } + + $this->ExtendMatch( $rulesetNode, $allExtend, $selectorPath ); + + } + } + } + + private function ExtendMatch( $rulesetNode, $extend, $selectorPath ) { + $matches = $this->findMatch( $extend, $selectorPath ); + + if ( $matches ) { + foreach ( $extend->selfSelectors as $selfSelector ) { + $rulesetNode->paths[] = $this->extendSelector( $matches, $selectorPath, $selfSelector ); + } + } + } + + private function findMatch( $extend, $haystackSelectorPath ) { + if ( !$this->HasMatches( $extend, $haystackSelectorPath ) ) { + return false; + } + + // + // look through the haystack selector path to try and find the needle - extend.selector + // returns an array of selector matches that can then be replaced + // + $needleElements = $extend->selector->elements; + $potentialMatches = array(); + $potentialMatches_len = 0; + $potentialMatch = null; + $matches = array(); + + // loop through the haystack elements + $haystack_path_len = count( $haystackSelectorPath ); + for ( $haystackSelectorIndex = 0; $haystackSelectorIndex < $haystack_path_len; $haystackSelectorIndex++ ) { + $hackstackSelector = $haystackSelectorPath[$haystackSelectorIndex]; + + $haystack_elements_len = count( $hackstackSelector->elements ); + for ( $hackstackElementIndex = 0; $hackstackElementIndex < $haystack_elements_len; $hackstackElementIndex++ ) { + + $haystackElement = $hackstackSelector->elements[$hackstackElementIndex]; + + // if we allow elements before our match we can add a potential match every time. otherwise only at the first element. + if ( $extend->allowBefore || ( $haystackSelectorIndex === 0 && $hackstackElementIndex === 0 ) ) { + $potentialMatches[] = array( 'pathIndex' => $haystackSelectorIndex, 'index' => $hackstackElementIndex, 'matched' => 0, 'initialCombinator' => $haystackElement->combinator ); + $potentialMatches_len++; + } + + for ( $i = 0; $i < $potentialMatches_len; $i++ ) { + + $potentialMatch = &$potentialMatches[$i]; + $potentialMatch = $this->PotentialMatch( $potentialMatch, $needleElements, $haystackElement, $hackstackElementIndex ); + + // if we are still valid and have finished, test whether we have elements after and whether these are allowed + if ( $potentialMatch && $potentialMatch['matched'] === $extend->selector->elements_len ) { + $potentialMatch['finished'] = true; + + if ( !$extend->allowAfter && ( $hackstackElementIndex + 1 < $haystack_elements_len || $haystackSelectorIndex + 1 < $haystack_path_len ) ) { + $potentialMatch = null; + } + } + + // if null we remove, if not, we are still valid, so either push as a valid match or continue + if ( $potentialMatch ) { + if ( $potentialMatch['finished'] ) { + $potentialMatch['length'] = $extend->selector->elements_len; + $potentialMatch['endPathIndex'] = $haystackSelectorIndex; + $potentialMatch['endPathElementIndex'] = $hackstackElementIndex + 1; // index after end of match + $potentialMatches = array(); // we don't allow matches to overlap, so start matching again + $potentialMatches_len = 0; + $matches[] = $potentialMatch; + } + continue; + } + + array_splice( $potentialMatches, $i, 1 ); + $potentialMatches_len--; + $i--; + } + } + } + + return $matches; + } + + // Before going through all the nested loops, lets check to see if a match is possible + // Reduces Bootstrap 3.1 compile time from ~6.5s to ~5.6s + private function HasMatches( $extend, $haystackSelectorPath ) { + if ( !$extend->selector->cacheable ) { + return true; + } + + $first_el = $extend->selector->_oelements[0]; + + foreach ( $haystackSelectorPath as $hackstackSelector ) { + if ( !$hackstackSelector->cacheable ) { + return true; + } + + // Optimisation: Explicit reference,  + if ( \array_key_exists( $first_el, $hackstackSelector->_oelements_assoc ) ) { + return true; + } + } + + return false; + } + + /** + * @param integer $hackstackElementIndex + */ + private function PotentialMatch( $potentialMatch, $needleElements, $haystackElement, $hackstackElementIndex ) { + if ( $potentialMatch['matched'] > 0 ) { + + // selectors add " " onto the first element. When we use & it joins the selectors together, but if we don't + // then each selector in haystackSelectorPath has a space before it added in the toCSS phase. so we need to work out + // what the resulting combinator will be + $targetCombinator = $haystackElement->combinator; + if ( $targetCombinator === '' && $hackstackElementIndex === 0 ) { + $targetCombinator = ' '; + } + + if ( $needleElements[ $potentialMatch['matched'] ]->combinator !== $targetCombinator ) { + return null; + } + } + + // if we don't match, null our match to indicate failure + if ( !$this->isElementValuesEqual( $needleElements[$potentialMatch['matched'] ]->value, $haystackElement->value ) ) { + return null; + } + + $potentialMatch['finished'] = false; + $potentialMatch['matched']++; + + return $potentialMatch; + } + + private function isElementValuesEqual( $elementValue1, $elementValue2 ) { + if ( $elementValue1 === $elementValue2 ) { + return true; + } + + if ( is_string( $elementValue1 ) || is_string( $elementValue2 ) ) { + return false; + } + + if ( $elementValue1 instanceof Less_Tree_Attribute ) { + return $this->isAttributeValuesEqual( $elementValue1, $elementValue2 ); + } + + $elementValue1 = $elementValue1->value; + if ( $elementValue1 instanceof Less_Tree_Selector ) { + return $this->isSelectorValuesEqual( $elementValue1, $elementValue2 ); + } + + return false; + } + + /** + * @param Less_Tree_Selector $elementValue1 + */ + private function isSelectorValuesEqual( $elementValue1, $elementValue2 ) { + $elementValue2 = $elementValue2->value; + if ( !( $elementValue2 instanceof Less_Tree_Selector ) || $elementValue1->elements_len !== $elementValue2->elements_len ) { + return false; + } + + for ( $i = 0; $i < $elementValue1->elements_len; $i++ ) { + + if ( $elementValue1->elements[$i]->combinator !== $elementValue2->elements[$i]->combinator ) { + if ( $i !== 0 || ( $elementValue1->elements[$i]->combinator || ' ' ) !== ( $elementValue2->elements[$i]->combinator || ' ' ) ) { + return false; + } + } + + if ( !$this->isElementValuesEqual( $elementValue1->elements[$i]->value, $elementValue2->elements[$i]->value ) ) { + return false; + } + } + + return true; + } + + /** + * @param Less_Tree_Attribute $elementValue1 + */ + private function isAttributeValuesEqual( $elementValue1, $elementValue2 ) { + if ( $elementValue1->op !== $elementValue2->op || $elementValue1->key !== $elementValue2->key ) { + return false; + } + + if ( !$elementValue1->value || !$elementValue2->value ) { + if ( $elementValue1->value || $elementValue2->value ) { + return false; + } + return true; + } + + $elementValue1 = ( $elementValue1->value->value ? $elementValue1->value->value : $elementValue1->value ); + $elementValue2 = ( $elementValue2->value->value ? $elementValue2->value->value : $elementValue2->value ); + + return $elementValue1 === $elementValue2; + } + + private function extendSelector( $matches, $selectorPath, $replacementSelector ) { + // for a set of matches, replace each match with the replacement selector + + $currentSelectorPathIndex = 0; + $currentSelectorPathElementIndex = 0; + $path = array(); + $selectorPath_len = count( $selectorPath ); + + for ( $matchIndex = 0, $matches_len = count( $matches ); $matchIndex < $matches_len; $matchIndex++ ) { + + $match = $matches[$matchIndex]; + $selector = $selectorPath[ $match['pathIndex'] ]; + + $firstElement = new Less_Tree_Element( + $match['initialCombinator'], + $replacementSelector->elements[0]->value, + $replacementSelector->elements[0]->index, + $replacementSelector->elements[0]->currentFileInfo + ); + + if ( $match['pathIndex'] > $currentSelectorPathIndex && $currentSelectorPathElementIndex > 0 ) { + $last_path = end( $path ); + $last_path->elements = array_merge( $last_path->elements, array_slice( $selectorPath[$currentSelectorPathIndex]->elements, $currentSelectorPathElementIndex ) ); + $currentSelectorPathElementIndex = 0; + $currentSelectorPathIndex++; + } + + $newElements = array_merge( + array_slice( $selector->elements, $currentSelectorPathElementIndex, ( $match['index'] - $currentSelectorPathElementIndex ) ), // last parameter of array_slice is different than the last parameter of javascript's slice + array( $firstElement ), + array_slice( $replacementSelector->elements, 1 ) + ); + + if ( $currentSelectorPathIndex === $match['pathIndex'] && $matchIndex > 0 ) { + $last_key = count( $path ) - 1; + $path[$last_key]->elements = array_merge( $path[$last_key]->elements, $newElements ); + } else { + $path = array_merge( $path, array_slice( $selectorPath, $currentSelectorPathIndex, $match['pathIndex'] ) ); + $path[] = new Less_Tree_Selector( $newElements ); + } + + $currentSelectorPathIndex = $match['endPathIndex']; + $currentSelectorPathElementIndex = $match['endPathElementIndex']; + if ( $currentSelectorPathElementIndex >= count( $selectorPath[$currentSelectorPathIndex]->elements ) ) { + $currentSelectorPathElementIndex = 0; + $currentSelectorPathIndex++; + } + } + + if ( $currentSelectorPathIndex < $selectorPath_len && $currentSelectorPathElementIndex > 0 ) { + $last_path = end( $path ); + $last_path->elements = array_merge( $last_path->elements, array_slice( $selectorPath[$currentSelectorPathIndex]->elements, $currentSelectorPathElementIndex ) ); + $currentSelectorPathIndex++; + } + + $slice_len = $selectorPath_len - $currentSelectorPathIndex; + $path = array_merge( $path, array_slice( $selectorPath, $currentSelectorPathIndex, $slice_len ) ); + + return $path; + } + + protected function visitMedia( $mediaNode ) { + $newAllExtends = array_merge( $mediaNode->allExtends, end( $this->allExtendsStack ) ); + $this->allExtendsStack[] = $this->doExtendChaining( $newAllExtends, $mediaNode->allExtends ); + } + + protected function visitMediaOut() { + array_pop( $this->allExtendsStack ); + } + + protected function visitDirective( $directiveNode ) { + $newAllExtends = array_merge( $directiveNode->allExtends, end( $this->allExtendsStack ) ); + $this->allExtendsStack[] = $this->doExtendChaining( $newAllExtends, $directiveNode->allExtends ); + } + + protected function visitDirectiveOut() { + array_pop( $this->allExtendsStack ); + } + +} diff --git a/less.php/lib/Less/Visitor/toCSS.php b/less.php/lib/Less/Visitor/toCSS.php new file mode 100755 index 0000000..e90f211 --- /dev/null +++ b/less.php/lib/Less/Visitor/toCSS.php @@ -0,0 +1,280 @@ +visitObj( $root ); + } + + public function visitRule( $ruleNode ) { + if ( $ruleNode->variable ) { + return array(); + } + return $ruleNode; + } + + public function visitMixinDefinition( $mixinNode ) { + // mixin definitions do not get eval'd - this means they keep state + // so we have to clear that state here so it isn't used if toCSS is called twice + $mixinNode->frames = array(); + return array(); + } + + public function visitExtend() { + return array(); + } + + public function visitComment( $commentNode ) { + if ( $commentNode->isSilent() ) { + return array(); + } + return $commentNode; + } + + public function visitMedia( $mediaNode, &$visitDeeper ) { + $mediaNode->accept( $this ); + $visitDeeper = false; + + if ( !$mediaNode->rules ) { + return array(); + } + return $mediaNode; + } + + public function visitDirective( $directiveNode ) { + if ( isset( $directiveNode->currentFileInfo['reference'] ) && ( !property_exists( $directiveNode, 'isReferenced' ) || !$directiveNode->isReferenced ) ) { + return array(); + } + if ( $directiveNode->name === '@charset' ) { + // Only output the debug info together with subsequent @charset definitions + // a comment (or @media statement) before the actual @charset directive would + // be considered illegal css as it has to be on the first line + if ( isset( $this->charset ) && $this->charset ) { + + // if( $directiveNode->debugInfo ){ + // $comment = new Less_Tree_Comment('/* ' . str_replace("\n",'',$directiveNode->toCSS())." */\n"); + // $comment->debugInfo = $directiveNode->debugInfo; + // return $this->visit($comment); + //} + + return array(); + } + $this->charset = true; + } + return $directiveNode; + } + + public function checkPropertiesInRoot( $rulesetNode ) { + if ( !$rulesetNode->firstRoot ) { + return; + } + + foreach ( $rulesetNode->rules as $ruleNode ) { + if ( $ruleNode instanceof Less_Tree_Rule && !$ruleNode->variable ) { + $msg = "properties must be inside selector blocks, they cannot be in the root. Index ".$ruleNode->index.( $ruleNode->currentFileInfo ? ( ' Filename: '.$ruleNode->currentFileInfo['filename'] ) : null ); + throw new Less_Exception_Compiler( $msg ); + } + } + } + + public function visitRuleset( $rulesetNode, &$visitDeeper ) { + $visitDeeper = false; + + $this->checkPropertiesInRoot( $rulesetNode ); + + if ( $rulesetNode->root ) { + return $this->visitRulesetRoot( $rulesetNode ); + } + + $rulesets = array(); + $rulesetNode->paths = $this->visitRulesetPaths( $rulesetNode ); + + // Compile rules and rulesets + $nodeRuleCnt = $rulesetNode->rules ? count( $rulesetNode->rules ) : 0; + for ( $i = 0; $i < $nodeRuleCnt; ) { + $rule = $rulesetNode->rules[$i]; + + if ( property_exists( $rule, 'rules' ) ) { + // visit because we are moving them out from being a child + $rulesets[] = $this->visitObj( $rule ); + array_splice( $rulesetNode->rules, $i, 1 ); + $nodeRuleCnt--; + continue; + } + $i++; + } + + // accept the visitor to remove rules and refactor itself + // then we can decide now whether we want it or not + if ( $nodeRuleCnt > 0 ) { + $rulesetNode->accept( $this ); + + if ( $rulesetNode->rules ) { + + if ( count( $rulesetNode->rules ) > 1 ) { + $this->_mergeRules( $rulesetNode->rules ); + $this->_removeDuplicateRules( $rulesetNode->rules ); + } + + // now decide whether we keep the ruleset + if ( $rulesetNode->paths ) { + // array_unshift($rulesets, $rulesetNode); + array_splice( $rulesets, 0, 0, array( $rulesetNode ) ); + } + } + + } + + if ( count( $rulesets ) === 1 ) { + return $rulesets[0]; + } + return $rulesets; + } + + /** + * Helper function for visitiRuleset + * + * return array|Less_Tree_Ruleset + */ + private function visitRulesetRoot( $rulesetNode ) { + $rulesetNode->accept( $this ); + if ( $rulesetNode->firstRoot || $rulesetNode->rules ) { + return $rulesetNode; + } + return array(); + } + + /** + * Helper function for visitRuleset() + * + * @return array + */ + private function visitRulesetPaths( $rulesetNode ) { + $paths = array(); + foreach ( $rulesetNode->paths as $p ) { + if ( $p[0]->elements[0]->combinator === ' ' ) { + $p[0]->elements[0]->combinator = ''; + } + + foreach ( $p as $pi ) { + if ( $pi->getIsReferenced() && $pi->getIsOutput() ) { + $paths[] = $p; + break; + } + } + } + + return $paths; + } + + protected function _removeDuplicateRules( &$rules ) { + // remove duplicates + $ruleCache = array(); + for ( $i = count( $rules ) - 1; $i >= 0; $i-- ) { + $rule = $rules[$i]; + if ( $rule instanceof Less_Tree_Rule || $rule instanceof Less_Tree_NameValue ) { + + if ( !isset( $ruleCache[$rule->name] ) ) { + $ruleCache[$rule->name] = $rule; + } else { + $ruleList =& $ruleCache[$rule->name]; + + if ( $ruleList instanceof Less_Tree_Rule || $ruleList instanceof Less_Tree_NameValue ) { + $ruleList = $ruleCache[$rule->name] = array( $ruleCache[$rule->name]->toCSS() ); + } + + $ruleCSS = $rule->toCSS(); + if ( array_search( $ruleCSS, $ruleList ) !== false ) { + array_splice( $rules, $i, 1 ); + } else { + $ruleList[] = $ruleCSS; + } + } + } + } + } + + protected function _mergeRules( &$rules ) { + $groups = array(); + + // obj($rules); + + $rules_len = count( $rules ); + for ( $i = 0; $i < $rules_len; $i++ ) { + $rule = $rules[$i]; + + if ( ( $rule instanceof Less_Tree_Rule ) && $rule->merge ) { + + $key = $rule->name; + if ( $rule->important ) { + $key .= ',!'; + } + + if ( !isset( $groups[$key] ) ) { + $groups[$key] = array(); + } else { + array_splice( $rules, $i--, 1 ); + $rules_len--; + } + + $groups[$key][] = $rule; + } + } + + foreach ( $groups as $parts ) { + + if ( count( $parts ) > 1 ) { + $rule = $parts[0]; + $spacedGroups = array(); + $lastSpacedGroup = array(); + $parts_mapped = array(); + foreach ( $parts as $p ) { + if ( $p->merge === '+' ) { + if ( $lastSpacedGroup ) { + $spacedGroups[] = self::toExpression( $lastSpacedGroup ); + } + $lastSpacedGroup = array(); + } + $lastSpacedGroup[] = $p; + } + + $spacedGroups[] = self::toExpression( $lastSpacedGroup ); + $rule->value = self::toValue( $spacedGroups ); + } + } + + } + + public static function toExpression( $values ) { + $mapped = array(); + foreach ( $values as $p ) { + $mapped[] = $p->value; + } + return new Less_Tree_Expression( $mapped ); + } + + public static function toValue( $values ) { + // return new Less_Tree_Value($values); ?? + + $mapped = array(); + foreach ( $values as $p ) { + $mapped[] = $p; + } + return new Less_Tree_Value( $mapped ); + } +} diff --git a/less.php/lib/Less/VisitorReplacing.php b/less.php/lib/Less/VisitorReplacing.php new file mode 100755 index 0000000..cd401ad --- /dev/null +++ b/less.php/lib/Less/VisitorReplacing.php @@ -0,0 +1,70 @@ +type; + if ( isset( $this->_visitFnCache[$funcName] ) ) { + + $visitDeeper = true; + $node = $this->$funcName( $node, $visitDeeper ); + + if ( $node ) { + if ( $visitDeeper && is_object( $node ) ) { + $node->accept( $this ); + } + + $funcName = $funcName . "Out"; + if ( isset( $this->_visitFnCache[$funcName] ) ) { + $this->$funcName( $node ); + } + } + + } else { + $node->accept( $this ); + } + + return $node; + } + + public function visitArray( $nodes ) { + $newNodes = array(); + foreach ( $nodes as $node ) { + $evald = $this->visitObj( $node ); + if ( $evald ) { + if ( is_array( $evald ) ) { + self::flatten( $evald, $newNodes ); + } else { + $newNodes[] = $evald; + } + } + } + return $newNodes; + } + + public function flatten( $arr, &$out ) { + foreach ( $arr as $item ) { + if ( !is_array( $item ) ) { + $out[] = $item; + continue; + } + + foreach ( $item as $nestedItem ) { + if ( is_array( $nestedItem ) ) { + self::flatten( $nestedItem, $out ); + } else { + $out[] = $nestedItem; + } + } + } + + return $out; + } + +} diff --git a/mkht.php b/mkht.php new file mode 100755 index 0000000..882abd4 --- /dev/null +++ b/mkht.php @@ -0,0 +1,224 @@ +#!/usr/bin/php + CSS + +// Create CSS output directory if needed +if (!file_exists(SITE . "/css")) + mkdir(SITE . "/css", 0755); + +require ROOT . "/less.php/lib/Less/Autoloader.php"; +Less_Autoloader::register(); + +$colorScheme = array( + "darkColor" => "#2a2a2a", + "darkerColor" => "#222222", + "lightColor" => "white", + "lightlessColor" => "#eeeeee", + "mainColor" => "red", +); + +if ($config['trueBlack']) { + $colorScheme['darkColor'] = "#000000"; + $colorScheme['darkerColor'] = "#000000"; +} + +$options = array('cache_dir' => SITE . '/css', 'compress' => true); + +if (file_exists(SITE . "/style.less")) + $lessFiles = array(ROOT . '/style.less' => '', SITE . '/style.less' => ''); +else + $lessFiles = array(ROOT . '/style.less' => ''); + +define("CSS_FILENAME", Less_Cache::Get($lessFiles, $options, $colorScheme)); + +// URLs don't end with .html on the Antopie website +function formerUrlLocale($page) { + if (DESTINATION === "dns" OR DESTINATION === "onion") { + echo $page; + } else { + echo $page . ".html"; + } +} + +// Determine whether links need to use Onion or DNS +function clearnetOrOnion($clearnetUrl, $onionUrl) { + if (DESTINATION === "onion") { + return $onionUrl; + } else { + return $clearnetUrl; + } +} + +exec(FIND . " " . SITE . "/src -name '*.gmi' -o -name '*.md'", $pages); + +foreach ($pages as $page) { + + $pathParts = pathinfo(str_replace("/src", "", $page)); + + // Create parent directory if needed + if (!file_exists($pathParts['dirname'])) + mkdir($pathParts['dirname'], 0755, true); + + // Execute PHP code + ob_start(); + eval("?>" . file_get_contents($page)); + file_put_contents($pathParts['dirname'] . "/" . $pathParts['basename'], ob_get_contents()); + ob_end_clean(); + + // Convert Gemtext to Markdown + if ($pathParts['extension'] === "gmi") { + $gmilines = explode("\n", file_get_contents($pathParts['dirname'] . "/" . $pathParts['basename'])); + + if (substr($gmilines[0], 0, 2) === "# ") + $title = substr($gmilines[0], 2); + else + $title = NULL; + + foreach ($gmilines as $key => $line) { + if (substr($line, 0, 2) === "=>") { + preg_match("/=> +(.[^ ]+)/", $line, $lnUrl); + preg_match("/=> +.[^ ]+ +(.+)/", $line, $lnTitle); + + // Escape Markdown special characters + $mdSpecial = array("[", "]", "(", ")"); + $htmlEntities = array("[", "]", "(", ")"); + $lnUrl[1] = str_replace($mdSpecial, $htmlEntities, $lnUrl[1]); + + $urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH)); + + // .gmi > .md for local links + if (!str_contains($lnUrl[1], ":") AND $urlPathParts['extension'] === "gmi") // If it's a local link + $lnUrl[1] = $urlPathParts['dirname'] . "/" . $urlPathParts['filename'] . ".md"; + + if (isset($lnTitle[1])) { + $lnTitle[1] = str_replace($mdSpecial, $htmlEntities, $lnTitle[1]); + $gmilines[$key] = "[" . $lnTitle[1] . "](" . $lnUrl[1] . ")"; + } else { + $gmilines[$key] = "[" . $lnUrl[1] . "](" . $lnUrl[1] . ")"; + } + } + } + $code = ""; + foreach ($gmilines as $line) { + $code = $code . "\n" . $line; + } + file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md", $code); + } + + + // Compile Markdown to HTML with Parsedown + require_once ROOT . "/parsedown/Parsedown.php"; + require_once ROOT . "/parsedown-extra/ParsedownExtra.php"; + $Parsedown = new ParsedownExtra; + $Parsedown = $Parsedown->setUrlsLinked(false); + $Parsedown = $Parsedown->setMarkupEscaped(false); + $Parsedown = $Parsedown->setBreaksEnabled(true); + $pageContent = $Parsedown->text(file_get_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md")); + + + // .md > .html for local links + $pageContent = preg_replace('##', '', $pageContent); + + + // Add header and footer to HTML + + ob_start(); + + ?> + + + + + <?php + if (isset($title) AND !is_null($title)) + echo $title . " · " . $config['siteTitle']; + else + echo $config['siteTitle']; + ?> + + + + + + + + + +
+ + + +
+ + + '; + } else { + echo "
"; + } + echo $pageContent; + if ($config['centerIndex'] AND $pathParts['filename'] === "index") { + echo ""; + } else { + echo ""; + file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".html", ob_get_contents()); + ob_end_clean(); + + // Gzip compression + exec(GZIP . " --keep --fast --force " . $pathParts['dirname'] . "/" . $pathParts['filename'] . ".html"); +} + +exec(GZIP . " --keep --fast --force " . SITE . "/css/" . CSS_FILENAME); diff --git a/parsedown-extra/LICENSE.txt b/parsedown-extra/LICENSE.txt new file mode 100755 index 0000000..baca86f --- /dev/null +++ b/parsedown-extra/LICENSE.txt @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 Emanuil Rusev, erusev.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/parsedown-extra/ParsedownExtra.php b/parsedown-extra/ParsedownExtra.php new file mode 100755 index 0000000..8cdb5d2 --- /dev/null +++ b/parsedown-extra/ParsedownExtra.php @@ -0,0 +1,686 @@ +BlockTypes[':'] []= 'DefinitionList'; + $this->BlockTypes['*'] []= 'Abbreviation'; + + # identify footnote definitions before reference definitions + array_unshift($this->BlockTypes['['], 'Footnote'); + + # identify footnote markers before before links + array_unshift($this->InlineTypes['['], 'FootnoteMarker'); + } + + # + # ~ + + function text($text) + { + $Elements = $this->textElements($text); + + # convert to markup + $markup = $this->elements($Elements); + + # trim line breaks + $markup = trim($markup, "\n"); + + # merge consecutive dl elements + + $markup = preg_replace('/<\/dl>\s+
\s+/', '', $markup); + + # add footnotes + + if (isset($this->DefinitionData['Footnote'])) + { + $Element = $this->buildFootnoteElement(); + + $markup .= "\n" . $this->element($Element); + } + + return $markup; + } + + # + # Blocks + # + + # + # Abbreviation + + protected function blockAbbreviation($Line) + { + if (preg_match('/^\*\[(.+?)\]:[ ]*(.+?)[ ]*$/', $Line['text'], $matches)) + { + $this->DefinitionData['Abbreviation'][$matches[1]] = $matches[2]; + + $Block = array( + 'hidden' => true, + ); + + return $Block; + } + } + + # + # Footnote + + protected function blockFootnote($Line) + { + if (preg_match('/^\[\^(.+?)\]:[ ]?(.*)$/', $Line['text'], $matches)) + { + $Block = array( + 'label' => $matches[1], + 'text' => $matches[2], + 'hidden' => true, + ); + + return $Block; + } + } + + protected function blockFootnoteContinue($Line, $Block) + { + if ($Line['text'][0] === '[' and preg_match('/^\[\^(.+?)\]:/', $Line['text'])) + { + return; + } + + if (isset($Block['interrupted'])) + { + if ($Line['indent'] >= 4) + { + $Block['text'] .= "\n\n" . $Line['text']; + + return $Block; + } + } + else + { + $Block['text'] .= "\n" . $Line['text']; + + return $Block; + } + } + + protected function blockFootnoteComplete($Block) + { + $this->DefinitionData['Footnote'][$Block['label']] = array( + 'text' => $Block['text'], + 'count' => null, + 'number' => null, + ); + + return $Block; + } + + # + # Definition List + + protected function blockDefinitionList($Line, $Block) + { + if ( ! isset($Block) or $Block['type'] !== 'Paragraph') + { + return; + } + + $Element = array( + 'name' => 'dl', + 'elements' => array(), + ); + + $terms = explode("\n", $Block['element']['handler']['argument']); + + foreach ($terms as $term) + { + $Element['elements'] []= array( + 'name' => 'dt', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $term, + 'destination' => 'elements' + ), + ); + } + + $Block['element'] = $Element; + + $Block = $this->addDdElement($Line, $Block); + + return $Block; + } + + protected function blockDefinitionListContinue($Line, array $Block) + { + if ($Line['text'][0] === ':') + { + $Block = $this->addDdElement($Line, $Block); + + return $Block; + } + else + { + if (isset($Block['interrupted']) and $Line['indent'] === 0) + { + return; + } + + if (isset($Block['interrupted'])) + { + $Block['dd']['handler']['function'] = 'textElements'; + $Block['dd']['handler']['argument'] .= "\n\n"; + + $Block['dd']['handler']['destination'] = 'elements'; + + unset($Block['interrupted']); + } + + $text = substr($Line['body'], min($Line['indent'], 4)); + + $Block['dd']['handler']['argument'] .= "\n" . $text; + + return $Block; + } + } + + # + # Header + + protected function blockHeader($Line) + { + $Block = parent::blockHeader($Line); + + if ($Block !== null && preg_match('/[ #]*{('.$this->regexAttribute.'+)}[ ]*$/', $Block['element']['handler']['argument'], $matches, PREG_OFFSET_CAPTURE)) + { + $attributeString = $matches[1][0]; + + $Block['element']['attributes'] = $this->parseAttributeData($attributeString); + + $Block['element']['handler']['argument'] = substr($Block['element']['handler']['argument'], 0, $matches[0][1]); + } + + return $Block; + } + + # + # Markup + + protected function blockMarkup($Line) + { + if ($this->markupEscaped or $this->safeMode) + { + return; + } + + if (preg_match('/^<(\w[\w-]*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches)) + { + $element = strtolower($matches[1]); + + if (in_array($element, $this->textLevelElements)) + { + return; + } + + $Block = array( + 'name' => $matches[1], + 'depth' => 0, + 'element' => array( + 'rawHtml' => $Line['text'], + 'autobreak' => true, + ), + ); + + $length = strlen($matches[0]); + $remainder = substr($Line['text'], $length); + + if (trim($remainder) === '') + { + if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) + { + $Block['closed'] = true; + $Block['void'] = true; + } + } + else + { + if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) + { + return; + } + if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder)) + { + $Block['closed'] = true; + } + } + + return $Block; + } + } + + protected function blockMarkupContinue($Line, array $Block) + { + if (isset($Block['closed'])) + { + return; + } + + if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open + { + $Block['depth'] ++; + } + + if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close + { + if ($Block['depth'] > 0) + { + $Block['depth'] --; + } + else + { + $Block['closed'] = true; + } + } + + if (isset($Block['interrupted'])) + { + $Block['element']['rawHtml'] .= "\n"; + unset($Block['interrupted']); + } + + $Block['element']['rawHtml'] .= "\n".$Line['body']; + + return $Block; + } + + protected function blockMarkupComplete($Block) + { + if ( ! isset($Block['void'])) + { + $Block['element']['rawHtml'] = $this->processTag($Block['element']['rawHtml']); + } + + return $Block; + } + + # + # Setext + + protected function blockSetextHeader($Line, array $Block = null) + { + $Block = parent::blockSetextHeader($Line, $Block); + + if ($Block !== null && preg_match('/[ ]*{('.$this->regexAttribute.'+)}[ ]*$/', $Block['element']['handler']['argument'], $matches, PREG_OFFSET_CAPTURE)) + { + $attributeString = $matches[1][0]; + + $Block['element']['attributes'] = $this->parseAttributeData($attributeString); + + $Block['element']['handler']['argument'] = substr($Block['element']['handler']['argument'], 0, $matches[0][1]); + } + + return $Block; + } + + # + # Inline Elements + # + + # + # Footnote Marker + + protected function inlineFootnoteMarker($Excerpt) + { + if (preg_match('/^\[\^(.+?)\]/', $Excerpt['text'], $matches)) + { + $name = $matches[1]; + + if ( ! isset($this->DefinitionData['Footnote'][$name])) + { + return; + } + + $this->DefinitionData['Footnote'][$name]['count'] ++; + + if ( ! isset($this->DefinitionData['Footnote'][$name]['number'])) + { + $this->DefinitionData['Footnote'][$name]['number'] = ++ $this->footnoteCount; # » & + } + + $Element = array( + 'name' => 'sup', + 'attributes' => array('id' => 'fnref'.$this->DefinitionData['Footnote'][$name]['count'].':'.$name), + 'element' => array( + 'name' => 'a', + 'attributes' => array('href' => '#fn:'.$name, 'class' => 'footnote-ref'), + 'text' => $this->DefinitionData['Footnote'][$name]['number'], + ), + ); + + return array( + 'extent' => strlen($matches[0]), + 'element' => $Element, + ); + } + } + + private $footnoteCount = 0; + + # + # Link + + protected function inlineLink($Excerpt) + { + $Link = parent::inlineLink($Excerpt); + + $remainder = $Link !== null ? substr($Excerpt['text'], $Link['extent']) : ''; + + if (preg_match('/^[ ]*{('.$this->regexAttribute.'+)}/', $remainder, $matches)) + { + $Link['element']['attributes'] += $this->parseAttributeData($matches[1]); + + $Link['extent'] += strlen($matches[0]); + } + + return $Link; + } + + # + # ~ + # + + private $currentAbreviation; + private $currentMeaning; + + protected function insertAbreviation(array $Element) + { + if (isset($Element['text'])) + { + $Element['elements'] = self::pregReplaceElements( + '/\b'.preg_quote($this->currentAbreviation, '/').'\b/', + array( + array( + 'name' => 'abbr', + 'attributes' => array( + 'title' => $this->currentMeaning, + ), + 'text' => $this->currentAbreviation, + ) + ), + $Element['text'] + ); + + unset($Element['text']); + } + + return $Element; + } + + protected function inlineText($text) + { + $Inline = parent::inlineText($text); + + if (isset($this->DefinitionData['Abbreviation'])) + { + foreach ($this->DefinitionData['Abbreviation'] as $abbreviation => $meaning) + { + $this->currentAbreviation = $abbreviation; + $this->currentMeaning = $meaning; + + $Inline['element'] = $this->elementApplyRecursiveDepthFirst( + array($this, 'insertAbreviation'), + $Inline['element'] + ); + } + } + + return $Inline; + } + + # + # Util Methods + # + + protected function addDdElement(array $Line, array $Block) + { + $text = substr($Line['text'], 1); + $text = trim($text); + + unset($Block['dd']); + + $Block['dd'] = array( + 'name' => 'dd', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $text, + 'destination' => 'elements' + ), + ); + + if (isset($Block['interrupted'])) + { + $Block['dd']['handler']['function'] = 'textElements'; + + unset($Block['interrupted']); + } + + $Block['element']['elements'] []= & $Block['dd']; + + return $Block; + } + + protected function buildFootnoteElement() + { + $Element = array( + 'name' => 'div', + 'attributes' => array('class' => 'footnotes'), + 'elements' => array( + array('name' => 'hr'), + array( + 'name' => 'ol', + 'elements' => array(), + ), + ), + ); + + uasort($this->DefinitionData['Footnote'], 'self::sortFootnotes'); + + foreach ($this->DefinitionData['Footnote'] as $definitionId => $DefinitionData) + { + if ( ! isset($DefinitionData['number'])) + { + continue; + } + + $text = $DefinitionData['text']; + + $textElements = parent::textElements($text); + + $numbers = range(1, $DefinitionData['count']); + + $backLinkElements = array(); + + foreach ($numbers as $number) + { + $backLinkElements[] = array('text' => ' '); + $backLinkElements[] = array( + 'name' => 'a', + 'attributes' => array( + 'href' => "#fnref$number:$definitionId", + 'rev' => 'footnote', + 'class' => 'footnote-backref', + ), + 'rawHtml' => '↩', + 'allowRawHtmlInSafeMode' => true, + 'autobreak' => false, + ); + } + + unset($backLinkElements[0]); + + $n = count($textElements) -1; + + if ($textElements[$n]['name'] === 'p') + { + $backLinkElements = array_merge( + array( + array( + 'rawHtml' => ' ', + 'allowRawHtmlInSafeMode' => true, + ), + ), + $backLinkElements + ); + + unset($textElements[$n]['name']); + + $textElements[$n] = array( + 'name' => 'p', + 'elements' => array_merge( + array($textElements[$n]), + $backLinkElements + ), + ); + } + else + { + $textElements[] = array( + 'name' => 'p', + 'elements' => $backLinkElements + ); + } + + $Element['elements'][1]['elements'] []= array( + 'name' => 'li', + 'attributes' => array('id' => 'fn:'.$definitionId), + 'elements' => array_merge( + $textElements + ), + ); + } + + return $Element; + } + + # ~ + + protected function parseAttributeData($attributeString) + { + $Data = array(); + + $attributes = preg_split('/[ ]+/', $attributeString, - 1, PREG_SPLIT_NO_EMPTY); + + foreach ($attributes as $attribute) + { + if ($attribute[0] === '#') + { + $Data['id'] = substr($attribute, 1); + } + else # "." + { + $classes []= substr($attribute, 1); + } + } + + if (isset($classes)) + { + $Data['class'] = implode(' ', $classes); + } + + return $Data; + } + + # ~ + + protected function processTag($elementMarkup) # recursive + { + # http://stackoverflow.com/q/1148928/200145 + libxml_use_internal_errors(true); + + $DOMDocument = new DOMDocument; + + # http://stackoverflow.com/q/11309194/200145 + $elementMarkup = mb_convert_encoding($elementMarkup, 'HTML-ENTITIES', 'UTF-8'); + + # http://stackoverflow.com/q/4879946/200145 + $DOMDocument->loadHTML($elementMarkup); + $DOMDocument->removeChild($DOMDocument->doctype); + $DOMDocument->replaceChild($DOMDocument->firstChild->firstChild->firstChild, $DOMDocument->firstChild); + + $elementText = ''; + + if ($DOMDocument->documentElement->getAttribute('markdown') === '1') + { + foreach ($DOMDocument->documentElement->childNodes as $Node) + { + $elementText .= $DOMDocument->saveHTML($Node); + } + + $DOMDocument->documentElement->removeAttribute('markdown'); + + $elementText = "\n".$this->text($elementText)."\n"; + } + else + { + foreach ($DOMDocument->documentElement->childNodes as $Node) + { + $nodeMarkup = $DOMDocument->saveHTML($Node); + + if ($Node instanceof DOMElement and ! in_array($Node->nodeName, $this->textLevelElements)) + { + $elementText .= $this->processTag($nodeMarkup); + } + else + { + $elementText .= $nodeMarkup; + } + } + } + + # because we don't want for markup to get encoded + $DOMDocument->documentElement->nodeValue = 'placeholder\x1A'; + + $markup = $DOMDocument->saveHTML($DOMDocument->documentElement); + $markup = str_replace('placeholder\x1A', $elementText, $markup); + + return $markup; + } + + # ~ + + protected function sortFootnotes($A, $B) # callback + { + return $A['number'] - $B['number']; + } + + # + # Fields + # + + protected $regexAttribute = '(?:[#.][-\w]+[ ]*)'; +} diff --git a/parsedown-extra/README.md b/parsedown-extra/README.md new file mode 100755 index 0000000..cee4b54 --- /dev/null +++ b/parsedown-extra/README.md @@ -0,0 +1,31 @@ +> You might also like [Caret](http://caret.io?ref=parsedown) - our Markdown editor for the Desktop. + +## Parsedown Extra + +[![Build Status](https://img.shields.io/travis/erusev/parsedown-extra/master.svg?style=flat-square)](https://travis-ci.org/erusev/parsedown-extra) + +An extension of [Parsedown](http://parsedown.org) that adds support for [Markdown Extra](https://michelf.ca/projects/php-markdown/extra/). + +[See Demo](http://parsedown.org/extra/) + +### Installation + +Include both `Parsedown.php` and `ParsedownExtra.php` or install [the composer package](https://packagist.org/packages/erusev/parsedown-extra). + +### Example + +``` php +$Extra = new ParsedownExtra(); + +echo $Extra->text('# Header {.sth}'); # prints:

Header

+``` + +### Questions + +**Who uses Parsedown Extra?** + +[October CMS](http://octobercms.com/), [Bolt CMS](http://bolt.cm/), [Kirby CMS](http://getkirby.com/), [Grav CMS](http://getgrav.org/), [Statamic CMS](http://www.statamic.com/) and [more](https://www.versioneye.com/php/erusev:parsedown-extra/references). + +**How can I help?** + +Use it, star it, share it and in case you feel generous, [donate some money](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=528P3NZQMP8N2). diff --git a/parsedown/LICENSE.txt b/parsedown/LICENSE.txt new file mode 100755 index 0000000..8e7c764 --- /dev/null +++ b/parsedown/LICENSE.txt @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013-2018 Emanuil Rusev, erusev.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/parsedown/Parsedown.php b/parsedown/Parsedown.php new file mode 100755 index 0000000..ae0cbde --- /dev/null +++ b/parsedown/Parsedown.php @@ -0,0 +1,1994 @@ +textElements($text); + + # convert to markup + $markup = $this->elements($Elements); + + # trim line breaks + $markup = trim($markup, "\n"); + + return $markup; + } + + protected function textElements($text) + { + # make sure no definitions are set + $this->DefinitionData = array(); + + # standardize line breaks + $text = str_replace(array("\r\n", "\r"), "\n", $text); + + # remove surrounding line breaks + $text = trim($text, "\n"); + + # split text into lines + $lines = explode("\n", $text); + + # iterate through lines to identify blocks + return $this->linesElements($lines); + } + + # + # Setters + # + + function setBreaksEnabled($breaksEnabled) + { + $this->breaksEnabled = $breaksEnabled; + + return $this; + } + + protected $breaksEnabled; + + function setMarkupEscaped($markupEscaped) + { + $this->markupEscaped = $markupEscaped; + + return $this; + } + + protected $markupEscaped; + + function setUrlsLinked($urlsLinked) + { + $this->urlsLinked = $urlsLinked; + + return $this; + } + + protected $urlsLinked = true; + + function setSafeMode($safeMode) + { + $this->safeMode = (bool) $safeMode; + + return $this; + } + + protected $safeMode; + + function setStrictMode($strictMode) + { + $this->strictMode = (bool) $strictMode; + + return $this; + } + + protected $strictMode; + + protected $safeLinksWhitelist = array( + 'http://', + 'https://', + 'ftp://', + 'ftps://', + 'mailto:', + 'tel:', + 'data:image/png;base64,', + 'data:image/gif;base64,', + 'data:image/jpeg;base64,', + 'irc:', + 'ircs:', + 'git:', + 'ssh:', + 'news:', + 'steam:', + ); + + # + # Lines + # + + protected $BlockTypes = array( + '#' => array('Header'), + '*' => array('Rule', 'List'), + '+' => array('List'), + '-' => array('SetextHeader', 'Table', 'Rule', 'List'), + '0' => array('List'), + '1' => array('List'), + '2' => array('List'), + '3' => array('List'), + '4' => array('List'), + '5' => array('List'), + '6' => array('List'), + '7' => array('List'), + '8' => array('List'), + '9' => array('List'), + ':' => array('Table'), + '<' => array('Comment', 'Markup'), + '=' => array('SetextHeader'), + '>' => array('Quote'), + '[' => array('Reference'), + '_' => array('Rule'), + '`' => array('FencedCode'), + '|' => array('Table'), + '~' => array('FencedCode'), + ); + + # ~ + + protected $unmarkedBlockTypes = array( + 'Code', + ); + + # + # Blocks + # + + protected function lines(array $lines) + { + return $this->elements($this->linesElements($lines)); + } + + protected function linesElements(array $lines) + { + $Elements = array(); + $CurrentBlock = null; + + foreach ($lines as $line) + { + if (chop($line) === '') + { + if (isset($CurrentBlock)) + { + $CurrentBlock['interrupted'] = (isset($CurrentBlock['interrupted']) + ? $CurrentBlock['interrupted'] + 1 : 1 + ); + } + + continue; + } + + while (($beforeTab = strstr($line, "\t", true)) !== false) + { + $shortage = 4 - mb_strlen($beforeTab, 'utf-8') % 4; + + $line = $beforeTab + . str_repeat(' ', $shortage) + . substr($line, strlen($beforeTab) + 1) + ; + } + + $indent = strspn($line, ' '); + + $text = $indent > 0 ? substr($line, $indent) : $line; + + # ~ + + $Line = array('body' => $line, 'indent' => $indent, 'text' => $text); + + # ~ + + if (isset($CurrentBlock['continuable'])) + { + $methodName = 'block' . $CurrentBlock['type'] . 'Continue'; + $Block = $this->$methodName($Line, $CurrentBlock); + + if (isset($Block)) + { + $CurrentBlock = $Block; + + continue; + } + else + { + if ($this->isBlockCompletable($CurrentBlock['type'])) + { + $methodName = 'block' . $CurrentBlock['type'] . 'Complete'; + $CurrentBlock = $this->$methodName($CurrentBlock); + } + } + } + + # ~ + + $marker = $text[0]; + + # ~ + + $blockTypes = $this->unmarkedBlockTypes; + + if (isset($this->BlockTypes[$marker])) + { + foreach ($this->BlockTypes[$marker] as $blockType) + { + $blockTypes []= $blockType; + } + } + + # + # ~ + + foreach ($blockTypes as $blockType) + { + $Block = $this->{"block$blockType"}($Line, $CurrentBlock); + + if (isset($Block)) + { + $Block['type'] = $blockType; + + if ( ! isset($Block['identified'])) + { + if (isset($CurrentBlock)) + { + $Elements[] = $this->extractElement($CurrentBlock); + } + + $Block['identified'] = true; + } + + if ($this->isBlockContinuable($blockType)) + { + $Block['continuable'] = true; + } + + $CurrentBlock = $Block; + + continue 2; + } + } + + # ~ + + if (isset($CurrentBlock) and $CurrentBlock['type'] === 'Paragraph') + { + $Block = $this->paragraphContinue($Line, $CurrentBlock); + } + + if (isset($Block)) + { + $CurrentBlock = $Block; + } + else + { + if (isset($CurrentBlock)) + { + $Elements[] = $this->extractElement($CurrentBlock); + } + + $CurrentBlock = $this->paragraph($Line); + + $CurrentBlock['identified'] = true; + } + } + + # ~ + + if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type'])) + { + $methodName = 'block' . $CurrentBlock['type'] . 'Complete'; + $CurrentBlock = $this->$methodName($CurrentBlock); + } + + # ~ + + if (isset($CurrentBlock)) + { + $Elements[] = $this->extractElement($CurrentBlock); + } + + # ~ + + return $Elements; + } + + protected function extractElement(array $Component) + { + if ( ! isset($Component['element'])) + { + if (isset($Component['markup'])) + { + $Component['element'] = array('rawHtml' => $Component['markup']); + } + elseif (isset($Component['hidden'])) + { + $Component['element'] = array(); + } + } + + return $Component['element']; + } + + protected function isBlockContinuable($Type) + { + return method_exists($this, 'block' . $Type . 'Continue'); + } + + protected function isBlockCompletable($Type) + { + return method_exists($this, 'block' . $Type . 'Complete'); + } + + # + # Code + + protected function blockCode($Line, $Block = null) + { + if (isset($Block) and $Block['type'] === 'Paragraph' and ! isset($Block['interrupted'])) + { + return; + } + + if ($Line['indent'] >= 4) + { + $text = substr($Line['body'], 4); + + $Block = array( + 'element' => array( + 'name' => 'pre', + 'element' => array( + 'name' => 'code', + 'text' => $text, + ), + ), + ); + + return $Block; + } + } + + protected function blockCodeContinue($Line, $Block) + { + if ($Line['indent'] >= 4) + { + if (isset($Block['interrupted'])) + { + $Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']); + + unset($Block['interrupted']); + } + + $Block['element']['element']['text'] .= "\n"; + + $text = substr($Line['body'], 4); + + $Block['element']['element']['text'] .= $text; + + return $Block; + } + } + + protected function blockCodeComplete($Block) + { + return $Block; + } + + # + # Comment + + protected function blockComment($Line) + { + if ($this->markupEscaped or $this->safeMode) + { + return; + } + + if (strpos($Line['text'], '') !== false) + { + $Block['closed'] = true; + } + + return $Block; + } + } + + protected function blockCommentContinue($Line, array $Block) + { + if (isset($Block['closed'])) + { + return; + } + + $Block['element']['rawHtml'] .= "\n" . $Line['body']; + + if (strpos($Line['text'], '-->') !== false) + { + $Block['closed'] = true; + } + + return $Block; + } + + # + # Fenced Code + + protected function blockFencedCode($Line) + { + $marker = $Line['text'][0]; + + $openerLength = strspn($Line['text'], $marker); + + if ($openerLength < 3) + { + return; + } + + $infostring = trim(substr($Line['text'], $openerLength), "\t "); + + if (strpos($infostring, '`') !== false) + { + return; + } + + $Element = array( + 'name' => 'code', + 'text' => '', + ); + + if ($infostring !== '') + { + /** + * https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes + * Every HTML element may have a class attribute specified. + * The attribute, if specified, must have a value that is a set + * of space-separated tokens representing the various classes + * that the element belongs to. + * [...] + * The space characters, for the purposes of this specification, + * are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), + * U+000A LINE FEED (LF), U+000C FORM FEED (FF), and + * U+000D CARRIAGE RETURN (CR). + */ + $language = substr($infostring, 0, strcspn($infostring, " \t\n\f\r")); + + $Element['attributes'] = array('class' => "language-$language"); + } + + $Block = array( + 'char' => $marker, + 'openerLength' => $openerLength, + 'element' => array( + 'name' => 'pre', + 'element' => $Element, + ), + ); + + return $Block; + } + + protected function blockFencedCodeContinue($Line, $Block) + { + if (isset($Block['complete'])) + { + return; + } + + if (isset($Block['interrupted'])) + { + $Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']); + + unset($Block['interrupted']); + } + + if (($len = strspn($Line['text'], $Block['char'])) >= $Block['openerLength'] + and chop(substr($Line['text'], $len), ' ') === '' + ) { + $Block['element']['element']['text'] = substr($Block['element']['element']['text'], 1); + + $Block['complete'] = true; + + return $Block; + } + + $Block['element']['element']['text'] .= "\n" . $Line['body']; + + return $Block; + } + + protected function blockFencedCodeComplete($Block) + { + return $Block; + } + + # + # Header + + protected function blockHeader($Line) + { + $level = strspn($Line['text'], '#'); + + if ($level > 6) + { + return; + } + + $text = trim($Line['text'], '#'); + + if ($this->strictMode and isset($text[0]) and $text[0] !== ' ') + { + return; + } + + $text = trim($text, ' '); + + $Block = array( + 'element' => array( + 'name' => 'h' . $level, + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $text, + 'destination' => 'elements', + ) + ), + ); + + return $Block; + } + + # + # List + + protected function blockList($Line, array $CurrentBlock = null) + { + list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]{1,9}+[.\)]'); + + if (preg_match('/^('.$pattern.'([ ]++|$))(.*+)/', $Line['text'], $matches)) + { + $contentIndent = strlen($matches[2]); + + if ($contentIndent >= 5) + { + $contentIndent -= 1; + $matches[1] = substr($matches[1], 0, -$contentIndent); + $matches[3] = str_repeat(' ', $contentIndent) . $matches[3]; + } + elseif ($contentIndent === 0) + { + $matches[1] .= ' '; + } + + $markerWithoutWhitespace = strstr($matches[1], ' ', true); + + $Block = array( + 'indent' => $Line['indent'], + 'pattern' => $pattern, + 'data' => array( + 'type' => $name, + 'marker' => $matches[1], + 'markerType' => ($name === 'ul' ? $markerWithoutWhitespace : substr($markerWithoutWhitespace, -1)), + ), + 'element' => array( + 'name' => $name, + 'elements' => array(), + ), + ); + $Block['data']['markerTypeRegex'] = preg_quote($Block['data']['markerType'], '/'); + + if ($name === 'ol') + { + $listStart = ltrim(strstr($matches[1], $Block['data']['markerType'], true), '0') ?: '0'; + + if ($listStart !== '1') + { + if ( + isset($CurrentBlock) + and $CurrentBlock['type'] === 'Paragraph' + and ! isset($CurrentBlock['interrupted']) + ) { + return; + } + + $Block['element']['attributes'] = array('start' => $listStart); + } + } + + $Block['li'] = array( + 'name' => 'li', + 'handler' => array( + 'function' => 'li', + 'argument' => !empty($matches[3]) ? array($matches[3]) : array(), + 'destination' => 'elements' + ) + ); + + $Block['element']['elements'] []= & $Block['li']; + + return $Block; + } + } + + protected function blockListContinue($Line, array $Block) + { + if (isset($Block['interrupted']) and empty($Block['li']['handler']['argument'])) + { + return null; + } + + $requiredIndent = ($Block['indent'] + strlen($Block['data']['marker'])); + + if ($Line['indent'] < $requiredIndent + and ( + ( + $Block['data']['type'] === 'ol' + and preg_match('/^[0-9]++'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) + ) or ( + $Block['data']['type'] === 'ul' + and preg_match('/^'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) + ) + ) + ) { + if (isset($Block['interrupted'])) + { + $Block['li']['handler']['argument'] []= ''; + + $Block['loose'] = true; + + unset($Block['interrupted']); + } + + unset($Block['li']); + + $text = isset($matches[1]) ? $matches[1] : ''; + + $Block['indent'] = $Line['indent']; + + $Block['li'] = array( + 'name' => 'li', + 'handler' => array( + 'function' => 'li', + 'argument' => array($text), + 'destination' => 'elements' + ) + ); + + $Block['element']['elements'] []= & $Block['li']; + + return $Block; + } + elseif ($Line['indent'] < $requiredIndent and $this->blockList($Line)) + { + return null; + } + + if ($Line['text'][0] === '[' and $this->blockReference($Line)) + { + return $Block; + } + + if ($Line['indent'] >= $requiredIndent) + { + if (isset($Block['interrupted'])) + { + $Block['li']['handler']['argument'] []= ''; + + $Block['loose'] = true; + + unset($Block['interrupted']); + } + + $text = substr($Line['body'], $requiredIndent); + + $Block['li']['handler']['argument'] []= $text; + + return $Block; + } + + if ( ! isset($Block['interrupted'])) + { + $text = preg_replace('/^[ ]{0,'.$requiredIndent.'}+/', '', $Line['body']); + + $Block['li']['handler']['argument'] []= $text; + + return $Block; + } + } + + protected function blockListComplete(array $Block) + { + if (isset($Block['loose'])) + { + foreach ($Block['element']['elements'] as &$li) + { + if (end($li['handler']['argument']) !== '') + { + $li['handler']['argument'] []= ''; + } + } + } + + return $Block; + } + + # + # Quote + + protected function blockQuote($Line) + { + if (preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) + { + $Block = array( + 'element' => array( + 'name' => 'blockquote', + 'handler' => array( + 'function' => 'linesElements', + 'argument' => (array) $matches[1], + 'destination' => 'elements', + ) + ), + ); + + return $Block; + } + } + + protected function blockQuoteContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + if ($Line['text'][0] === '>' and preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) + { + $Block['element']['handler']['argument'] []= $matches[1]; + + return $Block; + } + + if ( ! isset($Block['interrupted'])) + { + $Block['element']['handler']['argument'] []= $Line['text']; + + return $Block; + } + } + + # + # Rule + + protected function blockRule($Line) + { + $marker = $Line['text'][0]; + + if (substr_count($Line['text'], $marker) >= 3 and chop($Line['text'], " $marker") === '') + { + $Block = array( + 'element' => array( + 'name' => 'hr', + ), + ); + + return $Block; + } + } + + # + # Setext + + protected function blockSetextHeader($Line, array $Block = null) + { + if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) + { + return; + } + + if ($Line['indent'] < 4 and chop(chop($Line['text'], ' '), $Line['text'][0]) === '') + { + $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2'; + + return $Block; + } + } + + # + # Markup + + protected function blockMarkup($Line) + { + if ($this->markupEscaped or $this->safeMode) + { + return; + } + + if (preg_match('/^<[\/]?+(\w*)(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+(\/)?>/', $Line['text'], $matches)) + { + $element = strtolower($matches[1]); + + if (in_array($element, $this->textLevelElements)) + { + return; + } + + $Block = array( + 'name' => $matches[1], + 'element' => array( + 'rawHtml' => $Line['text'], + 'autobreak' => true, + ), + ); + + return $Block; + } + } + + protected function blockMarkupContinue($Line, array $Block) + { + if (isset($Block['closed']) or isset($Block['interrupted'])) + { + return; + } + + $Block['element']['rawHtml'] .= "\n" . $Line['body']; + + return $Block; + } + + # + # Reference + + protected function blockReference($Line) + { + if (strpos($Line['text'], ']') !== false + and preg_match('/^\[(.+?)\]:[ ]*+?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/', $Line['text'], $matches) + ) { + $id = strtolower($matches[1]); + + $Data = array( + 'url' => $matches[2], + 'title' => isset($matches[3]) ? $matches[3] : null, + ); + + $this->DefinitionData['Reference'][$id] = $Data; + + $Block = array( + 'element' => array(), + ); + + return $Block; + } + } + + # + # Table + + protected function blockTable($Line, array $Block = null) + { + if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) + { + return; + } + + if ( + strpos($Block['element']['handler']['argument'], '|') === false + and strpos($Line['text'], '|') === false + and strpos($Line['text'], ':') === false + or strpos($Block['element']['handler']['argument'], "\n") !== false + ) { + return; + } + + if (chop($Line['text'], ' -:|') !== '') + { + return; + } + + $alignments = array(); + + $divider = $Line['text']; + + $divider = trim($divider); + $divider = trim($divider, '|'); + + $dividerCells = explode('|', $divider); + + foreach ($dividerCells as $dividerCell) + { + $dividerCell = trim($dividerCell); + + if ($dividerCell === '') + { + return; + } + + $alignment = null; + + if ($dividerCell[0] === ':') + { + $alignment = 'left'; + } + + if (substr($dividerCell, - 1) === ':') + { + $alignment = $alignment === 'left' ? 'center' : 'right'; + } + + $alignments []= $alignment; + } + + # ~ + + $HeaderElements = array(); + + $header = $Block['element']['handler']['argument']; + + $header = trim($header); + $header = trim($header, '|'); + + $headerCells = explode('|', $header); + + if (count($headerCells) !== count($alignments)) + { + return; + } + + foreach ($headerCells as $index => $headerCell) + { + $headerCell = trim($headerCell); + + $HeaderElement = array( + 'name' => 'th', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $headerCell, + 'destination' => 'elements', + ) + ); + + if (isset($alignments[$index])) + { + $alignment = $alignments[$index]; + + $HeaderElement['attributes'] = array( + 'style' => "text-align: $alignment;", + ); + } + + $HeaderElements []= $HeaderElement; + } + + # ~ + + $Block = array( + 'alignments' => $alignments, + 'identified' => true, + 'element' => array( + 'name' => 'table', + 'elements' => array(), + ), + ); + + $Block['element']['elements'] []= array( + 'name' => 'thead', + ); + + $Block['element']['elements'] []= array( + 'name' => 'tbody', + 'elements' => array(), + ); + + $Block['element']['elements'][0]['elements'] []= array( + 'name' => 'tr', + 'elements' => $HeaderElements, + ); + + return $Block; + } + + protected function blockTableContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + if (count($Block['alignments']) === 1 or $Line['text'][0] === '|' or strpos($Line['text'], '|')) + { + $Elements = array(); + + $row = $Line['text']; + + $row = trim($row); + $row = trim($row, '|'); + + preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]++`|`)++/', $row, $matches); + + $cells = array_slice($matches[0], 0, count($Block['alignments'])); + + foreach ($cells as $index => $cell) + { + $cell = trim($cell); + + $Element = array( + 'name' => 'td', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $cell, + 'destination' => 'elements', + ) + ); + + if (isset($Block['alignments'][$index])) + { + $Element['attributes'] = array( + 'style' => 'text-align: ' . $Block['alignments'][$index] . ';', + ); + } + + $Elements []= $Element; + } + + $Element = array( + 'name' => 'tr', + 'elements' => $Elements, + ); + + $Block['element']['elements'][1]['elements'] []= $Element; + + return $Block; + } + } + + # + # ~ + # + + protected function paragraph($Line) + { + return array( + 'type' => 'Paragraph', + 'element' => array( + 'name' => 'p', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $Line['text'], + 'destination' => 'elements', + ), + ), + ); + } + + protected function paragraphContinue($Line, array $Block) + { + if (isset($Block['interrupted'])) + { + return; + } + + $Block['element']['handler']['argument'] .= "\n".$Line['text']; + + return $Block; + } + + # + # Inline Elements + # + + protected $InlineTypes = array( + '!' => array('Image'), + '&' => array('SpecialCharacter'), + '*' => array('Emphasis'), + ':' => array('Url'), + '<' => array('UrlTag', 'EmailTag', 'Markup'), + '[' => array('Link'), + '_' => array('Emphasis'), + '`' => array('Code'), + '~' => array('Strikethrough'), + '\\' => array('EscapeSequence'), + ); + + # ~ + + protected $inlineMarkerList = '!*_&[:<`~\\'; + + # + # ~ + # + + public function line($text, $nonNestables = array()) + { + return $this->elements($this->lineElements($text, $nonNestables)); + } + + protected function lineElements($text, $nonNestables = array()) + { + # standardize line breaks + $text = str_replace(array("\r\n", "\r"), "\n", $text); + + $Elements = array(); + + $nonNestables = (empty($nonNestables) + ? array() + : array_combine($nonNestables, $nonNestables) + ); + + # $excerpt is based on the first occurrence of a marker + + while ($excerpt = strpbrk($text, $this->inlineMarkerList)) + { + $marker = $excerpt[0]; + + $markerPosition = strlen($text) - strlen($excerpt); + + $Excerpt = array('text' => $excerpt, 'context' => $text); + + foreach ($this->InlineTypes[$marker] as $inlineType) + { + # check to see if the current inline type is nestable in the current context + + if (isset($nonNestables[$inlineType])) + { + continue; + } + + $Inline = $this->{"inline$inlineType"}($Excerpt); + + if ( ! isset($Inline)) + { + continue; + } + + # makes sure that the inline belongs to "our" marker + + if (isset($Inline['position']) and $Inline['position'] > $markerPosition) + { + continue; + } + + # sets a default inline position + + if ( ! isset($Inline['position'])) + { + $Inline['position'] = $markerPosition; + } + + # cause the new element to 'inherit' our non nestables + + + $Inline['element']['nonNestables'] = isset($Inline['element']['nonNestables']) + ? array_merge($Inline['element']['nonNestables'], $nonNestables) + : $nonNestables + ; + + # the text that comes before the inline + $unmarkedText = substr($text, 0, $Inline['position']); + + # compile the unmarked text + $InlineText = $this->inlineText($unmarkedText); + $Elements[] = $InlineText['element']; + + # compile the inline + $Elements[] = $this->extractElement($Inline); + + # remove the examined text + $text = substr($text, $Inline['position'] + $Inline['extent']); + + continue 2; + } + + # the marker does not belong to an inline + + $unmarkedText = substr($text, 0, $markerPosition + 1); + + $InlineText = $this->inlineText($unmarkedText); + $Elements[] = $InlineText['element']; + + $text = substr($text, $markerPosition + 1); + } + + $InlineText = $this->inlineText($text); + $Elements[] = $InlineText['element']; + + foreach ($Elements as &$Element) + { + if ( ! isset($Element['autobreak'])) + { + $Element['autobreak'] = false; + } + } + + return $Elements; + } + + # + # ~ + # + + protected function inlineText($text) + { + $Inline = array( + 'extent' => strlen($text), + 'element' => array(), + ); + + $Inline['element']['elements'] = self::pregReplaceElements( + $this->breaksEnabled ? '/[ ]*+\n/' : '/(?:[ ]*+\\\\|[ ]{2,}+)\n/', + array( + array('name' => 'br'), + array('text' => "\n"), + ), + $text + ); + + return $Inline; + } + + protected function inlineCode($Excerpt) + { + $marker = $Excerpt['text'][0]; + + if (preg_match('/^(['.$marker.']++)[ ]*+(.+?)[ ]*+(? strlen($matches[0]), + 'element' => array( + 'name' => 'code', + 'text' => $text, + ), + ); + } + } + + protected function inlineEmailTag($Excerpt) + { + $hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'; + + $commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@' + . $hostnameLabel . '(?:\.' . $hostnameLabel . ')*'; + + if (strpos($Excerpt['text'], '>') !== false + and preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches) + ){ + $url = $matches[1]; + + if ( ! isset($matches[2])) + { + $url = "mailto:$url"; + } + + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'a', + 'text' => $matches[1], + 'attributes' => array( + 'href' => $url, + ), + ), + ); + } + } + + protected function inlineEmphasis($Excerpt) + { + if ( ! isset($Excerpt['text'][1])) + { + return; + } + + $marker = $Excerpt['text'][0]; + + if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) + { + $emphasis = 'strong'; + } + elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches)) + { + $emphasis = 'em'; + } + else + { + return; + } + + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => $emphasis, + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $matches[1], + 'destination' => 'elements', + ) + ), + ); + } + + protected function inlineEscapeSequence($Excerpt) + { + if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters)) + { + return array( + 'element' => array('rawHtml' => $Excerpt['text'][1]), + 'extent' => 2, + ); + } + } + + protected function inlineImage($Excerpt) + { + if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') + { + return; + } + + $Excerpt['text']= substr($Excerpt['text'], 1); + + $Link = $this->inlineLink($Excerpt); + + if ($Link === null) + { + return; + } + + $Inline = array( + 'extent' => $Link['extent'] + 1, + 'element' => array( + 'name' => 'img', + 'attributes' => array( + 'src' => $Link['element']['attributes']['href'], + 'alt' => $Link['element']['handler']['argument'], + ), + 'autobreak' => true, + ), + ); + + $Inline['element']['attributes'] += $Link['element']['attributes']; + + unset($Inline['element']['attributes']['href']); + + return $Inline; + } + + protected function inlineLink($Excerpt) + { + $Element = array( + 'name' => 'a', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => null, + 'destination' => 'elements', + ), + 'nonNestables' => array('Url', 'Link'), + 'attributes' => array( + 'href' => null, + 'title' => null, + ), + ); + + $extent = 0; + + $remainder = $Excerpt['text']; + + if (preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches)) + { + $Element['handler']['argument'] = $matches[1]; + + $extent += strlen($matches[0]); + + $remainder = substr($remainder, $extent); + } + else + { + return; + } + + if (preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*+"|\'[^\']*+\'))?\s*+[)]/', $remainder, $matches)) + { + $Element['attributes']['href'] = $matches[1]; + + if (isset($matches[2])) + { + $Element['attributes']['title'] = substr($matches[2], 1, - 1); + } + + $extent += strlen($matches[0]); + } + else + { + if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) + { + $definition = strlen($matches[1]) ? $matches[1] : $Element['handler']['argument']; + $definition = strtolower($definition); + + $extent += strlen($matches[0]); + } + else + { + $definition = strtolower($Element['handler']['argument']); + } + + if ( ! isset($this->DefinitionData['Reference'][$definition])) + { + return; + } + + $Definition = $this->DefinitionData['Reference'][$definition]; + + $Element['attributes']['href'] = $Definition['url']; + $Element['attributes']['title'] = $Definition['title']; + } + + return array( + 'extent' => $extent, + 'element' => $Element, + ); + } + + protected function inlineMarkup($Excerpt) + { + if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false) + { + return; + } + + if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt['text'], $matches)) + { + return array( + 'element' => array('rawHtml' => $matches[0]), + 'extent' => strlen($matches[0]), + ); + } + + if ($Excerpt['text'][1] === '!' and preg_match('/^/s', $Excerpt['text'], $matches)) + { + return array( + 'element' => array('rawHtml' => $matches[0]), + 'extent' => strlen($matches[0]), + ); + } + + if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*+(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+\/?>/s', $Excerpt['text'], $matches)) + { + return array( + 'element' => array('rawHtml' => $matches[0]), + 'extent' => strlen($matches[0]), + ); + } + } + + protected function inlineSpecialCharacter($Excerpt) + { + if (substr($Excerpt['text'], 1, 1) !== ' ' and strpos($Excerpt['text'], ';') !== false + and preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches) + ) { + return array( + 'element' => array('rawHtml' => '&' . $matches[1] . ';'), + 'extent' => strlen($matches[0]), + ); + } + + return; + } + + protected function inlineStrikethrough($Excerpt) + { + if ( ! isset($Excerpt['text'][1])) + { + return; + } + + if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) + { + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'del', + 'handler' => array( + 'function' => 'lineElements', + 'argument' => $matches[1], + 'destination' => 'elements', + ) + ), + ); + } + } + + protected function inlineUrl($Excerpt) + { + if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') + { + return; + } + + if (strpos($Excerpt['context'], 'http') !== false + and preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE) + ) { + $url = $matches[0][0]; + + $Inline = array( + 'extent' => strlen($matches[0][0]), + 'position' => $matches[0][1], + 'element' => array( + 'name' => 'a', + 'text' => $url, + 'attributes' => array( + 'href' => $url, + ), + ), + ); + + return $Inline; + } + } + + protected function inlineUrlTag($Excerpt) + { + if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) + { + $url = $matches[1]; + + return array( + 'extent' => strlen($matches[0]), + 'element' => array( + 'name' => 'a', + 'text' => $url, + 'attributes' => array( + 'href' => $url, + ), + ), + ); + } + } + + # ~ + + protected function unmarkedText($text) + { + $Inline = $this->inlineText($text); + return $this->element($Inline['element']); + } + + # + # Handlers + # + + protected function handle(array $Element) + { + if (isset($Element['handler'])) + { + if (!isset($Element['nonNestables'])) + { + $Element['nonNestables'] = array(); + } + + if (is_string($Element['handler'])) + { + $function = $Element['handler']; + $argument = $Element['text']; + unset($Element['text']); + $destination = 'rawHtml'; + } + else + { + $function = $Element['handler']['function']; + $argument = $Element['handler']['argument']; + $destination = $Element['handler']['destination']; + } + + $Element[$destination] = $this->{$function}($argument, $Element['nonNestables']); + + if ($destination === 'handler') + { + $Element = $this->handle($Element); + } + + unset($Element['handler']); + } + + return $Element; + } + + protected function handleElementRecursive(array $Element) + { + return $this->elementApplyRecursive(array($this, 'handle'), $Element); + } + + protected function handleElementsRecursive(array $Elements) + { + return $this->elementsApplyRecursive(array($this, 'handle'), $Elements); + } + + protected function elementApplyRecursive($closure, array $Element) + { + $Element = call_user_func($closure, $Element); + + if (isset($Element['elements'])) + { + $Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']); + } + elseif (isset($Element['element'])) + { + $Element['element'] = $this->elementApplyRecursive($closure, $Element['element']); + } + + return $Element; + } + + protected function elementApplyRecursiveDepthFirst($closure, array $Element) + { + if (isset($Element['elements'])) + { + $Element['elements'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['elements']); + } + elseif (isset($Element['element'])) + { + $Element['element'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['element']); + } + + $Element = call_user_func($closure, $Element); + + return $Element; + } + + protected function elementsApplyRecursive($closure, array $Elements) + { + foreach ($Elements as &$Element) + { + $Element = $this->elementApplyRecursive($closure, $Element); + } + + return $Elements; + } + + protected function elementsApplyRecursiveDepthFirst($closure, array $Elements) + { + foreach ($Elements as &$Element) + { + $Element = $this->elementApplyRecursiveDepthFirst($closure, $Element); + } + + return $Elements; + } + + protected function element(array $Element) + { + if ($this->safeMode) + { + $Element = $this->sanitiseElement($Element); + } + + # identity map if element has no handler + $Element = $this->handle($Element); + + $hasName = isset($Element['name']); + + $markup = ''; + + if ($hasName) + { + $markup .= '<' . $Element['name']; + + if (isset($Element['attributes'])) + { + foreach ($Element['attributes'] as $name => $value) + { + if ($value === null) + { + continue; + } + + $markup .= " $name=\"".self::escape($value).'"'; + } + } + } + + $permitRawHtml = false; + + if (isset($Element['text'])) + { + $text = $Element['text']; + } + // very strongly consider an alternative if you're writing an + // extension + elseif (isset($Element['rawHtml'])) + { + $text = $Element['rawHtml']; + + $allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode']; + $permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode; + } + + $hasContent = isset($text) || isset($Element['element']) || isset($Element['elements']); + + if ($hasContent) + { + $markup .= $hasName ? '>' : ''; + + if (isset($Element['elements'])) + { + $markup .= $this->elements($Element['elements']); + } + elseif (isset($Element['element'])) + { + $markup .= $this->element($Element['element']); + } + else + { + if (!$permitRawHtml) + { + $markup .= self::escape($text, true); + } + else + { + $markup .= $text; + } + } + + $markup .= $hasName ? '' : ''; + } + elseif ($hasName) + { + $markup .= ' />'; + } + + return $markup; + } + + protected function elements(array $Elements) + { + $markup = ''; + + $autoBreak = true; + + foreach ($Elements as $Element) + { + if (empty($Element)) + { + continue; + } + + $autoBreakNext = (isset($Element['autobreak']) + ? $Element['autobreak'] : isset($Element['name']) + ); + // (autobreak === false) covers both sides of an element + $autoBreak = !$autoBreak ? $autoBreak : $autoBreakNext; + + $markup .= ($autoBreak ? "\n" : '') . $this->element($Element); + $autoBreak = $autoBreakNext; + } + + $markup .= $autoBreak ? "\n" : ''; + + return $markup; + } + + # ~ + + protected function li($lines) + { + $Elements = $this->linesElements($lines); + + if ( ! in_array('', $lines) + and isset($Elements[0]) and isset($Elements[0]['name']) + and $Elements[0]['name'] === 'p' + ) { + unset($Elements[0]['name']); + } + + return $Elements; + } + + # + # AST Convenience + # + + /** + * Replace occurrences $regexp with $Elements in $text. Return an array of + * elements representing the replacement. + */ + protected static function pregReplaceElements($regexp, $Elements, $text) + { + $newElements = array(); + + while (preg_match($regexp, $text, $matches, PREG_OFFSET_CAPTURE)) + { + $offset = $matches[0][1]; + $before = substr($text, 0, $offset); + $after = substr($text, $offset + strlen($matches[0][0])); + + $newElements[] = array('text' => $before); + + foreach ($Elements as $Element) + { + $newElements[] = $Element; + } + + $text = $after; + } + + $newElements[] = array('text' => $text); + + return $newElements; + } + + # + # Deprecated Methods + # + + function parse($text) + { + $markup = $this->text($text); + + return $markup; + } + + protected function sanitiseElement(array $Element) + { + static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/'; + static $safeUrlNameToAtt = array( + 'a' => 'href', + 'img' => 'src', + ); + + if ( ! isset($Element['name'])) + { + unset($Element['attributes']); + return $Element; + } + + if (isset($safeUrlNameToAtt[$Element['name']])) + { + $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]); + } + + if ( ! empty($Element['attributes'])) + { + foreach ($Element['attributes'] as $att => $val) + { + # filter out badly parsed attribute + if ( ! preg_match($goodAttribute, $att)) + { + unset($Element['attributes'][$att]); + } + # dump onevent attribute + elseif (self::striAtStart($att, 'on')) + { + unset($Element['attributes'][$att]); + } + } + } + + return $Element; + } + + protected function filterUnsafeUrlInAttribute(array $Element, $attribute) + { + foreach ($this->safeLinksWhitelist as $scheme) + { + if (self::striAtStart($Element['attributes'][$attribute], $scheme)) + { + return $Element; + } + } + + $Element['attributes'][$attribute] = str_replace(':', '%3A', $Element['attributes'][$attribute]); + + return $Element; + } + + # + # Static Methods + # + + protected static function escape($text, $allowQuotes = false) + { + return htmlspecialchars($text, $allowQuotes ? ENT_NOQUOTES : ENT_QUOTES, 'UTF-8'); + } + + protected static function striAtStart($string, $needle) + { + $len = strlen($needle); + + if ($len > strlen($string)) + { + return false; + } + else + { + return strtolower(substr($string, 0, $len)) === strtolower($needle); + } + } + + static function instance($name = 'default') + { + if (isset(self::$instances[$name])) + { + return self::$instances[$name]; + } + + $instance = new static(); + + self::$instances[$name] = $instance; + + return $instance; + } + + private static $instances = array(); + + # + # Fields + # + + protected $DefinitionData; + + # + # Read-Only + + protected $specialCharacters = array( + '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', '~' + ); + + protected $StrongRegex = array( + '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s', + '_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us', + ); + + protected $EmRegex = array( + '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', + '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', + ); + + protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+'; + + protected $voidElements = array( + 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', + ); + + protected $textLevelElements = array( + 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont', + 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing', + 'i', 'rp', 'del', 'code', 'strike', 'marquee', + 'q', 'rt', 'ins', 'font', 'strong', + 's', 'tt', 'kbd', 'mark', + 'u', 'xm', 'sub', 'nobr', + 'sup', 'ruby', + 'var', 'span', + 'wbr', 'time', + ); +} diff --git a/parsedown/README.md b/parsedown/README.md new file mode 100755 index 0000000..a4b8c63 --- /dev/null +++ b/parsedown/README.md @@ -0,0 +1,103 @@ + + +

Parsedown

+ +

Parsedown

+ +[![Build Status](https://travis-ci.org/erusev/parsedown.svg)](https://travis-ci.org/erusev/parsedown) +[![Total Downloads](https://poser.pugx.org/erusev/parsedown/d/total.svg)](https://packagist.org/packages/erusev/parsedown) +[![Version](https://poser.pugx.org/erusev/parsedown/v/stable.svg)](https://packagist.org/packages/erusev/parsedown) +[![License](https://poser.pugx.org/erusev/parsedown/license.svg)](https://packagist.org/packages/erusev/parsedown) + +Better Markdown Parser in PHP - Demo. + +## Features + +* One File +* No Dependencies +* [Super Fast](http://parsedown.org/speed) +* Extensible +* [GitHub flavored](https://github.github.com/gfm) +* [Tested](http://parsedown.org/tests/) in 5.3 to 7.3 +* [Markdown Extra extension](https://github.com/erusev/parsedown-extra) + +## Installation + +Install the [composer package]: + + composer require erusev/parsedown + +Or download the [latest release] and include `Parsedown.php` + +[composer package]: https://packagist.org/packages/erusev/parsedown "The Parsedown package on packagist.org" +[latest release]: https://github.com/erusev/parsedown/releases/latest "The latest release of Parsedown" + +## Example + +```php +$Parsedown = new Parsedown(); + +echo $Parsedown->text('Hello _Parsedown_!'); # prints:

Hello Parsedown!

+``` + +You can also parse inline markdown only: + +```php +echo $Parsedown->line('Hello _Parsedown_!'); # prints: Hello Parsedown! +``` + +More examples in [the wiki](https://github.com/erusev/parsedown/wiki/) and in [this video tutorial](http://youtu.be/wYZBY8DEikI). + +## Security + +Parsedown is capable of escaping user-input within the HTML that it generates. Additionally Parsedown will apply sanitisation to additional scripting vectors (such as scripting link destinations) that are introduced by the markdown syntax itself. + +To tell Parsedown that it is processing untrusted user-input, use the following: + +```php +$Parsedown->setSafeMode(true); +``` + +If instead, you wish to allow HTML within untrusted user-input, but still want output to be free from XSS it is recommended that you make use of a HTML sanitiser that allows HTML tags to be whitelisted, like [HTML Purifier](http://htmlpurifier.org/). + +In both cases you should strongly consider employing defence-in-depth measures, like [deploying a Content-Security-Policy](https://scotthelme.co.uk/content-security-policy-an-introduction/) (a browser security feature) so that your page is likely to be safe even if an attacker finds a vulnerability in one of the first lines of defence above. + +#### Security of Parsedown Extensions + +Safe mode does not necessarily yield safe results when using extensions to Parsedown. Extensions should be evaluated on their own to determine their specific safety against XSS. + +## Escaping HTML + +> **WARNING:** This method isn't safe from XSS! + +If you wish to escape HTML **in trusted input**, you can use the following: + +```php +$Parsedown->setMarkupEscaped(true); +``` + +Beware that this still allows users to insert unsafe scripting vectors, such as links like `[xss](javascript:alert%281%29)`. + +## Questions + +**How does Parsedown work?** + +It tries to read Markdown like a human. First, it looks at the lines. It’s interested in how the lines start. This helps it recognise blocks. It knows, for example, that if a line starts with a `-` then perhaps it belongs to a list. Once it recognises the blocks, it continues to the content. As it reads, it watches out for special characters. This helps it recognise inline elements (or inlines). + +We call this approach "line based". We believe that Parsedown is the first Markdown parser to use it. Since the release of Parsedown, other developers have used the same approach to develop other Markdown parsers in PHP and in other languages. + +**Is it compliant with CommonMark?** + +It passes most of the CommonMark tests. Most of the tests that don't pass deal with cases that are quite uncommon. Still, as CommonMark matures, compliance should improve. + +**Who uses it?** + +[Laravel Framework](https://laravel.com/), [Bolt CMS](http://bolt.cm/), [Grav CMS](http://getgrav.org/), [Herbie CMS](http://www.getherbie.org/), [Kirby CMS](http://getkirby.com/), [October CMS](http://octobercms.com/), [Pico CMS](http://picocms.org), [Statamic CMS](http://www.statamic.com/), [phpDocumentor](http://www.phpdoc.org/), [RaspberryPi.org](http://www.raspberrypi.org/), [Symfony Demo](https://github.com/symfony/demo) and [more](https://packagist.org/packages/erusev/parsedown/dependents). + +**How can I help?** + +Use it, star it, share it and if you feel generous, [donate](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=528P3NZQMP8N2). + +**What else should I know?** + +I also make [Nota](https://nota.md/) — a writing app designed for Markdown files :) diff --git a/style.less b/style.less new file mode 100755 index 0000000..14e3432 --- /dev/null +++ b/style.less @@ -0,0 +1,236 @@ +@light: ~"(prefers-color-scheme: light)"; +@dark: ~"(prefers-color-scheme: dark)"; + +* { + padding: 0px; + margin: 0px; + font-family: system-ui, sans-serif; + font-size: 30px; + line-height: 50px; + + @media @light { + background-color: @lightColor; + color: @darkColor; + scrollbar-color: @darkColor @lightColor; + } + + @media @dark { + background-color: @darkColor; + color: @lightColor; + scrollbar-color: @lightColor @darkColor; + } +} + +::selection { + @media @light { + color: @lightColor; + background-color: @darkColor; + } + @media @dark { + color: @darkColor; + background-color: @lightColor; + } +} + +main { + padding: 30px; + padding-top: 15px; + + @media (max-width: 500px) { + padding-left: 10px; + padding-right: 10px; + } + + margin-left: 20%; + margin-right: 20%; + + @media (max-width: 1400px) { + margin-left: 10%; + margin-right: 10%; + } + + @media (max-width: 800px) { + margin-left: 0px; + margin-right: 0px; + } +} + +a, a:visited { + text-decoration: underline; + transition-property: color, border-color; + transition-duration: 0.1s; + transition-timing-function: linear; + + &:hover, &:focus { + text-decoration: none; + color: @mainColor; + } +} + +.button { + border-width: 2px; + border-style: solid; + padding: 8px 16px 8px 16px; + margin: 5px; + display: inline-block; + border-radius: 14px; + text-decoration: none; + + @media @light { + border-color: @darkColor; + } + @media @dark { + border-color: @lightColor; + } + + &:hover, &:focus { + color: @mainColor; + border: 2px solid @mainColor; + } + +} + +.smallButton { + .button(); + font-size: 80%; + padding: 0px 16px 0px 16px; +} + +.logo { + margin-bottom: 10px; +} + +img { + height: auto; + max-width: 100%; +} + +.imgArticle { + display: block; + margin: auto; + margin-top: 20px; + margin-bottom: 20px; +} + +.border { + border-width: 1px; + border-style: solid; + border-radius: 5px; + + @media @light { + border-color: @darkColor; + } + + @media @dark { + border-color: @lightColor; + } +} + +strong { + font-weight: bold; +} + +pre, *:not(pre) > code, var, samp { + padding: 5px; +} + +pre, code, var, samp { + font-family: monospace; + overflow: auto; + border-radius: 10px; + font-style: normal; + @media @dark { + background-color: @darkerColor; + } + + @media @light { + background-color: @lightlessColor; + } +} + +abbr[title] { + text-decoration: dotted underline; +} + +address { + font-style: normal; +} + +ul { + padding-left: 35px; +} + +dt { + font-weight: 500; +} + +dd { + margin-left: 25px; + padding-bottom: 10px; +} + +p { + padding-top: 10px; + padding-bottom: 10px; +} + +.centered { + text-align: center; + justify-content: center; +} + +// FOOTER + +.textefooter { + margin: 30px; + line-height: 1.5em; + text-align: center; +} + +footer { + text-align: center; + justify-content: center; +} + +header { + display: flex; + justify-content: center; +} + +// ----- TITLES ----- + +h1, h2, h3, h4, h5, h6 { + font-weight: normal; + line-height: 100%; +} + +h1 { + font-size: 250%; + text-align: center; + padding-bottom: 20px; + padding-top: 15px; +} + +h2 { + font-size: 200%; + margin-top: 50px; + margin-bottom: 20px; +} + +h3 { + font-size: 160%; + margin-top: 30px; + margin-bottom: 10px; +} + +h4 { + font-size: 130%; +} + +h5 { + font-size: 115%; +} + +h6 { + font-size: 107%; +}