Blame src/sass2scss.cpp

Packit Service 7770af
/**
Packit Service 7770af
 * sass2scss
Packit Service 7770af
 * Licensed under the MIT License
Packit Service 7770af
 * Copyright (c) Marcel Greter
Packit Service 7770af
 */
Packit Service 7770af
Packit Service 7770af
#ifdef _MSC_VER
Packit Service 7770af
#define _CRT_SECURE_NO_WARNINGS
Packit Service 7770af
#define _CRT_NONSTDC_NO_DEPRECATE
Packit Service 7770af
#endif
Packit Service 7770af
Packit Service 7770af
// include library
Packit Service 7770af
#include <stack>
Packit Service 7770af
#include <string>
Packit Service 7770af
#include <cstring>
Packit Service 7770af
#include <cstdlib>
Packit Service 7770af
#include <sstream>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <stdio.h>
Packit Service 7770af
Packit Service 7770af
///*
Packit Service 7770af
//
Packit Service 7770af
// src comments: comments in sass syntax (staring with //)
Packit Service 7770af
// css comments: multiline comments in css syntax (starting with /*)
Packit Service 7770af
//
Packit Service 7770af
// KEEP_COMMENT: keep src comments in the resulting css code
Packit Service 7770af
// STRIP_COMMENT: strip out all comments (either src or css)
Packit Service 7770af
// CONVERT_COMMENT: convert all src comments to css comments
Packit Service 7770af
//
Packit Service 7770af
//*/
Packit Service 7770af
Packit Service 7770af
// our own header
Packit Service 7770af
#include "sass2scss.h"
Packit Service 7770af
Packit Service 7770af
// add namespace for c++
Packit Service 7770af
namespace Sass
Packit Service 7770af
{
Packit Service 7770af
Packit Service 7770af
	// return the actual prettify value from options
Packit Service 7770af
	#define PRETTIFY(converter) (converter.options - (converter.options & 248))
Packit Service 7770af
	// query the options integer to check if the option is enables
Packit Service 7770af
	#define KEEP_COMMENT(converter) ((converter.options & SASS2SCSS_KEEP_COMMENT) == SASS2SCSS_KEEP_COMMENT)
Packit Service 7770af
	#define STRIP_COMMENT(converter) ((converter.options & SASS2SCSS_STRIP_COMMENT) == SASS2SCSS_STRIP_COMMENT)
Packit Service 7770af
	#define CONVERT_COMMENT(converter) ((converter.options & SASS2SCSS_CONVERT_COMMENT) == SASS2SCSS_CONVERT_COMMENT)
Packit Service 7770af
Packit Service 7770af
	// some makros to access the indentation stack
Packit Service 7770af
	#define INDENT(converter) (converter.indents.top())
Packit Service 7770af
Packit Service 7770af
	// some makros to query comment parser status
Packit Service 7770af
	#define IS_PARSING(converter) (converter.comment == "")
Packit Service 7770af
	#define IS_COMMENT(converter) (converter.comment != "")
Packit Service 7770af
	#define IS_SRC_COMMENT(converter) (converter.comment == "//" && ! CONVERT_COMMENT(converter))
Packit Service 7770af
	#define IS_CSS_COMMENT(converter) (converter.comment == "/*" || (converter.comment == "//" && CONVERT_COMMENT(converter)))
Packit Service 7770af
Packit Service 7770af
	// pretty printer helper function
Packit Service 7770af
	static std::string closer (const converter& converter)
Packit Service 7770af
	{
Packit Service 7770af
		return PRETTIFY(converter) == 0 ? " }" :
Packit Service 7770af
		     PRETTIFY(converter) <= 1 ? " }" :
Packit Service 7770af
		       "\n" + INDENT(converter) + "}";
Packit Service 7770af
	}
Packit Service 7770af
Packit Service 7770af
	// pretty printer helper function
Packit Service 7770af
	static std::string opener (const converter& converter)
Packit Service 7770af
	{
Packit Service 7770af
		return PRETTIFY(converter) == 0 ? " { " :
Packit Service 7770af
		     PRETTIFY(converter) <= 2 ? " {" :
Packit Service 7770af
		       "\n" + INDENT(converter) + "{";
Packit Service 7770af
	}
Packit Service 7770af
Packit Service 7770af
	// check if the given string is a pseudo selector
Packit Service 7770af
	// needed to differentiate from sass property syntax
Packit Service 7770af
	static bool isPseudoSelector (std::string& sel)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		size_t len = sel.length();
Packit Service 7770af
		if (len < 1) return false;
Packit Service 7770af
		size_t pos = sel.find_first_not_of("abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1);
Packit Service 7770af
		if (pos != std::string::npos) sel.erase(pos, std::string::npos);
Packit Service 7770af
		size_t i = sel.length();
Packit Service 7770af
		while (i -- > 0) { sel.at(i) = tolower(sel.at(i)); }
Packit Service 7770af
Packit Service 7770af
		// CSS Level 1 - Recommendation
Packit Service 7770af
		if (sel == ":link") return true;
Packit Service 7770af
		if (sel == ":visited") return true;
Packit Service 7770af
		if (sel == ":active") return true;
Packit Service 7770af
Packit Service 7770af
		// CSS Level 2 (Revision 1) - Recommendation
Packit Service 7770af
		if (sel == ":lang") return true;
Packit Service 7770af
		if (sel == ":first-child") return true;
Packit Service 7770af
		if (sel == ":hover") return true;
Packit Service 7770af
		if (sel == ":focus") return true;
Packit Service 7770af
		// disabled - also valid properties
Packit Service 7770af
		// if (sel == ":left") return true;
Packit Service 7770af
		// if (sel == ":right") return true;
Packit Service 7770af
		if (sel == ":first") return true;
Packit Service 7770af
Packit Service 7770af
		// Selectors Level 3 - Recommendation
Packit Service 7770af
		if (sel == ":target") return true;
Packit Service 7770af
		if (sel == ":root") return true;
Packit Service 7770af
		if (sel == ":nth-child") return true;
Packit Service 7770af
		if (sel == ":nth-last-of-child") return true;
Packit Service 7770af
		if (sel == ":nth-of-type") return true;
Packit Service 7770af
		if (sel == ":nth-last-of-type") return true;
Packit Service 7770af
		if (sel == ":last-child") return true;
Packit Service 7770af
		if (sel == ":first-of-type") return true;
Packit Service 7770af
		if (sel == ":last-of-type") return true;
Packit Service 7770af
		if (sel == ":only-child") return true;
Packit Service 7770af
		if (sel == ":only-of-type") return true;
Packit Service 7770af
		if (sel == ":empty") return true;
Packit Service 7770af
		if (sel == ":not") return true;
Packit Service 7770af
Packit Service 7770af
		// CSS Basic User Interface Module Level 3 - Working Draft
Packit Service 7770af
		if (sel == ":default") return true;
Packit Service 7770af
		if (sel == ":valid") return true;
Packit Service 7770af
		if (sel == ":invalid") return true;
Packit Service 7770af
		if (sel == ":in-range") return true;
Packit Service 7770af
		if (sel == ":out-of-range") return true;
Packit Service 7770af
		if (sel == ":required") return true;
Packit Service 7770af
		if (sel == ":optional") return true;
Packit Service 7770af
		if (sel == ":read-only") return true;
Packit Service 7770af
		if (sel == ":read-write") return true;
Packit Service 7770af
		if (sel == ":dir") return true;
Packit Service 7770af
		if (sel == ":enabled") return true;
Packit Service 7770af
		if (sel == ":disabled") return true;
Packit Service 7770af
		if (sel == ":checked") return true;
Packit Service 7770af
		if (sel == ":indeterminate") return true;
Packit Service 7770af
		if (sel == ":nth-last-child") return true;
Packit Service 7770af
Packit Service 7770af
		// Selectors Level 4 - Working Draft
Packit Service 7770af
		if (sel == ":any-link") return true;
Packit Service 7770af
		if (sel == ":local-link") return true;
Packit Service 7770af
		if (sel == ":scope") return true;
Packit Service 7770af
		if (sel == ":active-drop-target") return true;
Packit Service 7770af
		if (sel == ":valid-drop-target") return true;
Packit Service 7770af
		if (sel == ":invalid-drop-target") return true;
Packit Service 7770af
		if (sel == ":current") return true;
Packit Service 7770af
		if (sel == ":past") return true;
Packit Service 7770af
		if (sel == ":future") return true;
Packit Service 7770af
		if (sel == ":placeholder-shown") return true;
Packit Service 7770af
		if (sel == ":user-error") return true;
Packit Service 7770af
		if (sel == ":blank") return true;
Packit Service 7770af
		if (sel == ":nth-match") return true;
Packit Service 7770af
		if (sel == ":nth-last-match") return true;
Packit Service 7770af
		if (sel == ":nth-column") return true;
Packit Service 7770af
		if (sel == ":nth-last-column") return true;
Packit Service 7770af
		if (sel == ":matches") return true;
Packit Service 7770af
Packit Service 7770af
		// Fullscreen API - Living Standard
Packit Service 7770af
		if (sel == ":fullscreen") return true;
Packit Service 7770af
Packit Service 7770af
		// not a pseudo selector
Packit Service 7770af
		return false;
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
Packit Service 7770af
	// check if there is some char data
Packit Service 7770af
	// will ignore everything in comments
Packit Service 7770af
	static bool hasCharData (std::string& sass)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		size_t col_pos = 0;
Packit Service 7770af
Packit Service 7770af
		while (true)
Packit Service 7770af
		{
Packit Service 7770af
Packit Service 7770af
			// try to find some meaningfull char
Packit Service 7770af
			col_pos = sass.find_first_not_of(" \t\n\v\f\r", col_pos);
Packit Service 7770af
Packit Service 7770af
			// there was no meaningfull char found
Packit Service 7770af
			if (col_pos == std::string::npos) return false;
Packit Service 7770af
Packit Service 7770af
			// found a multiline comment opener
Packit Service 7770af
			if (sass.substr(col_pos, 2) == "/*")
Packit Service 7770af
			{
Packit Service 7770af
				// find the multiline comment closer
Packit Service 7770af
				col_pos = sass.find("*/", col_pos);
Packit Service 7770af
				// maybe we did not find the closer here
Packit Service 7770af
				if (col_pos == std::string::npos) return false;
Packit Service 7770af
				// skip closer
Packit Service 7770af
				col_pos += 2;
Packit Service 7770af
			}
Packit Service 7770af
			else
Packit Service 7770af
			{
Packit Service 7770af
				return true;
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
		}
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
	// EO hasCharData
Packit Service 7770af
Packit Service 7770af
	// find src comment opener
Packit Service 7770af
	// correctly skips quoted strings
Packit Service 7770af
	static size_t findCommentOpener (std::string& sass)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		size_t col_pos = 0;
Packit Service 7770af
		bool apoed = false;
Packit Service 7770af
		bool quoted = false;
Packit Service 7770af
		bool comment = false;
Packit Service 7770af
		size_t brackets = 0;
Packit Service 7770af
Packit Service 7770af
		while (col_pos != std::string::npos)
Packit Service 7770af
		{
Packit Service 7770af
Packit Service 7770af
			// process all interesting chars
Packit Service 7770af
			col_pos = sass.find_first_of("\"\'/\\*()", col_pos);
Packit Service 7770af
Packit Service 7770af
			// assertion for valid result
Packit Service 7770af
			if (col_pos != std::string::npos)
Packit Service 7770af
			{
Packit Service 7770af
				char character = sass.at(col_pos);
Packit Service 7770af
Packit Service 7770af
				if (character == '(')
Packit Service 7770af
				{
Packit Service 7770af
					if (!quoted && !apoed) brackets ++;
Packit Service 7770af
				}
Packit Service 7770af
				else if (character == ')')
Packit Service 7770af
				{
Packit Service 7770af
					if (!quoted && !apoed) brackets --;
Packit Service 7770af
				}
Packit Service 7770af
				else if (character == '\"')
Packit Service 7770af
				{
Packit Service 7770af
					// invert quote bool
Packit Service 7770af
					if (!apoed && !comment) quoted = !quoted;
Packit Service 7770af
				}
Packit Service 7770af
				else if (character == '\'')
Packit Service 7770af
				{
Packit Service 7770af
					// invert quote bool
Packit Service 7770af
					if (!quoted && !comment) apoed = !apoed;
Packit Service 7770af
				}
Packit Service 7770af
				else if (col_pos > 0 && character == '/')
Packit Service 7770af
				{
Packit Service 7770af
					if (sass.at(col_pos - 1) == '*')
Packit Service 7770af
					{
Packit Service 7770af
						comment = false;
Packit Service 7770af
					}
Packit Service 7770af
					// next needs to be a slash too
Packit Service 7770af
					else if (sass.at(col_pos - 1) == '/')
Packit Service 7770af
					{
Packit Service 7770af
						// only found if not in single or double quote, bracket or comment
Packit Service 7770af
						if (!quoted && !apoed && !comment && brackets == 0) return col_pos - 1;
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
				else if (character == '\\')
Packit Service 7770af
				{
Packit Service 7770af
					// skip next char if in quote
Packit Service 7770af
					if (quoted || apoed) col_pos ++;
Packit Service 7770af
				}
Packit Service 7770af
				// this might be a comment opener
Packit Service 7770af
				else if (col_pos > 0 && character == '*')
Packit Service 7770af
				{
Packit Service 7770af
					// opening a multiline comment
Packit Service 7770af
					if (sass.at(col_pos - 1) == '/')
Packit Service 7770af
					{
Packit Service 7770af
						// we are now in a comment
Packit Service 7770af
						if (!quoted && !apoed) comment = true;
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
				// skip char
Packit Service 7770af
				col_pos ++;
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
		}
Packit Service 7770af
		// EO while
Packit Service 7770af
Packit Service 7770af
		return col_pos;
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
	// EO findCommentOpener
Packit Service 7770af
Packit Service 7770af
	// remove multiline comments from sass string
Packit Service 7770af
	// correctly skips quoted strings
Packit Service 7770af
	static std::string removeMultilineComment (std::string &sass)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		std::string clean = "";
Packit Service 7770af
		size_t col_pos = 0;
Packit Service 7770af
		size_t open_pos = 0;
Packit Service 7770af
		size_t close_pos = 0;
Packit Service 7770af
		bool apoed = false;
Packit Service 7770af
		bool quoted = false;
Packit Service 7770af
		bool comment = false;
Packit Service 7770af
Packit Service 7770af
		// process sass til string end
Packit Service 7770af
		while (col_pos != std::string::npos)
Packit Service 7770af
		{
Packit Service 7770af
Packit Service 7770af
			// process all interesting chars
Packit Service 7770af
			col_pos = sass.find_first_of("\"\'/\\*", col_pos);
Packit Service 7770af
Packit Service 7770af
			// assertion for valid result
Packit Service 7770af
			if (col_pos != std::string::npos)
Packit Service 7770af
			{
Packit Service 7770af
				char character = sass.at(col_pos);
Packit Service 7770af
Packit Service 7770af
				// found quoted string delimiter
Packit Service 7770af
				if (character == '\"')
Packit Service 7770af
				{
Packit Service 7770af
					if (!apoed && !comment) quoted = !quoted;
Packit Service 7770af
				}
Packit Service 7770af
				else if (character == '\'')
Packit Service 7770af
				{
Packit Service 7770af
					if (!quoted && !comment) apoed = !apoed;
Packit Service 7770af
				}
Packit Service 7770af
				// found possible comment closer
Packit Service 7770af
				else if (character == '/')
Packit Service 7770af
				{
Packit Service 7770af
					// look back to see if it is actually a closer
Packit Service 7770af
					if (comment && col_pos > 0 && sass.at(col_pos - 1) == '*')
Packit Service 7770af
					{
Packit Service 7770af
						close_pos = col_pos + 1; comment = false;
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
				else if (character == '\\')
Packit Service 7770af
				{
Packit Service 7770af
					// skip escaped char
Packit Service 7770af
					if (quoted || apoed) col_pos ++;
Packit Service 7770af
				}
Packit Service 7770af
				// this might be a comment opener
Packit Service 7770af
				else if (character == '*')
Packit Service 7770af
				{
Packit Service 7770af
					// look back to see if it is actually an opener
Packit Service 7770af
					if (!quoted && !apoed && col_pos > 0 && sass.at(col_pos - 1) == '/')
Packit Service 7770af
					{
Packit Service 7770af
						comment = true; open_pos = col_pos - 1;
Packit Service 7770af
						clean += sass.substr(close_pos, open_pos - close_pos);
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
				// skip char
Packit Service 7770af
				col_pos ++;
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
		}
Packit Service 7770af
		// EO while
Packit Service 7770af
Packit Service 7770af
		// add final parts (add half open comment text)
Packit Service 7770af
		if (comment) clean += sass.substr(open_pos);
Packit Service 7770af
		else clean += sass.substr(close_pos);
Packit Service 7770af
Packit Service 7770af
		// return string
Packit Service 7770af
		return clean;
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
	// EO removeMultilineComment
Packit Service 7770af
Packit Service 7770af
	// right trim a given string
Packit Service 7770af
	std::string rtrim(const std::string &sass)
Packit Service 7770af
	{
Packit Service 7770af
		std::string trimmed = sass;
Packit Service 7770af
		size_t pos_ws = trimmed.find_last_not_of(" \t\n\v\f\r");
Packit Service 7770af
		if (pos_ws != std::string::npos)
Packit Service 7770af
		{ trimmed.erase(pos_ws + 1); }
Packit Service 7770af
		else { trimmed.clear(); }
Packit Service 7770af
		return trimmed;
Packit Service 7770af
	}
Packit Service 7770af
	// EO rtrim
Packit Service 7770af
Packit Service 7770af
	// flush whitespace and print additional text, but
Packit Service 7770af
	// only print additional chars and buffer whitespace
Packit Service 7770af
	std::string flush (std::string& sass, converter& converter)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		// return flushed
Packit Service 7770af
		std::string scss = "";
Packit Service 7770af
Packit Service 7770af
		// print whitespace buffer
Packit Service 7770af
		scss += PRETTIFY(converter) > 0 ?
Packit Service 7770af
		        converter.whitespace : "";
Packit Service 7770af
		// reset whitespace buffer
Packit Service 7770af
		converter.whitespace = "";
Packit Service 7770af
Packit Service 7770af
		// remove possible newlines from string
Packit Service 7770af
		size_t pos_right = sass.find_last_not_of("\n\r");
Packit Service 7770af
		if (pos_right == std::string::npos) return scss;
Packit Service 7770af
Packit Service 7770af
		// get the linefeeds from the string
Packit Service 7770af
		std::string lfs = sass.substr(pos_right + 1);
Packit Service 7770af
		sass = sass.substr(0, pos_right + 1);
Packit Service 7770af
Packit Service 7770af
		// find some source comment opener
Packit Service 7770af
		size_t comment_pos = findCommentOpener(sass);
Packit Service 7770af
		// check if there was a source comment
Packit Service 7770af
		if (comment_pos != std::string::npos)
Packit Service 7770af
		{
Packit Service 7770af
			// convert comment (but only outside other coments)
Packit Service 7770af
			if (CONVERT_COMMENT(converter) && !IS_COMMENT(converter))
Packit Service 7770af
			{
Packit Service 7770af
				// convert to multiline comment
Packit Service 7770af
				sass.at(comment_pos + 1) = '*';
Packit Service 7770af
				// add comment node to the whitespace
Packit Service 7770af
				sass += " */";
Packit Service 7770af
			}
Packit Service 7770af
			// not at line start
Packit Service 7770af
			if (comment_pos > 0)
Packit Service 7770af
			{
Packit Service 7770af
				// also include whitespace before the actual comment opener
Packit Service 7770af
				size_t ws_pos = sass.find_last_not_of(SASS2SCSS_FIND_WHITESPACE, comment_pos - 1);
Packit Service 7770af
				comment_pos = ws_pos == std::string::npos ? 0 : ws_pos + 1;
Packit Service 7770af
			}
Packit Service 7770af
			if (!STRIP_COMMENT(converter))
Packit Service 7770af
			{
Packit Service 7770af
				// add comment node to the whitespace
Packit Service 7770af
				converter.whitespace += sass.substr(comment_pos);
Packit Service 7770af
			}
Packit Service 7770af
			else
Packit Service 7770af
			{
Packit Service 7770af
				// sass = removeMultilineComments(sass);
Packit Service 7770af
			}
Packit Service 7770af
			// update the actual sass code
Packit Service 7770af
			sass = sass.substr(0, comment_pos);
Packit Service 7770af
		}
Packit Service 7770af
Packit Service 7770af
		// add newline as getline discharged it
Packit Service 7770af
		converter.whitespace += lfs + "\n";
Packit Service 7770af
Packit Service 7770af
		// maybe remove any leading whitespace
Packit Service 7770af
		if (PRETTIFY(converter) == 0)
Packit Service 7770af
		{
Packit Service 7770af
			// remove leading whitespace and update string
Packit Service 7770af
			size_t pos_left = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE);
Packit Service 7770af
			if (pos_left != std::string::npos) sass = sass.substr(pos_left);
Packit Service 7770af
		}
Packit Service 7770af
Packit Service 7770af
		// add flushed data
Packit Service 7770af
		scss += sass;
Packit Service 7770af
Packit Service 7770af
		// return string
Packit Service 7770af
		return scss;
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
	// EO flush
Packit Service 7770af
Packit Service 7770af
	// process a line of the sass text
Packit Service 7770af
	std::string process (std::string& sass, converter& converter)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		// resulting string
Packit Service 7770af
		std::string scss = "";
Packit Service 7770af
Packit Service 7770af
		// strip multi line comments
Packit Service 7770af
		if (STRIP_COMMENT(converter))
Packit Service 7770af
		{
Packit Service 7770af
			sass = removeMultilineComment(sass);
Packit Service 7770af
		}
Packit Service 7770af
Packit Service 7770af
		// right trim input
Packit Service 7770af
		sass = rtrim(sass);
Packit Service 7770af
Packit Service 7770af
		// get postion of first meaningfull character in string
Packit Service 7770af
		size_t pos_left = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE);
Packit Service 7770af
Packit Service 7770af
		// special case for final run
Packit Service 7770af
		if (converter.end_of_file) pos_left = 0;
Packit Service 7770af
Packit Service 7770af
		// maybe has only whitespace
Packit Service 7770af
		if (pos_left == std::string::npos)
Packit Service 7770af
		{
Packit Service 7770af
			// just add complete whitespace
Packit Service 7770af
			converter.whitespace += sass + "\n";
Packit Service 7770af
		}
Packit Service 7770af
		// have meaningfull first char
Packit Service 7770af
		else
Packit Service 7770af
		{
Packit Service 7770af
Packit Service 7770af
			// extract and store indentation string
Packit Service 7770af
			std::string indent = sass.substr(0, pos_left);
Packit Service 7770af
Packit Service 7770af
			// check if current line starts a comment
Packit Service 7770af
			std::string open = sass.substr(pos_left, 2);
Packit Service 7770af
Packit Service 7770af
			// line has less or same indentation
Packit Service 7770af
			// finalize previous open parser context
Packit Service 7770af
			if (indent.length() <= INDENT(converter).length())
Packit Service 7770af
			{
Packit Service 7770af
Packit Service 7770af
				// close multilinie comment
Packit Service 7770af
				if (IS_CSS_COMMENT(converter))
Packit Service 7770af
				{
Packit Service 7770af
					// check if comments will be stripped anyway
Packit Service 7770af
					if (!STRIP_COMMENT(converter)) scss += " */";
Packit Service 7770af
				}
Packit Service 7770af
				// close src comment comment
Packit Service 7770af
				else if (IS_SRC_COMMENT(converter))
Packit Service 7770af
				{
Packit Service 7770af
					// add a newline to avoid closer on same line
Packit Service 7770af
					// this would put the bracket in the comment node
Packit Service 7770af
					// no longer needed since we parse them correctly
Packit Service 7770af
					// if (KEEP_COMMENT(converter)) scss += "\n";
Packit Service 7770af
				}
Packit Service 7770af
				// close css properties
Packit Service 7770af
				else if (converter.property)
Packit Service 7770af
				{
Packit Service 7770af
					// add closer unless in concat mode
Packit Service 7770af
					if (!converter.comma)
Packit Service 7770af
					{
Packit Service 7770af
						// if there was no colon we have a selector
Packit Service 7770af
						// looks like there were no inner properties
Packit Service 7770af
						if (converter.selector) scss += " {}";
Packit Service 7770af
						// add final semicolon
Packit Service 7770af
						else if (!converter.semicolon) scss += ";";
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
				// reset comment state
Packit Service 7770af
				converter.comment = "";
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// make sure we close every "higher" block
Packit Service 7770af
			while (indent.length() < INDENT(converter).length())
Packit Service 7770af
			{
Packit Service 7770af
				// pop stacked context
Packit Service 7770af
				converter.indents.pop();
Packit Service 7770af
				// print close bracket
Packit Service 7770af
				if (IS_PARSING(converter))
Packit Service 7770af
				{ scss += closer(converter); }
Packit Service 7770af
				else { scss += " */"; }
Packit Service 7770af
				// reset comment state
Packit Service 7770af
				converter.comment = "";
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// reset converter state
Packit Service 7770af
			converter.selector = false;
Packit Service 7770af
Packit Service 7770af
			// looks like some undocumented behavior ...
Packit Service 7770af
			// https://github.com/mgreter/sass2scss/issues/29
Packit Service 7770af
			if (sass.substr(pos_left, 1) == "\\") {
Packit Service 7770af
				converter.selector = true;
Packit Service 7770af
				sass[pos_left] = ' ';
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// check if we have sass property syntax
Packit Service 7770af
			if (sass.substr(pos_left, 1) == ":" && sass.substr(pos_left, 2) != "::")
Packit Service 7770af
			{
Packit Service 7770af
Packit Service 7770af
				// default to a selector
Packit Service 7770af
				// change back if property found
Packit Service 7770af
				converter.selector = true;
Packit Service 7770af
				// get postion of first whitespace char
Packit Service 7770af
				size_t pos_wspace = sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left);
Packit Service 7770af
				// assertion check for valid result
Packit Service 7770af
				if (pos_wspace != std::string::npos)
Packit Service 7770af
				{
Packit Service 7770af
					// get the possible pseudo selector
Packit Service 7770af
					std::string pseudo = sass.substr(pos_left, pos_wspace - pos_left);
Packit Service 7770af
					// get position of the first real property value char
Packit Service 7770af
					// pseudo selectors get this far, but have no actual value
Packit Service 7770af
					size_t pos_value =  sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE, pos_wspace);
Packit Service 7770af
					// assertion check for valid result
Packit Service 7770af
					if (pos_value != std::string::npos)
Packit Service 7770af
					{
Packit Service 7770af
						// only process if not (fallowed by a semicolon or is a pseudo selector)
Packit Service 7770af
						if (!(sass.at(pos_value) == ':' || isPseudoSelector(pseudo)))
Packit Service 7770af
						{
Packit Service 7770af
							// create new string by interchanging the colon sign for property and value
Packit Service 7770af
							sass = indent + sass.substr(pos_left + 1, pos_wspace - pos_left - 1) + ":" + sass.substr(pos_wspace);
Packit Service 7770af
							// try to find a colon in the current line, but only ...
Packit Service 7770af
							size_t pos_colon = sass.find_first_not_of(":", pos_left);
Packit Service 7770af
							// assertion for valid result
Packit Service 7770af
							if (pos_colon != std::string::npos)
Packit Service 7770af
							{
Packit Service 7770af
								// ... after the first word (skip begining colons)
Packit Service 7770af
								pos_colon = sass.find_first_of(":", pos_colon);
Packit Service 7770af
								// it is a selector if there was no colon found
Packit Service 7770af
								converter.selector = pos_colon == std::string::npos;
Packit Service 7770af
							}
Packit Service 7770af
						}
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
				// check if we have a BEM property (one colon and no selector)
Packit Service 7770af
				if (sass.substr(pos_left, 1) == ":" && converter.selector == true) {
Packit Service 7770af
					size_t pos_wspace = sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left);
Packit Service 7770af
					sass = indent + sass.substr(pos_left + 1, pos_wspace) + ":";
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// terminate some statements immediately
Packit Service 7770af
			else if (
Packit Service 7770af
				sass.substr(pos_left, 5) == "@warn" ||
Packit Service 7770af
				sass.substr(pos_left, 6) == "@debug" ||
Packit Service 7770af
				sass.substr(pos_left, 6) == "@error" ||
Packit Service 7770af
				sass.substr(pos_left, 8) == "@charset" ||
Packit Service 7770af
				sass.substr(pos_left, 10) == "@namespace"
Packit Service 7770af
			) { sass = indent + sass.substr(pos_left); }
Packit Service 7770af
			// replace some specific sass shorthand directives (if not fallowed by a white space character)
Packit Service 7770af
			else if (sass.substr(pos_left, 1) == "=")
Packit Service 7770af
			{ sass = indent + "@mixin " + sass.substr(pos_left + 1); }
Packit Service 7770af
			else if (sass.substr(pos_left, 1) == "+")
Packit Service 7770af
			{
Packit Service 7770af
				// must be followed by a mixin call (no whitespace afterwards or at ending directly)
Packit Service 7770af
				if (sass[pos_left+1] != 0 && sass[pos_left+1] != ' ' && sass[pos_left+1] != '\t') {
Packit Service 7770af
					sass = indent + "@include " + sass.substr(pos_left + 1);
Packit Service 7770af
				}
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// add quotes for import if needed
Packit Service 7770af
			else if (sass.substr(pos_left, 7) == "@import")
Packit Service 7770af
			{
Packit Service 7770af
				// get positions for the actual import url
Packit Service 7770af
				size_t pos_import = sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left + 7);
Packit Service 7770af
				size_t pos_quote = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE, pos_import);
Packit Service 7770af
				// leave proper urls untouched
Packit Service 7770af
				if (sass.substr(pos_quote, 4) != "url(")
Packit Service 7770af
				{
Packit Service 7770af
					// check if the url appears to be already quoted
Packit Service 7770af
					if (sass.substr(pos_quote, 1) != "\"" && sass.substr(pos_quote, 1) != "\'")
Packit Service 7770af
					{
Packit Service 7770af
						// get position of the last char on the line
Packit Service 7770af
						size_t pos_end = sass.find_last_not_of(SASS2SCSS_FIND_WHITESPACE);
Packit Service 7770af
						// assertion check for valid result
Packit Service 7770af
						if (pos_end != std::string::npos)
Packit Service 7770af
						{
Packit Service 7770af
							// add quotes around the full line after the import statement
Packit Service 7770af
							sass = sass.substr(0, pos_quote) + "\"" + sass.substr(pos_quote, pos_end - pos_quote + 1) + "\"";
Packit Service 7770af
						}
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
			else if (
Packit Service 7770af
				sass.substr(pos_left, 7) != "@return" &&
Packit Service 7770af
				sass.substr(pos_left, 7) != "@extend" &&
Packit Service 7770af
				sass.substr(pos_left, 8) != "@include" &&
Packit Service 7770af
				sass.substr(pos_left, 8) != "@content"
Packit Service 7770af
			) {
Packit Service 7770af
Packit Service 7770af
				// probably a selector anyway
Packit Service 7770af
				converter.selector = true;
Packit Service 7770af
				// try to find first colon in the current line
Packit Service 7770af
				size_t pos_colon = sass.find_first_of(":", pos_left);
Packit Service 7770af
				// assertion that we have a colon
Packit Service 7770af
				if (pos_colon != std::string::npos)
Packit Service 7770af
				{
Packit Service 7770af
					// it is not a selector if we have a space after a colon
Packit Service 7770af
					if (sass[pos_colon+1] == ' ') converter.selector = false;
Packit Service 7770af
					if (sass[pos_colon+1] == '	') converter.selector = false;
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// current line has more indentation
Packit Service 7770af
			if (indent.length() >= INDENT(converter).length())
Packit Service 7770af
			{
Packit Service 7770af
				// not in comment mode
Packit Service 7770af
				if (IS_PARSING(converter))
Packit Service 7770af
				{
Packit Service 7770af
					// has meaningfull chars
Packit Service 7770af
					if (hasCharData(sass))
Packit Service 7770af
					{
Packit Service 7770af
						// is probably a property
Packit Service 7770af
						// also true for selectors
Packit Service 7770af
						converter.property = true;
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
			}
Packit Service 7770af
			// current line has more indentation
Packit Service 7770af
			if (indent.length() > INDENT(converter).length())
Packit Service 7770af
			{
Packit Service 7770af
				// not in comment mode
Packit Service 7770af
				if (IS_PARSING(converter))
Packit Service 7770af
				{
Packit Service 7770af
					// had meaningfull chars
Packit Service 7770af
					if (converter.property)
Packit Service 7770af
					{
Packit Service 7770af
						// print block opener
Packit Service 7770af
						scss += opener(converter);
Packit Service 7770af
						// push new stack context
Packit Service 7770af
						converter.indents.push("");
Packit Service 7770af
						// store block indentation
Packit Service 7770af
						INDENT(converter) = indent;
Packit Service 7770af
					}
Packit Service 7770af
				}
Packit Service 7770af
				// is and will be a src comment
Packit Service 7770af
				else if (!IS_CSS_COMMENT(converter))
Packit Service 7770af
				{
Packit Service 7770af
					// scss does not allow multiline src comments
Packit Service 7770af
					// therefore add forward slashes to all lines
Packit Service 7770af
					sass.at(INDENT(converter).length()+0) = '/';
Packit Service 7770af
					// there is an edge case here if indentation
Packit Service 7770af
					// is minimal (will overwrite the fist char)
Packit Service 7770af
					sass.at(INDENT(converter).length()+1) = '/';
Packit Service 7770af
					// could code around that, but I dont' think
Packit Service 7770af
					// this will ever be the cause for any trouble
Packit Service 7770af
				}
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// line is opening a new comment
Packit Service 7770af
			if (open == "/*" || open == "//")
Packit Service 7770af
			{
Packit Service 7770af
				// reset the property state
Packit Service 7770af
				converter.property = false;
Packit Service 7770af
				// close previous comment
Packit Service 7770af
				if (IS_CSS_COMMENT(converter) && open != "")
Packit Service 7770af
				{
Packit Service 7770af
					if (!STRIP_COMMENT(converter) && !CONVERT_COMMENT(converter)) scss += " */";
Packit Service 7770af
				}
Packit Service 7770af
				// force single line comments
Packit Service 7770af
				// into a correct css comment
Packit Service 7770af
				if (CONVERT_COMMENT(converter))
Packit Service 7770af
				{
Packit Service 7770af
					if (IS_PARSING(converter))
Packit Service 7770af
					{ sass.at(pos_left + 1) = '*'; }
Packit Service 7770af
				}
Packit Service 7770af
				// set comment flag
Packit Service 7770af
				converter.comment = open;
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// flush data only under certain conditions
Packit Service 7770af
			if (!(
Packit Service 7770af
				// strip css and src comments if option is set
Packit Service 7770af
				(IS_COMMENT(converter) && STRIP_COMMENT(converter)) ||
Packit Service 7770af
				// strip src comment even if strip option is not set
Packit Service 7770af
				// but only if the keep src comment option is not set
Packit Service 7770af
				(IS_SRC_COMMENT(converter) && ! KEEP_COMMENT(converter))
Packit Service 7770af
			))
Packit Service 7770af
			{
Packit Service 7770af
				// flush data and buffer whitespace
Packit Service 7770af
				scss += flush(sass, converter);
Packit Service 7770af
			}
Packit Service 7770af
Packit Service 7770af
			// get postion of last meaningfull char
Packit Service 7770af
			size_t pos_right = sass.find_last_not_of(SASS2SCSS_FIND_WHITESPACE);
Packit Service 7770af
Packit Service 7770af
			// check for invalid result
Packit Service 7770af
			if (pos_right != std::string::npos)
Packit Service 7770af
			{
Packit Service 7770af
Packit Service 7770af
				// get the last meaningfull char
Packit Service 7770af
				std::string close = sass.substr(pos_right, 1);
Packit Service 7770af
Packit Service 7770af
				// check if next line should be concatenated (list mode)
Packit Service 7770af
				converter.comma = IS_PARSING(converter) && close == ",";
Packit Service 7770af
				converter.semicolon = IS_PARSING(converter) && close == ";";
Packit Service 7770af
Packit Service 7770af
				// check if we have more than
Packit Service 7770af
				// one meaningfull char
Packit Service 7770af
				if (pos_right > 0)
Packit Service 7770af
				{
Packit Service 7770af
Packit Service 7770af
					// get the last two chars from string
Packit Service 7770af
					std::string close = sass.substr(pos_right - 1, 2);
Packit Service 7770af
					// update parser status for expicitly closed comment
Packit Service 7770af
					if (close == "*/") converter.comment = "";
Packit Service 7770af
Packit Service 7770af
				}
Packit Service 7770af
Packit Service 7770af
			}
Packit Service 7770af
			// EO have meaningfull chars from end
Packit Service 7770af
Packit Service 7770af
		}
Packit Service 7770af
		// EO have meaningfull chars from start
Packit Service 7770af
Packit Service 7770af
		// return scss
Packit Service 7770af
		return scss;
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
	// EO process
Packit Service 7770af
Packit Service 7770af
	// read line with either CR, LF or CR LF format
Packit Service 7770af
	// http://stackoverflow.com/a/6089413/1550314
Packit Service 7770af
	static std::istream& safeGetline(std::istream& is, std::string& t)
Packit Service 7770af
	{
Packit Service 7770af
		t.clear();
Packit Service 7770af
Packit Service 7770af
		// The characters in the stream are read one-by-one using a std::streambuf.
Packit Service 7770af
		// That is faster than reading them one-by-one using the std::istream.
Packit Service 7770af
		// Code that uses streambuf this way must be guarded by a sentry object.
Packit Service 7770af
		// The sentry object performs various tasks,
Packit Service 7770af
		// such as thread synchronization and updating the stream state.
Packit Service 7770af
Packit Service 7770af
		std::istream::sentry se(is, true);
Packit Service 7770af
		std::streambuf* sb = is.rdbuf();
Packit Service 7770af
Packit Service 7770af
		for(;;) {
Packit Service 7770af
			int c = sb->sbumpc();
Packit Service 7770af
			switch (c) {
Packit Service 7770af
				case '\n':
Packit Service 7770af
					return is;
Packit Service 7770af
				case '\r':
Packit Service 7770af
					if(sb->sgetc() == '\n')
Packit Service 7770af
						sb->sbumpc();
Packit Service 7770af
					return is;
Packit Service 7770af
				case EOF:
Packit Service 7770af
					// Also handle the case when the last line has no line ending
Packit Service 7770af
					if(t.empty())
Packit Service 7770af
						is.setstate(std::ios::eofbit);
Packit Service 7770af
					return is;
Packit Service 7770af
				default:
Packit Service 7770af
					t += (char)c;
Packit Service 7770af
			}
Packit Service 7770af
		}
Packit Service 7770af
	}
Packit Service 7770af
Packit Service 7770af
	// the main converter function for c++
Packit Service 7770af
	char* sass2scss (const std::string& sass, const int options)
Packit Service 7770af
	{
Packit Service 7770af
Packit Service 7770af
		// local variables
Packit Service 7770af
		std::string line;
Packit Service 7770af
		std::string scss = "";
Packit Service 7770af
		std::stringstream stream(sass);
Packit Service 7770af
Packit Service 7770af
		// create converter variable
Packit Service 7770af
		converter converter;
Packit Service 7770af
		// initialise all options
Packit Service 7770af
		converter.comma = false;
Packit Service 7770af
		converter.property = false;
Packit Service 7770af
		converter.selector = false;
Packit Service 7770af
		converter.semicolon = false;
Packit Service 7770af
		converter.end_of_file = false;
Packit Service 7770af
		converter.comment = "";
Packit Service 7770af
		converter.whitespace = "";
Packit Service 7770af
		converter.indents.push("");
Packit Service 7770af
		converter.options = options;
Packit Service 7770af
Packit Service 7770af
		// read line by line and process them
Packit Service 7770af
		while(safeGetline(stream, line) && !stream.eof())
Packit Service 7770af
		{ scss += process(line, converter); }
Packit Service 7770af
Packit Service 7770af
		// create mutable string
Packit Service 7770af
		std::string closer = "";
Packit Service 7770af
		// set the end of file flag
Packit Service 7770af
		converter.end_of_file = true;
Packit Service 7770af
		// process to close all open blocks
Packit Service 7770af
		scss += process(closer, converter);
Packit Service 7770af
Packit Service 7770af
		// allocate new memory on the heap
Packit Service 7770af
		// caller has to free it after use
Packit Service 7770af
		char * cstr = (char*) malloc (scss.length() + 1);
Packit Service 7770af
		// create a copy of the string
Packit Service 7770af
		strcpy (cstr, scss.c_str());
Packit Service 7770af
		// return pointer
Packit Service 7770af
		return &cstr[0];
Packit Service 7770af
Packit Service 7770af
	}
Packit Service 7770af
	// EO sass2scss
Packit Service 7770af
Packit Service 7770af
}
Packit Service 7770af
// EO namespace
Packit Service 7770af
Packit Service 7770af
// implement for c
Packit Service 7770af
extern "C"
Packit Service 7770af
{
Packit Service 7770af
Packit Service 7770af
	char* ADDCALL sass2scss (const char* sass, const int options)
Packit Service 7770af
	{
Packit Service 7770af
		return Sass::sass2scss(sass, options);
Packit Service 7770af
	}
Packit Service 7770af
Packit Service 7770af
	// Get compiled sass2scss version
Packit Service 7770af
	const char* ADDCALL sass2scss_version(void) {
Packit Service 7770af
		return SASS2SCSS_VERSION;
Packit Service 7770af
	}
Packit Service 7770af
Packit Service 7770af
}