PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/front/node_modules/node-sass/src/libsass/src/sass2scss.cpp

https://gitlab.com/boxnia/NFU_MOVIL
C++ | 845 lines | 503 code | 117 blank | 225 comment | 341 complexity | fcfada8a8c8b9e58044e0a00c4f6dea3 MD5 | raw file
  1. /**
  2. * sass2scss
  3. * Licensed under the MIT License
  4. * Copyright (c) Marcel Greter
  5. */
  6. #ifdef _MSC_VER
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #define _CRT_NONSTDC_NO_DEPRECATE
  9. #endif
  10. // include library
  11. #include <stack>
  12. #include <string>
  13. #include <cstring>
  14. #include <cstdlib>
  15. #include <sstream>
  16. #include <iostream>
  17. #include <stdio.h>
  18. ///*
  19. //
  20. // src comments: comments in sass syntax (staring with //)
  21. // css comments: multiline comments in css syntax (starting with /*)
  22. //
  23. // KEEP_COMMENT: keep src comments in the resulting css code
  24. // STRIP_COMMENT: strip out all comments (either src or css)
  25. // CONVERT_COMMENT: convert all src comments to css comments
  26. //
  27. //*/
  28. // our own header
  29. #include "sass2scss.h"
  30. // add namespace for c++
  31. namespace Sass
  32. {
  33. // return the actual prettify value from options
  34. #define PRETTIFY(converter) (converter.options - (converter.options & 248))
  35. // query the options integer to check if the option is enables
  36. #define KEEP_COMMENT(converter) ((converter.options & SASS2SCSS_KEEP_COMMENT) == SASS2SCSS_KEEP_COMMENT)
  37. #define STRIP_COMMENT(converter) ((converter.options & SASS2SCSS_STRIP_COMMENT) == SASS2SCSS_STRIP_COMMENT)
  38. #define CONVERT_COMMENT(converter) ((converter.options & SASS2SCSS_CONVERT_COMMENT) == SASS2SCSS_CONVERT_COMMENT)
  39. // some makros to access the indentation stack
  40. #define INDENT(converter) (converter.indents.top())
  41. // some makros to query comment parser status
  42. #define IS_PARSING(converter) (converter.comment == "")
  43. #define IS_COMMENT(converter) (converter.comment != "")
  44. #define IS_SRC_COMMENT(converter) (converter.comment == "//" && ! CONVERT_COMMENT(converter))
  45. #define IS_CSS_COMMENT(converter) (converter.comment == "/*" || (converter.comment == "//" && CONVERT_COMMENT(converter)))
  46. // pretty printer helper function
  47. static std::string closer (const converter& converter)
  48. {
  49. return PRETTIFY(converter) == 0 ? " }" :
  50. PRETTIFY(converter) <= 1 ? " }" :
  51. "\n" + INDENT(converter) + "}";
  52. }
  53. // pretty printer helper function
  54. static std::string opener (const converter& converter)
  55. {
  56. return PRETTIFY(converter) == 0 ? " { " :
  57. PRETTIFY(converter) <= 2 ? " {" :
  58. "\n" + INDENT(converter) + "{";
  59. }
  60. // check if the given string is a pseudo selector
  61. // needed to differentiate from sass property syntax
  62. static bool isPseudoSelector (std::string& sel)
  63. {
  64. size_t len = sel.length();
  65. if (len < 1) return false;
  66. size_t pos = sel.find_first_not_of("abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1);
  67. if (pos != std::string::npos) sel.erase(pos, std::string::npos);
  68. size_t i = sel.length();
  69. while (i -- > 0) { sel.at(i) = tolower(sel.at(i)); }
  70. // CSS Level 1 - Recommendation
  71. if (sel == ":link") return true;
  72. if (sel == ":visited") return true;
  73. if (sel == ":active") return true;
  74. // CSS Level 2 (Revision 1) - Recommendation
  75. if (sel == ":lang") return true;
  76. if (sel == ":first-child") return true;
  77. if (sel == ":hover") return true;
  78. if (sel == ":focus") return true;
  79. // disabled - also valid properties
  80. // if (sel == ":left") return true;
  81. // if (sel == ":right") return true;
  82. if (sel == ":first") return true;
  83. // Selectors Level 3 - Recommendation
  84. if (sel == ":target") return true;
  85. if (sel == ":root") return true;
  86. if (sel == ":nth-child") return true;
  87. if (sel == ":nth-last-of-child") return true;
  88. if (sel == ":nth-of-type") return true;
  89. if (sel == ":nth-last-of-type") return true;
  90. if (sel == ":last-child") return true;
  91. if (sel == ":first-of-type") return true;
  92. if (sel == ":last-of-type") return true;
  93. if (sel == ":only-child") return true;
  94. if (sel == ":only-of-type") return true;
  95. if (sel == ":empty") return true;
  96. if (sel == ":not") return true;
  97. // CSS Basic User Interface Module Level 3 - Working Draft
  98. if (sel == ":default") return true;
  99. if (sel == ":valid") return true;
  100. if (sel == ":invalid") return true;
  101. if (sel == ":in-range") return true;
  102. if (sel == ":out-of-range") return true;
  103. if (sel == ":required") return true;
  104. if (sel == ":optional") return true;
  105. if (sel == ":read-only") return true;
  106. if (sel == ":read-write") return true;
  107. if (sel == ":dir") return true;
  108. if (sel == ":enabled") return true;
  109. if (sel == ":disabled") return true;
  110. if (sel == ":checked") return true;
  111. if (sel == ":indeterminate") return true;
  112. if (sel == ":nth-last-child") return true;
  113. // Selectors Level 4 - Working Draft
  114. if (sel == ":any-link") return true;
  115. if (sel == ":local-link") return true;
  116. if (sel == ":scope") return true;
  117. if (sel == ":active-drop-target") return true;
  118. if (sel == ":valid-drop-target") return true;
  119. if (sel == ":invalid-drop-target") return true;
  120. if (sel == ":current") return true;
  121. if (sel == ":past") return true;
  122. if (sel == ":future") return true;
  123. if (sel == ":placeholder-shown") return true;
  124. if (sel == ":user-error") return true;
  125. if (sel == ":blank") return true;
  126. if (sel == ":nth-match") return true;
  127. if (sel == ":nth-last-match") return true;
  128. if (sel == ":nth-column") return true;
  129. if (sel == ":nth-last-column") return true;
  130. if (sel == ":matches") return true;
  131. // Fullscreen API - Living Standard
  132. if (sel == ":fullscreen") return true;
  133. // not a pseudo selector
  134. return false;
  135. }
  136. // check if there is some char data
  137. // will ignore everything in comments
  138. static bool hasCharData (std::string& sass)
  139. {
  140. size_t col_pos = 0;
  141. while (true)
  142. {
  143. // try to find some meaningfull char
  144. col_pos = sass.find_first_not_of(" \t\n\v\f\r", col_pos);
  145. // there was no meaningfull char found
  146. if (col_pos == std::string::npos) return false;
  147. // found a multiline comment opener
  148. if (sass.substr(col_pos, 2) == "/*")
  149. {
  150. // find the multiline comment closer
  151. col_pos = sass.find("*/", col_pos);
  152. // maybe we did not find the closer here
  153. if (col_pos == std::string::npos) return false;
  154. // skip closer
  155. col_pos += 2;
  156. }
  157. else
  158. {
  159. return true;
  160. }
  161. }
  162. }
  163. // EO hasCharData
  164. // find src comment opener
  165. // correctly skips quoted strings
  166. static size_t findCommentOpener (std::string& sass)
  167. {
  168. size_t col_pos = 0;
  169. bool apoed = false;
  170. bool quoted = false;
  171. bool comment = false;
  172. size_t brackets = 0;
  173. while (col_pos != std::string::npos)
  174. {
  175. // process all interesting chars
  176. col_pos = sass.find_first_of("\"\'/\\*()", col_pos);
  177. // assertion for valid result
  178. if (col_pos != std::string::npos)
  179. {
  180. char character = sass.at(col_pos);
  181. if (character == '(')
  182. {
  183. if (!quoted && !apoed) brackets ++;
  184. }
  185. else if (character == ')')
  186. {
  187. if (!quoted && !apoed) brackets --;
  188. }
  189. else if (character == '\"')
  190. {
  191. // invert quote bool
  192. if (!apoed && !comment) quoted = !quoted;
  193. }
  194. else if (character == '\'')
  195. {
  196. // invert quote bool
  197. if (!quoted && !comment) apoed = !apoed;
  198. }
  199. else if (col_pos > 0 && character == '/')
  200. {
  201. if (sass.at(col_pos - 1) == '*')
  202. {
  203. comment = false;
  204. }
  205. // next needs to be a slash too
  206. else if (sass.at(col_pos - 1) == '/')
  207. {
  208. // only found if not in single or double quote, bracket or comment
  209. if (!quoted && !apoed && !comment && brackets == 0) return col_pos - 1;
  210. }
  211. }
  212. else if (character == '\\')
  213. {
  214. // skip next char if in quote
  215. if (quoted || apoed) col_pos ++;
  216. }
  217. // this might be a comment opener
  218. else if (col_pos > 0 && character == '*')
  219. {
  220. // opening a multiline comment
  221. if (sass.at(col_pos - 1) == '/')
  222. {
  223. // we are now in a comment
  224. if (!quoted && !apoed) comment = true;
  225. }
  226. }
  227. // skip char
  228. col_pos ++;
  229. }
  230. }
  231. // EO while
  232. return col_pos;
  233. }
  234. // EO findCommentOpener
  235. // remove multiline comments from sass string
  236. // correctly skips quoted strings
  237. static std::string removeMultilineComment (std::string &sass)
  238. {
  239. std::string clean = "";
  240. size_t col_pos = 0;
  241. size_t open_pos = 0;
  242. size_t close_pos = 0;
  243. bool apoed = false;
  244. bool quoted = false;
  245. bool comment = false;
  246. // process sass til string end
  247. while (col_pos != std::string::npos)
  248. {
  249. // process all interesting chars
  250. col_pos = sass.find_first_of("\"\'/\\*", col_pos);
  251. // assertion for valid result
  252. if (col_pos != std::string::npos)
  253. {
  254. char character = sass.at(col_pos);
  255. // found quoted string delimiter
  256. if (character == '\"')
  257. {
  258. if (!apoed && !comment) quoted = !quoted;
  259. }
  260. else if (character == '\'')
  261. {
  262. if (!quoted && !comment) apoed = !apoed;
  263. }
  264. // found possible comment closer
  265. else if (character == '/')
  266. {
  267. // look back to see if it is actually a closer
  268. if (comment && col_pos > 0 && sass.at(col_pos - 1) == '*')
  269. {
  270. close_pos = col_pos + 1; comment = false;
  271. }
  272. }
  273. else if (character == '\\')
  274. {
  275. // skip escaped char
  276. if (quoted || apoed) col_pos ++;
  277. }
  278. // this might be a comment opener
  279. else if (character == '*')
  280. {
  281. // look back to see if it is actually an opener
  282. if (!quoted && !apoed && col_pos > 0 && sass.at(col_pos - 1) == '/')
  283. {
  284. comment = true; open_pos = col_pos - 1;
  285. clean += sass.substr(close_pos, open_pos - close_pos);
  286. }
  287. }
  288. // skip char
  289. col_pos ++;
  290. }
  291. }
  292. // EO while
  293. // add final parts (add half open comment text)
  294. if (comment) clean += sass.substr(open_pos);
  295. else clean += sass.substr(close_pos);
  296. // return string
  297. return clean;
  298. }
  299. // EO removeMultilineComment
  300. // right trim a given string
  301. std::string rtrim(const std::string &sass)
  302. {
  303. std::string trimmed = sass;
  304. size_t pos_ws = trimmed.find_last_not_of(" \t\n\v\f\r");
  305. if (pos_ws != std::string::npos)
  306. { trimmed.erase(pos_ws + 1); }
  307. else { trimmed.clear(); }
  308. return trimmed;
  309. }
  310. // EO rtrim
  311. // flush whitespace and print additional text, but
  312. // only print additional chars and buffer whitespace
  313. std::string flush (std::string& sass, converter& converter)
  314. {
  315. // return flushed
  316. std::string scss = "";
  317. // print whitespace buffer
  318. scss += PRETTIFY(converter) > 0 ?
  319. converter.whitespace : "";
  320. // reset whitespace buffer
  321. converter.whitespace = "";
  322. // remove possible newlines from string
  323. size_t pos_right = sass.find_last_not_of("\n\r");
  324. if (pos_right == std::string::npos) return scss;
  325. // get the linefeeds from the string
  326. std::string lfs = sass.substr(pos_right + 1);
  327. sass = sass.substr(0, pos_right + 1);
  328. // find some source comment opener
  329. size_t comment_pos = findCommentOpener(sass);
  330. // check if there was a source comment
  331. if (comment_pos != std::string::npos)
  332. {
  333. // convert comment (but only outside other coments)
  334. if (CONVERT_COMMENT(converter) && !IS_COMMENT(converter))
  335. {
  336. // convert to multiline comment
  337. sass.at(comment_pos + 1) = '*';
  338. // add comment node to the whitespace
  339. sass += " */";
  340. }
  341. // not at line start
  342. if (comment_pos > 0)
  343. {
  344. // also include whitespace before the actual comment opener
  345. size_t ws_pos = sass.find_last_not_of(SASS2SCSS_FIND_WHITESPACE, comment_pos - 1);
  346. comment_pos = ws_pos == std::string::npos ? 0 : ws_pos + 1;
  347. }
  348. if (!STRIP_COMMENT(converter))
  349. {
  350. // add comment node to the whitespace
  351. converter.whitespace += sass.substr(comment_pos);
  352. }
  353. else
  354. {
  355. // sass = removeMultilineComments(sass);
  356. }
  357. // update the actual sass code
  358. sass = sass.substr(0, comment_pos);
  359. }
  360. // add newline as getline discharged it
  361. converter.whitespace += lfs + "\n";
  362. // maybe remove any leading whitespace
  363. if (PRETTIFY(converter) == 0)
  364. {
  365. // remove leading whitespace and update string
  366. size_t pos_left = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE);
  367. if (pos_left != std::string::npos) sass = sass.substr(pos_left);
  368. }
  369. // add flushed data
  370. scss += sass;
  371. // return string
  372. return scss;
  373. }
  374. // EO flush
  375. // process a line of the sass text
  376. std::string process (std::string& sass, converter& converter)
  377. {
  378. // resulting string
  379. std::string scss = "";
  380. // strip multi line comments
  381. if (STRIP_COMMENT(converter))
  382. {
  383. sass = removeMultilineComment(sass);
  384. }
  385. // right trim input
  386. sass = rtrim(sass);
  387. // get postion of first meaningfull character in string
  388. size_t pos_left = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE);
  389. // special case for final run
  390. if (converter.end_of_file) pos_left = 0;
  391. // maybe has only whitespace
  392. if (pos_left == std::string::npos)
  393. {
  394. // just add complete whitespace
  395. converter.whitespace += sass + "\n";
  396. }
  397. // have meaningfull first char
  398. else
  399. {
  400. // extract and store indentation string
  401. std::string indent = sass.substr(0, pos_left);
  402. // check if current line starts a comment
  403. std::string open = sass.substr(pos_left, 2);
  404. // line has less or same indentation
  405. // finalize previous open parser context
  406. if (indent.length() <= INDENT(converter).length())
  407. {
  408. // close multilinie comment
  409. if (IS_CSS_COMMENT(converter))
  410. {
  411. // check if comments will be stripped anyway
  412. if (!STRIP_COMMENT(converter)) scss += " */";
  413. }
  414. // close src comment comment
  415. else if (IS_SRC_COMMENT(converter))
  416. {
  417. // add a newline to avoid closer on same line
  418. // this would put the bracket in the comment node
  419. // no longer needed since we parse them correctly
  420. // if (KEEP_COMMENT(converter)) scss += "\n";
  421. }
  422. // close css properties
  423. else if (converter.property)
  424. {
  425. // add closer unless in concat mode
  426. if (!converter.comma)
  427. {
  428. // if there was no colon we have a selector
  429. // looks like there were no inner properties
  430. if (converter.selector) scss += " {}";
  431. // add final semicolon
  432. else if (!converter.semicolon) scss += ";";
  433. }
  434. }
  435. // reset comment state
  436. converter.comment = "";
  437. }
  438. // make sure we close every "higher" block
  439. while (indent.length() < INDENT(converter).length())
  440. {
  441. // pop stacked context
  442. converter.indents.pop();
  443. // print close bracket
  444. if (IS_PARSING(converter))
  445. { scss += closer(converter); }
  446. else { scss += " */"; }
  447. // reset comment state
  448. converter.comment = "";
  449. }
  450. // reset converter state
  451. converter.selector = false;
  452. // check if we have sass property syntax
  453. if (sass.substr(pos_left, 1) == ":" && sass.substr(pos_left, 2) != "::")
  454. {
  455. // default to a selector
  456. // change back if property found
  457. converter.selector = true;
  458. // get postion of first whitespace char
  459. size_t pos_wspace = sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left);
  460. // assertion check for valid result
  461. if (pos_wspace != std::string::npos)
  462. {
  463. // get the possible pseudo selector
  464. std::string pseudo = sass.substr(pos_left, pos_wspace - pos_left);
  465. // get position of the first real property value char
  466. // pseudo selectors get this far, but have no actual value
  467. size_t pos_value = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE, pos_wspace);
  468. // assertion check for valid result
  469. if (pos_value != std::string::npos)
  470. {
  471. // only process if not (fallowed by a semicolon or is a pseudo selector)
  472. if (!(sass.at(pos_value) == ':' || isPseudoSelector(pseudo)))
  473. {
  474. // create new string by interchanging the colon sign for property and value
  475. sass = indent + sass.substr(pos_left + 1, pos_wspace - pos_left - 1) + ":" + sass.substr(pos_wspace);
  476. // try to find a colon in the current line, but only ...
  477. size_t pos_colon = sass.find_first_not_of(":", pos_left);
  478. // assertion for valid result
  479. if (pos_colon != std::string::npos)
  480. {
  481. // ... after the first word (skip begining colons)
  482. pos_colon = sass.find_first_of(":", pos_colon);
  483. // it is a selector if there was no colon found
  484. converter.selector = pos_colon == std::string::npos;
  485. }
  486. }
  487. }
  488. }
  489. }
  490. // terminate some statements immediately
  491. else if (
  492. sass.substr(pos_left, 5) == "@warn" ||
  493. sass.substr(pos_left, 6) == "@debug" ||
  494. sass.substr(pos_left, 6) == "@error" ||
  495. sass.substr(pos_left, 8) == "@charset"
  496. ) { sass = indent + sass.substr(pos_left); }
  497. // replace some specific sass shorthand directives (if not fallowed by a white space character)
  498. else if (sass.substr(pos_left, 1) == "=" && sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left) != pos_left + 1)
  499. { sass = indent + "@mixin " + sass.substr(pos_left + 1); }
  500. else if (sass.substr(pos_left, 1) == "+" && sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left) != pos_left + 1)
  501. { sass = indent + "@include " + sass.substr(pos_left + 1); }
  502. // add quotes for import if needed
  503. else if (sass.substr(pos_left, 7) == "@import")
  504. {
  505. // get positions for the actual import url
  506. size_t pos_import = sass.find_first_of(SASS2SCSS_FIND_WHITESPACE, pos_left + 7);
  507. size_t pos_quote = sass.find_first_not_of(SASS2SCSS_FIND_WHITESPACE, pos_import);
  508. // leave proper urls untouched
  509. if (sass.substr(pos_quote, 4) != "url(")
  510. {
  511. // check if the url appears to be already quoted
  512. if (sass.substr(pos_quote, 1) != "\"" && sass.substr(pos_quote, 1) != "\'")
  513. {
  514. // get position of the last char on the line
  515. size_t pos_end = sass.find_last_not_of(SASS2SCSS_FIND_WHITESPACE);
  516. // assertion check for valid result
  517. if (pos_end != std::string::npos)
  518. {
  519. // add quotes around the full line after the import statement
  520. sass = sass.substr(0, pos_quote) + "\"" + sass.substr(pos_quote, pos_end - pos_quote + 1) + "\"";
  521. }
  522. }
  523. }
  524. }
  525. else if (
  526. sass.substr(pos_left, 7) != "@return" &&
  527. sass.substr(pos_left, 7) != "@extend" &&
  528. sass.substr(pos_left, 8) != "@include" &&
  529. sass.substr(pos_left, 8) != "@content"
  530. ) {
  531. // probably a selector anyway
  532. converter.selector = true;
  533. // try to find first colon in the current line
  534. size_t pos_colon = sass.find_first_of(":", pos_left);
  535. // assertion that we have a colon
  536. if (pos_colon != std::string::npos)
  537. {
  538. // it is not a selector if we have a space after a colon
  539. if (sass[pos_colon+1] == ' ') converter.selector = false;
  540. if (sass[pos_colon+1] == ' ') converter.selector = false;
  541. }
  542. }
  543. // current line has more indentation
  544. if (indent.length() >= INDENT(converter).length())
  545. {
  546. // not in comment mode
  547. if (IS_PARSING(converter))
  548. {
  549. // has meaningfull chars
  550. if (hasCharData(sass))
  551. {
  552. // is probably a property
  553. // also true for selectors
  554. converter.property = true;
  555. }
  556. }
  557. }
  558. // current line has more indentation
  559. if (indent.length() > INDENT(converter).length())
  560. {
  561. // not in comment mode
  562. if (IS_PARSING(converter))
  563. {
  564. // had meaningfull chars
  565. if (converter.property)
  566. {
  567. // print block opener
  568. scss += opener(converter);
  569. // push new stack context
  570. converter.indents.push("");
  571. // store block indentation
  572. INDENT(converter) = indent;
  573. }
  574. }
  575. // is and will be a src comment
  576. else if (!IS_CSS_COMMENT(converter))
  577. {
  578. // scss does not allow multiline src comments
  579. // therefore add forward slashes to all lines
  580. sass.at(INDENT(converter).length()+0) = '/';
  581. // there is an edge case here if indentation
  582. // is minimal (will overwrite the fist char)
  583. sass.at(INDENT(converter).length()+1) = '/';
  584. // could code around that, but I dont' think
  585. // this will ever be the cause for any trouble
  586. }
  587. }
  588. // line is opening a new comment
  589. if (open == "/*" || open == "//")
  590. {
  591. // reset the property state
  592. converter.property = false;
  593. // close previous comment
  594. if (IS_CSS_COMMENT(converter) && open != "")
  595. {
  596. if (!STRIP_COMMENT(converter) && !CONVERT_COMMENT(converter)) scss += " */";
  597. }
  598. // force single line comments
  599. // into a correct css comment
  600. if (CONVERT_COMMENT(converter))
  601. {
  602. if (IS_PARSING(converter))
  603. { sass.at(pos_left + 1) = '*'; }
  604. }
  605. // set comment flag
  606. converter.comment = open;
  607. }
  608. // flush data only under certain conditions
  609. if (!(
  610. // strip css and src comments if option is set
  611. (IS_COMMENT(converter) && STRIP_COMMENT(converter)) ||
  612. // strip src comment even if strip option is not set
  613. // but only if the keep src comment option is not set
  614. (IS_SRC_COMMENT(converter) && ! KEEP_COMMENT(converter))
  615. ))
  616. {
  617. // flush data and buffer whitespace
  618. scss += flush(sass, converter);
  619. }
  620. // get postion of last meaningfull char
  621. size_t pos_right = sass.find_last_not_of(SASS2SCSS_FIND_WHITESPACE);
  622. // check for invalid result
  623. if (pos_right != std::string::npos)
  624. {
  625. // get the last meaningfull char
  626. std::string close = sass.substr(pos_right, 1);
  627. // check if next line should be concatenated (list mode)
  628. converter.comma = IS_PARSING(converter) && close == ",";
  629. converter.semicolon = IS_PARSING(converter) && close == ";";
  630. // check if we have more than
  631. // one meaningfull char
  632. if (pos_right > 0)
  633. {
  634. // get the last two chars from string
  635. std::string close = sass.substr(pos_right - 1, 2);
  636. // update parser status for expicitly closed comment
  637. if (close == "*/") converter.comment = "";
  638. }
  639. }
  640. // EO have meaningfull chars from end
  641. }
  642. // EO have meaningfull chars from start
  643. // return scss
  644. return scss;
  645. }
  646. // EO process
  647. // read line with either CR, LF or CR LF format
  648. // http://stackoverflow.com/a/6089413/1550314
  649. static std::istream& safeGetline(std::istream& is, std::string& t)
  650. {
  651. t.clear();
  652. // The characters in the stream are read one-by-one using a std::streambuf.
  653. // That is faster than reading them one-by-one using the std::istream.
  654. // Code that uses streambuf this way must be guarded by a sentry object.
  655. // The sentry object performs various tasks,
  656. // such as thread synchronization and updating the stream state.
  657. std::istream::sentry se(is, true);
  658. std::streambuf* sb = is.rdbuf();
  659. for(;;) {
  660. int c = sb->sbumpc();
  661. switch (c) {
  662. case '\n':
  663. return is;
  664. case '\r':
  665. if(sb->sgetc() == '\n')
  666. sb->sbumpc();
  667. return is;
  668. case EOF:
  669. // Also handle the case when the last line has no line ending
  670. if(t.empty())
  671. is.setstate(std::ios::eofbit);
  672. return is;
  673. default:
  674. t += (char)c;
  675. }
  676. }
  677. }
  678. // the main converter function for c++
  679. char* sass2scss (const std::string& sass, const int options)
  680. {
  681. // local variables
  682. std::string line;
  683. std::string scss = "";
  684. std::stringstream stream(sass);
  685. // create converter variable
  686. converter converter;
  687. // initialise all options
  688. converter.comma = false;
  689. converter.property = false;
  690. converter.selector = false;
  691. converter.semicolon = false;
  692. converter.end_of_file = false;
  693. converter.comment = "";
  694. converter.whitespace = "";
  695. converter.indents.push("");
  696. converter.options = options;
  697. // read line by line and process them
  698. while(safeGetline(stream, line) && !stream.eof())
  699. { scss += process(line, converter); }
  700. // create mutable string
  701. std::string closer = "";
  702. // set the end of file flag
  703. converter.end_of_file = true;
  704. // process to close all open blocks
  705. scss += process(closer, converter);
  706. // allocate new memory on the heap
  707. // caller has to free it after use
  708. char * cstr = (char*) malloc (scss.length() + 1);
  709. // create a copy of the string
  710. strcpy (cstr, scss.c_str());
  711. // return pointer
  712. return &cstr[0];
  713. }
  714. // EO sass2scss
  715. }
  716. // EO namespace
  717. // implement for c
  718. extern "C"
  719. {
  720. char* ADDCALL sass2scss (const char* sass, const int options)
  721. {
  722. return Sass::sass2scss(sass, options);
  723. }
  724. // Get compiled sass2scss version
  725. const char* ADDCALL sass2scss_version(void) {
  726. return SASS2SCSS_VERSION;
  727. }
  728. }